为什么这个Nginx配置会导致“重写或内部重定向周期”

前端之家收集整理的这篇文章主要介绍了为什么这个Nginx配置会导致“重写或内部重定向周期”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有以下Nginx配置:

  1. server {
  2. listen 80;
  3. server_name mercury;
  4. access_log /var/log/Nginx/mercury.access.log;
  5. error_log /var/log/Nginx/mercury.error.log;
  6. location /static {
  7. add_header Cache-Control: max-age=31536000;
  8. }
  9. location / {
  10. root /opt/the-jam/www/dist/;
  11. try_files $uri /index.html;
  12. add_header Cache-Control: max-age=60;
  13. }
  14. }

我有目录结构:

  1. § tree /opt/the-jam/www/dist
  2. /opt/the-jam/www/dist
  3. ├── index.html
  4. └── static
  5. ├── 3522b60dabd4468d03f8.css
  6. └── 3522b60dabd4468d03f8.js

我收到了错误

  1. 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,为什么我得到重定向循环?

最佳答案
您的配置问题是,如果找不到/index.html,它将重定向到/index.html.即使您确定该文件在此处,也可以避免此类配置.像这样的配置没有这个问题:

  1. root /opt/the-jam/www/dist/;
  2. location / {
  3. try_files $uri /index.html;
  4. ...
  5. }
  6. location = /index.html {
  7. # no try_files here
  8. ...
  9. }

通过这样的配置,您还可以查看/index.html的错误以及无法访问的原因.我最好的猜测是,某些中间目录的访问权限不允许Nginx访问/opt/the-jam/www/dist/index.html.

猜你在找的Nginx相关文章