意外的nginx行为

时间:2017-03-21 05:56:25

标签: nginx

我的nginx配置中有以下块:

location ~ /$ {
    index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}

location /cat {
    try_files /index.php?url=$uri =404;
}

我期望这样做:发送到http://www.example.com/cat<whatever>的任何请求都将发送到index.php,并将GET变量url设置为请求uri。根据重写日志而发生的事情是location /cat块永远不会被命中。以下是我请求http://www.example.com/cart/testing/时重写日志的相关摘录:

2017/03/21 00:32:49 [error] 25711#0: *20566477 "/var/www/vhosts/example.com/httpdocs/cat/testing/index.html" is not found (2: No such file or directory), client: <ip redacted>, server: example.com, request: "GET /cat/testing/ HTTP/1.1", host: "www.example.com"

没有与请求相关的其他条目。

据我了解,nginx在涉及正则表达式(即location /cat)的任何位置块之前处理任何前缀位置块(即location ~ /$)。所以我被这种行为困扰了。

1 个答案:

答案 0 :(得分:1)

您的理解不正确。有关详细信息,请参阅this document

此外,您的try_files指令不正确。 /index.php?url=$uri=404应该是语句中的最后一个元素。有关详细信息,请参阅this document

你可能想要:

index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;

location /cat {
    try_files $uri $uri/ /index.php?url=$uri;
}

不确定其他位置块的用途,因为index指令已经处理任何带尾随/的URI。有关详细信息,请参阅this document