带有try_files和index的nginx无限循环

前端之家收集整理的这篇文章主要介绍了带有try_files和index的nginx无限循环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用Nginx的以下配置,我注意到我得到了一个无限循环的位置处理.根据我对文档的理解,我希望http:// localhost:8080的请求只是根据我的索引指令返回我的index.html文件.相反,请求根据我从X-debug标头中看到的内容,映射到位置/循环,每次/每次附加.我的调试消息的输出是:

X-debug-one:location / /////////// / var / www / content

正如您所看到的,$uri在几次迭代中不断增长.如果索引指令不受尊重,它的重点是什么?我希望在这个过程的某个时刻测试索引?

这是我的配置.我必须在这里遗漏一些明显的东西.请注意,这是从Docker容器内部运行,端口映射到8080.

server {
  server_name _;
  listen 80 default_server;

  root /var/www/content;
  index index.html;

  access_log /dev/stdout;
  error_log /dev/stdout info;

  add_header X-debug-base "base $uri $document_root" always;

  location / {
    add_header X-debug-one "location / $uri $document_root" always;
    try_files $uri $uri/;
  }
}

编辑:

以下是来自Docker(localhost:8080)和docker(localhost:80)内部的curl(curl -sSL -D – “localhost:8080”-o / dev / null)的响应

HTTP/1.1 500 Internal Server Error
Server: Nginx/1.10.3
Date: Sat,06 Jan 2018 22:55:35 GMT
Content-Type: text/html
Content-Length: 193
Connection: close
X-debug-one: location / /////////// /var/www/content

这是日志,捕捉我的卷曲,我认为那里也有Chrome请求.

unyozi_1    | 2018-01-06 22:57:23,869 DEBG 'Nginx' stdout output:
unyozi_1    | 127.0.0.1 - - [06/Jan/2018:22:57:23 +0000] "GET / HTTP/1.1" 500 193 "-" "curl/7.52.1"
unyozi_1    | 
unyozi_1    | 2018-01-06 22:57:37,722 DEBG 'Nginx' stdout output:
unyozi_1    | 2018/01/06 22:57:37 [error] 11#0: *13 rewrite or internal redirection cycle while internally redirecting to "////////////",client: 127.0.0.1,server: _,request: "GET / HTTP/1.1",host: "localhost"
unyozi_1    | 
unyozi_1    | 2018-01-06 22:57:37,722 DEBG 'Nginx' stdout output:
unyozi_1    | 127.0.0.1 - - [06/Jan/2018:22:57:37 +0000] "GET / HTTP/1.1" 500 193 "-" "curl/7.52.1"
unyozi_1    | 
unyozi_1    | 2018-01-06 22:58:43,546 DEBG 'Nginx' stdout output:
unyozi_1    | 2018/01/06 22:58:43 [error] 11#0: *14 rewrite or internal redirection cycle while internally redirecting to "/sockjs-node/info///////////",client: 172.19.0.1,request: "GET /sockjs-node/info?t=1515279507454 HTTP/1.1",host: "localhost:8080",referrer: "http://localhost:8080/"
unyozi_1    | 
unyozi_1    | 2018-01-06 22:58:43,547 DEBG 'Nginx' stdout output:
unyozi_1    | 172.19.0.1 - - [06/Jan/2018:22:58:43 +0000] "GET /sockjs-node/info?t=1515279507454 HTTP/1.1" 500 595 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/63.0.3239.84 Safari/537.36"
unyozi_1    | 

编辑2:/var/www/content/index.html是一个存在的文件.

最佳答案
问题出在这里:

    try_files $uri $uri/;

在try_files中添加一个/到一个参数的末尾会导致它在index.html中使用index指令中指定的路径尝试该URL.

当你加载/时,try_files首先尝试$uri,它不匹配,然后它到达$uri /并尝试/index.html,它重新进入相​​同的位置块.因为index.html不存在,所以它再次进入$uri /,再次尝试/index.html,重新进入位置块,并为您提供重写或内部重定向周期错误.

解决此问题,请在try_files中找不到静态文件时指定合理的默认值.如果您有一个仅限静态的站点,则可能是= 404.如果您要运行Web应用程序,那么它可能是该应用程序的位置.例如:

    try_files $uri $uri/ =404;        # Static site
    try_files $uri $uri/ /index.PHP;  # PHP front controller
    try_files $uri $uri/ @uwsgi;      # pass to a named location
原文链接:https://www.f2er.com/nginx/435100.html

猜你在找的Nginx相关文章