【问题标题】:Nginx rewrite or internal redirection cycle while internally redirecting to "/index.html"Nginx 重写或内部重定向循环,同时内部重定向到“/index.html”
【发布时间】:2016-11-16 04:35:30
【问题描述】:

我在 nginx 上使用 PHP 框架实现 web 服务器,没有任何 index.html,网页工作正常,但一些脚本不起作用,它说“500 内部服务器错误”

这是服务器日志

2016/11/16 11:08:38 [错误] 2551#0: *738 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /kelontong/getKelontong HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:09:20 [错误] 2551#0: *746 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /kelontong/getKelontong HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:14:47 [错误] 5500#0: *4 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /department/ HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:14:48 [错误] 5500#0: *6 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /department/getdepartment HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:18:56 [错误] 5518#0:*4 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /department/getdepartment HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:18:56 [错误] 5520#0:*8 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /department/getdepartment HTTP/1.1”,主机:“192.168.70.86”

2016/11/16 11:21:35 [错误] 5534#0: *3 重写或内部重定向周期,同时内部重定向到“/index.html”,客户端:27.131.251.6,服务器:www.foreverstore。 id,请求:“GET /department/getdepartment HTTP/1.1”,主机:“192.168.70.86”

这是我的 nginx 主机配置文件

server {
 listen 443 ssl http2;

 root /bwi/foreverstore.id;
 index index.html index.htm index.php;


 server_name www.foreverstore.id ;
 ssl_certificate /etc/nginx/ssl/foreverstore.crt;
 ssl_certificate_key /etc/nginx/ssl/foreverstore.key;

 location / {

    try_files $uri $uri/ /index.html;
    #  try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
 }

 location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
 }

 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
    root /usr/share/nginx/www;
 }

 location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_param MAGE_RUN_CODE default;
    fastcgi_param MAGE_RUN_TYPE store;
  }
}

如果你们知道如何解决这个问题,我将不胜感激 干杯。

【问题讨论】:

  • 我认为原因是因为一旦$uri$uri/ 失败,它会重定向到/index.html,然后再次通过try_files 并在$uri$uri/ 失败然后重定向到/index.html。你明白了......尝试将/index.html更改为/index.php,这样如果在$uri$uri/失败,它将默认为您的\.php$
  • 我也尝试过这种方法,但它仍然没有解决我的问题,它只是重定向到我的主页,我想要看到我的引擎像我的其他服务器(apache2)一样工作,比如@987654321 @(它在 apache2 上显示我的引擎)但在 nginx 上,它只是错误/重定向到主页
  • 如果它重定向到您的主页,那么您最初的重定向循环问题就解决了,因此这是您的问题的解决方案。除此之外,您还需要在后端进行配置,或者提出一个新问题,其中包含更多详细信息,描述您想要实现的目标、您尝试过的内容、您期望发生的事情以及实际发生的事情。
  • 你是怎么解决这个问题的?

标签: php nginx web


【解决方案1】:

我认为 try_files 行应该是这样的:

try_files $uri $uri/ index.html;

【讨论】:

  • 我已经在使用该脚本,但仍然无法正常工作。我没有使用任何 html 文件,只是 php framewrok
【解决方案2】:

由于您没有使用您所说的任何 html 文件,因此请更改您的根位置块:

location / {
 try_files $uri $uri/ =404;
}

你必须在你的 php 块中包含一个 try 文件,所以当它失败时它会去那里而不是寻求重定向:

location ~ \.php$ {
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
  fastcgi_intercept_errors on;
  fastcgi_param MAGE_RUN_CODE default;
  fastcgi_param MAGE_RUN_TYPE store;
  try_files $uri $uri/ =404;
}

由于您声明:error_page 404 /404.html;,您还可以添加 404 位置:

    location = /404.html {
            root /usr/share/nginx/html/;
            internal;
    }

这应该可以阻止那些丑陋的重定向循环。

【讨论】:

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