我有以下的Nginxconfiguration:
server { listen 80; server_name mercury; access_log /var/log/nginx/mercury.access.log; error_log /var/log/nginx/mercury.error.log; location /static { add_header Cache-Control: max-age=31536000; } location / { root /opt/the-jam/www/dist/; try_files $uri /index.html; add_header Cache-Control: max-age=60; } }
我有目录结构:
§ tree /opt/the-jam/www/dist /opt/the-jam/www/dist ├── index.html └── static ├── 3522b60dabd4468d03f8.css └── 3522b60dabd4468d03f8.js
而我得到的错误:
2015/10/20 14:25:26 [error] 4529#0: *95 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 0.0.0.0, server: the-jam, request: "GET /favicon.ico HTTP/1.1", host: "the-jam.example.com", referrer: "http://the-jam.example.com/"
这是一个单页面的应用程序,任何请求,即/foo/bar/baz应该只加载/index.html ,除非它在/static/[hash].js请求某些东西,所以我的理解是try_files指令将尝试在/foo/bar/baz加载文件,然后回退到/index.html ,为什么我要redirect循环?
你的configuration的问题是,如果/index.html找不到,它将redirect到/index.html 。 即使您确定文件在这里,这样的configuration也可以避免。 像这样的configuration没有这个问题:
root /opt/the-jam/www/dist/; location / { try_files $uri /index.html; ... } location = /index.html { # no try_files here ... }
有了这样的configuration,你也可以看到/index.html什么问题,为什么不能访问。 我最好的猜测是一些中间目录的访问权限不允许nginx访问/opt/the-jam/www/dist/index.html 。