【问题标题】:Nginx - rewrite or internal redirection cycleNginx - 重写或内部重定向循环
【发布时间】:2014-04-25 12:14:36
【问题描述】:

我使用 nginx,配置非常简单,它适用于 /usr/share/nginx/www/ 子目录中的所有 php 站点。

但现在我想在具有重写规则的子目录中创建一个新项目。 所以我决定在默认情况下为此创建一个.conf。 但是重写不起作用导致错误“重写或内部重定向循环”。

默认

server {
    listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

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

    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 /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    location ~ \.php$ {
            try_files $uri =404;
            # 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;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_intercept_errors on;
    }

    location ~ /\. {
            access_log off;
            log_not_found off;
            deny all;
    }
}

newproject.conf

# nginx configuration
autoindex off;

location /newproject/ {
    if (!-e $request_filename) {
            rewrite ^/newproject/(.+)$ /newproject/index.php?url=$1 break;
    }
}

【问题讨论】:

    标签: php nginx rewrite


    【解决方案1】:
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
    

    这部分关于回退到 404 是错误的。可能你在这里错过了=404,导致了重定向循环(它一次又一次地重定向到/index.html)。

    请注意the documentation:

    如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。

    【讨论】:

    • 即使使用 try_files $uri $uri/ =404;我在日志中得到同样的错误。
    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2013-08-21
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多