Nginx 1.2.2:如何使try_files工作?


11

我最近将nginx更新到1.2.2版,以下条目似乎已损坏;大概是版本之间的语法变化了吗?

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri /index.html;
        }

当我浏览到http://www.mysite.com/a-non-existent-url时,我被重定向到“ 500 Internal Server Error”错误页面。进行以下日志输入;

2012/08/13 09:20:29 [error] 18457#0: *60 rewrite or internal redirection cycle 
while internally redirecting to "/index.html", client: 10.0.14.1, server: 
mysite.com, request: "GET /a-non-existent-url HTTP/1.1", host: "www.mysite.com"

这曾经按预期工作,但我找不到该版本的正确语法。现在应该怎么样?

更新 根据要求的完整配置;

server {

    root /usr/share/nginx/mysite.com/public_html;
    index index.php index.html index.htm;
    server_name mysite.com www.mysite.com;
    access_log  /usr/share/nginx/mysite.com/logs/access_log;
    error_log   /usr/share/nginx/mysite.com/logs/error_log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
    }

    #Added for awstats
    location ^~ /awstats-icon {
            alias /usr/share/awstats/icon/;
            access_log off;
    }

    #Added for awstats
    location ^~ /awstatscss {
            alias /usr/share/doc/awstats/examples/css/;
            access_log off;
    }

    #Added for awstats
    location ^~ /awstatsclasses {
            alias /usr/share/doc/awstats/examples/classes/;                                 
            access_log off;
    }

    #Added for awstats
    # Configure /cgi-bin/scripts to go through php-fastcgi
    location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
            gzip off;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index cgi-bin.php;
            fastcgi_param SCRIPT_FILENAME    /etc/nginx/cgi-bin.php;
            fastcgi_param SCRIPT_NAME        /cgi-bin/cgi-bin.php;
            fastcgi_param X_SCRIPT_FILENAME  /usr/lib$fastcgi_script_name;
            fastcgi_param X_SCRIPT_NAME      $fastcgi_script_name;
            fastcgi_param QUERY_STRING       $query_string;
            fastcgi_param REQUEST_METHOD     $request_method;
            fastcgi_param CONTENT_TYPE       $content_type;
            fastcgi_param CONTENT_LENGTH     $content_length;
            fastcgi_param GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param SERVER_SOFTWARE    nginx;
            fastcgi_param REQUEST_URI        $request_uri;
            fastcgi_param DOCUMENT_URI       $document_uri;
            fastcgi_param DOCUMENT_ROOT      $document_root;
            fastcgi_param SERVER_PROTOCOL    $server_protocol;
            fastcgi_param REMOTE_ADDR        $remote_addr;
            fastcgi_param REMOTE_PORT        $remote_port;
            fastcgi_param SERVER_ADDR        $server_addr;
            fastcgi_param SERVER_PORT        $server_port;
            fastcgi_param SERVER_NAME        $server_name;
            fastcgi_param REMOTE_USER        $remote_user;
    }

    #Make sure all PHP is process by php-fpm
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    #rTorrent/wTorrent needs this
    #To loop back to the xml rpc service
        location /RPC2 {
                scgi_pass   127.0.0.1:5000;
                include     scgi_params;
                scgi_param    SCRIPT_NAME  /RPC2;
        }

}

第二次更新

已在此处发布了调试日志(http://pastebin.com/raw.php?i=PtLwvQhW)。它的时间很长,所以我这样做是为了避免垃圾邮件。


显示您的完整配置。
量子

1
追加debugerror_log行尾,重试,然后在此处发布错误日志。
量子

我在帖子中添加了一个指向粘贴容器的链接;pastebin.com/raw.php?i=PtLwvQhW
jwbensley,2012年

ls -l /usr/share/nginx/mysite.com/public_html/index.html
量子

Answers:


12

将try_files行修改为:

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

参考:https : //nginx.org/en/docs/http/ngx_http_core_module.html#try_files


1
好吧,我这方面有几点愚蠢的观点。首先index.html不存在,应该是index.php,它原来是,我有一个旧的配置副本,显示了这一点(只是运行diff就向我指出了这一点)。不确定如何改变?!其次,指向/index.php实际上导致我的浏览器下载原始的php代码,这有点危险,但是指向/的效果很好。所以现在该行非常try_files $uri $uri/ / =404;感谢:D
jwbensley

2
非常感谢。如果不匹配/index.html,我将不知道try_files导致内部重定向到列表中的最后一个项目而从我的产品中删除了。我离开这条线,因为try_files $uri $uri/ =404;这对我来说很有效。
Drew Noakes
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.