【问题标题】:Nginx 'rewrite or internal redirection cycle while internally redirecting' resulting in 500Nginx'在内部重定向时重写或内部重定向循环'导致500
【发布时间】:2013-08-21 16:39:25
【问题描述】:

我在 Raspbian wheezy 上的 nginx 1.2.1-2.2 有点问题。我认为它是在我更改了站点可用/默认文件中的索引后开始的。以下是相关文件:

nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    index index.html index.htm index.php;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

sites-available/default

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /home/tom/www;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

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

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /yourls {
    # YOURLS time
    if (!-e $request_filename){
         rewrite ^(.*)$ /yourls-loader.php break;
    }
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

我在尝试访问任何 WordPress 页面时遇到错误,包括在地址栏中键入 local.dyn.kwl.me/wordpress/ 或将 index.php 或 wp-admin 添加到该 URL。在浏览器中显示为 500,但 nginx 日志显示如下:

2013/08/19 17:55:49 [error] 31600#0: *58 rewrite or internal redirection cycle while internally redirecting to "/wordpress/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html", client: 109.149.13.53, server: localhost, request: "GET /wordpress HTTP/1.1", host: "local.dyn.kwl.me"
2013/08/19 17:55:49 [error] 31600#0: *59 rewrite or internal redirection cycle while internally redirecting to "/favicon.ico/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html", client: 109.149.13.53, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "local.dyn.kwl.me"

我想我的站点默认文件中的 index 或 try_files 有问题。谁能看到什么?

【问题讨论】:

  • 位置 /yourls 指的是什么?您在使用 domain.com/yourls 访问您的 Wordpress 网站吗?
  • 为什么在 location 块之外还有 try_files?
  • 我在一个magento网站上也遇到了这个错误
  • @JoeBuckle 这无关紧要。 WordPress 存储在 /wordpress/ 中。
  • @Danack 我的研究表明我应该尝试添加它来修复我遇到的错误。

标签: wordpress nginx


【解决方案1】:

我经常在 Nginx 上运行 WordPress 网站。如果您可以使用它们,这是位置指令的部分成功配置。 Nginx 版本为1.0.15。您可能想要更改 fastcgi_pass 参数以运行您的 fastcgi_wrapper 正在运行的任何内容。也许fastcgi_pass 127.0.0.1:9000

    location @php { 
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9090;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }  

    if (!-e $request_filename){
            rewrite ^(.*)$ /index.php?q=$1 last;
            break;
    }

所以,在您的网站配置中

sites-available/default

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /home/tom/www;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;


    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    ## Original Config
    # location ~ \.php$ {
    #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        # fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #    fastcgi_index index.php;
    #    include fastcgi_params;
    # }

    ## New Config
    location @php { ## Depending on your Nginx version, you might need to change this to location ~ \.php$ {
      try_files $uri =404;
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9090; ## Maybe change to 9000 or use socket fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
    }

    ## Wordpress Rewrite
    if (!-e $request_filename){
            rewrite ^(.*)$ /index.php?q=$1 last;
            break;
    }

}

编辑

根据您对我的最后评论。如果您的 DNS 没有解析到子目录 /wordpress/ 那么您必须更改您的重写规则。

    ## Wordpress Rewrite
    if (!-e $request_filename){
            rewrite ^(.*)$ /wordpress/index.php?q=$1 last;
            break;
    }

【讨论】:

  • 现在 WordPress(我的其他目录仍然没有任何问题)想要下载一个名为“下载”的文件,其中包含 index.php 的内容。我很困惑:/我正在使用您发布的配置,location ~ \.php$ 并使用套接字而不是本地 IP 和 php5-fpm 端口。
  • 它正在尝试下载文件,因为您没有将请求传递给 PHP,因此我认为您的 fastcgi_pass 配置可能是错误的。尝试使用这个 fastcgi_pass fastcgi_pass 如果它在unix:/var/run/php5-fpm.sock; 之前工作过@
  • 好吧,/yourls-next(php 脚本)和 /wordpress/wp-admin 都可以正常工作。只是 /wordpress 会立即生成下载文件。 /wordpress/index.php 需要一段时间,但仍然要求我下载该文件。我试过禁用 WordPress 重写规则,但这对任何事情都没有影响。我可能会尝试重新安装 WordPress。配置中没有专门针对 WordPress 前端的内容。
  • 我认为这与 WordPress 安装无关。您使用什么作为 FastCGI 包装器? php-fpm 我猜?
  • 是的,来自 debian 存储库的 php5-fpm 包。我会尝试重新安装 WordPress。我认为这可能与启用维护模式有关,但我现在禁用了它,没有任何改变。
