我已经成功设置了 Django、angularjs 和 phantomJS(出于 SEO 目的)来协同工作,但是在路线方面我遇到了一个小问题。
http://something.com/?_escaped_fragment_= 被正确重写,并且我得到了我的页面的渲染快照(由 phantomJS 提供),但是 http://something.com/news/?_escaped_fragment_= 没有被重写,并且我收到 404 未找到错误。
如果没有 _escaped_fragment_ 部分,两个站点都可以被找到并正确呈现。
这是我的 nginx 配置:
server {
listen 80;
server_name something.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/folder/project;
}
root /home/folder/project/frontend/;
index index.html;
if ($args ~ _escaped_fragment_) {
rewrite ^ /snapshot$uri;
}
location ~ ^/snapshot(.*) {
rewrite ^/snapshot(.*)$ $1 break;
# I commented out this line to test why PhantomJS wasn't rendering pages
# such as /news correctly
#proxy_pass http://something.com:8888;
proxy_set_header Host $host;
proxy_connect_timeout 60s;
}
location / {
#proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_pass http://unix:/home/folder/project/project.sock;
try_files $uri $uri/ /index.html;
}
location /admin/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/folder/project/project.sock;
}
}
我错过了什么?
编辑:基本上,问题在于通过 phantomJS 的请求 URL 是 http://something.comnews 的形式(请注意缺少 /)
答案1
好吧,看起来要将 phantomJS 的重写改为这样:
rewrite ^/snapshot(.*)$ /$1 break;
成功了。