nginx 另一个重写或内部重定向循环

tom*_*ums 6 php nginx php-fpm

又一个重定向循环,是的。

看过其他问题,但似乎无法让它发挥作用。

server {
    listen          80;
    server_name     localhost;

    charset         utf-8;

    access_log      /srv/http/localhost/log/access.log;
    error_log       /srv/http/localhost/log/error.log;

    location / {
        root        /srv/http/localhost/www;
        index       index.html index.php;
    }

    # prevent access to hidden files
    location ~ /\. {
        access_log      off;
        log_not_found   off;
        deny            all;
    }

    # do not log assets
    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
        access_log      off;
        log_not_found   off;
        expires         360d;
    }

    # pass the PHP scripts to PHP-FPM socket
    location ~* \.php$ {
        try_files       $uri /index.php;
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
        include         fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我server{}的主要主机的完整块,结果是:

2012/07/29 00:34:52 [错误] 14810#0:*10 重写或内部重定向循环,同时内部重定向到“/index.php”,客户端:xx.xx.xx.xx,服务器:本地主机,请求:“GET /phpinfo.php HTTP/1.1”,主机:“xx.xx”

访问时<anything>.php- index.php、phpinfo.php、i.php 等。

这导致500 Internal Server Error,当我尝试使用 try_files 时,我404不时结束,但主要是重定向循环。

这里有什么问题?

Mic*_*ton 4

你输入try_files错了location。它应该位于location您向上游发送请求到 php-fpm 的位置,而应该位于您的location /.

你也有root放错地方了。应该是在server区块内,而不是在location /区块内。

最后,你需要一个fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_filename;in the location ~* \.php$block。