加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:18:58 所属栏目: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/

我有以下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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读