nginx 上出现奇怪的 404

nginx 上出现奇怪的 404

我有一个位置块,/它将所有虚拟路径传递给 ,index.php如 nginx 缺陷页面中所述。我已经使用这个设置有一段时间了,但最近我注意到,-路径段中的一些 url 在 nginx 中返回 404,这意味着它们永远无法到达 php 脚本。我不确定这是否与-url 中的有关。有些路线可以与之配合,有些则不行。

作品: /profiles/recently-added

返回 404: /explore/puerto-rico /puerto-rico

服务器块:

server {
    listen 80;
    server_name example.com;
    root /usr/share/nginx/hosts/www/;

    location ~ \.(php)$ {
        # commented out next 3 lines to see if that was the problem, but alas it made no change
        # try_files $uri = 404;
        # location ~ \..*/.*\.php$ {return 404;}
        # fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_keep_conn on;
        fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* .(png|gif|jpg|jpeg|ico|css|js)$ {
        include /etc/nginx/mime.types;
        expires 365d;
    }

    location / {
        include /etc/nginx/mime.types;
        index index.php;
        try_files $request_uri $request_uri/ /index.php?$query_string;
    }

}

我很难理解我在这里做错了什么。我以前问过这个问题,但并没有得到很好的回应。nginx 版本来自主线分支,版本1.8.0

答案1

错误的位置正则表达式

问题在于这个位置正则表达式:

location ~* .(png|gif|jpg|jpeg|ico|css|js)$ {
            ^

这里的句号不仅仅意味着一个句号 - 它还意味着任意字符。因此它匹配ricopuerto-rico要匹配实际句点,需要对其进行转义:

location ~* \.(png|gif|jpg|jpeg|ico|css|js)$ {

无需猜测即可识别问题

使用调试日志将消除这些问题的神秘性。应用于问题的配置(为简洁起见省略了一些行):

...
2016/06/08 12:38:58 [debug] 7056#7056: *133 http process request line
2016/06/08 12:38:58 [debug] 7056#7056: *133 http request line: "GET /puerto-rico HTTP/1.1"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http uri: "/puerto-rico"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http args: ""
2016/06/08 12:38:58 [debug] 7056#7056: *133 http exten: ""
2016/06/08 12:38:58 [debug] 7056#7056: *133 posix_memalign: 000000000229E060:4096 @16
2016/06/08 12:38:58 [debug] 7056#7056: *133 http process request header line
2016/06/08 12:38:58 [debug] 7056#7056: *133 http header: "User-Agent: curl/7.26.0"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http header: "Accept: */*"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http header: "Host: example.com"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http header done
2016/06/08 12:38:58 [debug] 7056#7056: *133 event timer del: 7: 1465389598899
2016/06/08 12:38:58 [debug] 7056#7056: *133 generic phase: 0
2016/06/08 12:38:58 [debug] 7056#7056: *133 rewrite phase: 1
2016/06/08 12:38:58 [debug] 7056#7056: *133 test location: "/"
2016/06/08 12:38:58 [debug] 7056#7056: *133 test location: ~ "\.(php)$"
2016/06/08 12:38:58 [debug] 7056#7056: *133 test location: ~ ".(png|gif|jpg|jpeg|ico|css|js)$"
2016/06/08 12:38:58 [debug] 7056#7056: *133 using configuration ".(png|gif|jpg|jpeg|ico|css|js)$"
...
2016/06/08 12:38:58 [debug] 7056#7056: *133 http filename: "/var/www/puerto-rico"
2016/06/08 12:38:58 [debug] 7056#7056: *133 add cleanup: 0000000002301A48
2016/06/08 12:38:58 [error] 7056#7056: *133 open() "/var/www/puerto-rico" failed (2: No such file or directory), client: 127.0.0.1, server: example.com, request: "GET /puerto-rico HTTP/1.1", host: "example.com"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http finalize request: 404, "/puerto-rico?" a:1, c:1
2016/06/08 12:38:58 [debug] 7056#7056: *133 http special response: 404, "/puerto-rico?"
2016/06/08 12:38:58 [debug] 7056#7056: *133 http set discard body
2016/06/08 12:38:58 [debug] 7056#7056: *133 xslt filter header
2016/06/08 12:38:58 [debug] 7056#7056: *133 HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Wed, 08 Jun 2016 12:38:58 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

显示 nginx 如何处理该请求。此行:

使用配置“。(png|gif|jpg|jpeg|ico|css|js)$”

是否有确凿证据表明该请求与您未预料到的位置块相匹配,并且会将焦点集中在请求范围内的一条规则上。

答案2

显然,问题与 nginx 无关。在 hhvm 端查找问题,尤其是 index.php 的存在,它们应该在 URI 路径上工作

index index.php;

也许,您想要重写 URL。请注意,这.htaccess不起作用 - 这是 apache 的功能。对于 nginx:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

相关内容