【解决方案2】:

你尝试过这样的事情吗? (可能特定于 Magento)

server {
        listen 80 default;
        ssl off;
        root /var/www/magento;
        server_name mydomain.com.au;
        location / {
            index index.html index.php;
            try_files $uri $uri/ @handler;
            expires 30d;
        }
        location ^~ /app/ { deny all; }
        location ^~ /includes/ { deny all; }
        location ^~ /lib/ { deny all; }
        location ^~ /media/downloadable/ { deny all; }
        location ^~ /pkginfo/ { deny all; }
        location ^~ /report/config.xml { deny all; }
        location ^~ /var/ { deny all; }
        location /var/export/ { ## Allow admins only to view export folder
            auth_basic "Restricted"; ## Message shown in login window
            auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
            autoindex on;
        }
        location /. { ## Disable .htaccess and other hidden files
            return 404;
        }
        location @handler { ## Magento uses a common front handler
            rewrite / /index.php;
        }
        location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
            rewrite ^(.*.php)/ $1 last;
        }
        location ~ .php$ { ## Execute PHP scripts
            if (!-e $request_filename) { rewrite / /index.php last; }
            expires off; ## Do not cache dynamic content
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param HTTPS $fastcgi_https;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param MAGE_RUN_CODE default;
            fastcgi_param MAGE_RUN_TYPE store;
            include fastcgi_params;
        }           
    }

server {
        listen 443;
        ssl on;
        ssl_certificate /etc/ssl/certs/mydomain_com_au.crt;
        ssl_certificate_key /etc/ssl/private/mydomain_com_au.key;
        ssl_client_certificate /etc/ssl/crt/mydomain_com_au.crt;
        access_log /var/log/nginx/mydomain.ssl.access.log;
        error_log /var/log/nginx/mydomain.ssl.error.log;
        server_name mydomain.com.au;
        root /var/www/magento;
        location / {
            index index.html index.php;
            try_files $uri $uri/ @handler;
            expires 30d;
        }
        location ^~ /app/ { deny all; }
        location ^~ /includes/ { deny all; }
        location ^~ /lib/ { deny all; }
        location ^~ /media/downloadable/ { deny all; }
        location ^~ /pkginfo/ { deny all; }
        location ^~ /report/config.xml { deny all; }
        location ^~ /var/ { deny all; }
        location /var/export/ { ## Allow admins only to view export folder
            auth_basic "Restricted"; ## Message shown in login window
            auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
            autoindex on;
        }
        location /. { ## Disable .htaccess and other hidden files
            return 404;
        }
        location @handler { ## Magento uses a common front handler
            rewrite / /index.php;
        }
        location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
            rewrite ^(.*.php)/ $1 last;
        }
        location ~ .php$ { ## Execute PHP scripts
            if (!-e $request_filename) { rewrite / /index.php last; }
            expires off; ## Do not cache dynamic content
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param HTTPS $fastcgi_https;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param MAGE_RUN_CODE default;
            fastcgi_param MAGE_RUN_TYPE store;
            include fastcgi_params;
        }            
    }

HTH

【讨论】:

  • 嗯,这行得通,但只有一点点。现在我收到了2013/08/20 15:04:42 [error] 591#0: *6 rewrite or internal redirection cycle while internally redirecting to "/favicon.ico", client: 109.149.13.53, server: local.dyn.kwl.me, request: "GET /favicon.ico HTTP/1.1", host: "local.dyn.kwl.me",所以没有所有索引内容,只有 favicon.ico 错误。
  • 当我遇到同样的问题时,我发现我在 https 根文件夹中输入了错误的 1 个字符。检查是否有任何拼写错误。
【解决方案3】:

试试这个

location / {
...   
try_files $uri $uri/ /index.php?q=$uri&$args;
}

【讨论】:

  • 最好简要解释一下为什么这会对提问者有所帮助。
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 2014-04-25
  • 1970-01-01
  • 2017-04-05
  • 2011-05-16
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多