【问题标题】:Why does this Nginx config result in “rewrite or internal redirection cycle”为什么这个 Nginx 配置会导致“重写或内部重定向循环”
【发布时间】:2022-04-05 03:11:25
【问题描述】:

我无法弄清楚为什么会发生此错误:“在内部重定向到“/index.html”时重写或内部重定向循环” 在 nginx.conf 中

user www-data; worker_processes auto; pid /run/nginx.pid; 

events { worker_connections 768; # multi_accept on; } 

 http {

 sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off;

 # server_names_hash_bucket_size 64; # server_name_in_redirect off;

 include /etc/nginx/mime.types; default_type application/octet-stream;

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE #ssl_prefer_server_ciphers on;

 access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;

 gzip on; gzip_disable "msie6"; 

 upstream api { server 127.0.0.1:8080; }

 server { listen 0.0.0.0:80; root /open-ethereum-pool/www/dist; index index.html index.htm;

 server_name localhost;

 location /api { proxy_pass http://api; } 

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

 }

}

【问题讨论】:

  • 文件/open-ethereum-pool/www/dist/index.html是否存在,是否可以被运行nginx的进程读取?

标签: nginx


【解决方案1】:

问题与您的配置有关

server {

    listen 0.0.0.0:80; 
    root /open-ethereum-pool/www/dist; 
    index index.html index.htm;

    server_name localhost;

    location /api {
        proxy_pass http://api;
    }

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

改变

try_files $uri $uri/ /index.html;

try_files $uri $uri/ /index.html =404;

这将确保即使index.html 不存在,您也会得到 404 而不是内部重定向循环

当您浏览/index.html时,目前正在发生的事情如下

变成了

try_files /index.html /index.html/ /index.html;

现在如果/index.html 不存在,那么您的后备选项(try_file 的最后一个参数)是/index.html。这会创建内部重定向循环。所以创建 index.html 文件并使用 try_files 下面的方式来避免此类问题

try_files $uri $uri/ /index.html =404;

【讨论】:

    【解决方案2】:

    大多数时候你会想诉诸一个花哨的 404 而不仅仅是默认的 nginx 错误页面。

    改变

    try_files $uri $uri/ /index.html;
    

    try_files $uri $uri/ /error.html;
    

    显然你必须把这个error.html放在你的根目录下,甚至

    try_files $uri $uri/ /error404/error.html;
    

    【讨论】:

      【解决方案3】:

      在我的情况下,我收到了该错误,因为该根目录中缺少 index.html 文件。 我添加index.html文件后,它开始正常工作了。

      【讨论】:

        猜你喜欢
        • 2013-08-21
        • 1970-01-01
        • 2014-04-25
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        • 2011-05-16
        • 1970-01-01
        • 2011-06-04
        相关资源
        最近更新 更多