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

我有以下Nginx配置:

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

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

root /opt/the-jam/www/dist/;

location / {
    try_files $uri /index.html;
    ...
}

location = /index.html {
    # no try_files here
    ...
}

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

相关文章

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...