微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这个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.

原文地址:https://www.jb51.cc/nginx/435449.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