nginx重写不工作,但不知道为什么

我一定在这里错过了一些非常愚蠢的东西,但我看不见它。 我有以下url格式:

https://www.example.com/french/xx/cats_59_H_L.html 

…和这个规则:

 rewrite ^/french/.*/cats_([0-9]+)_([AZ])_([AZ])\.html$ /cgi-bin/hotels/related_cats.cgi?ID=$1&start=$2&end=$3&type=chambres&t=french last; 

但是,它给出了一个404:

 2017/08/30 12:52:59 [error] 8041#8041: *1147655 open() "/home/user/web/example.com/public_html/french/xx/cats_59_H_L.html" failed (2: No such file or directory), client: 81.174.134.133, server: example.com, request: "GET /french/xx/cats_59_H_L.html HTTP/2.0", host: "www.example.com" 

更新:好的,以及我已经find了违规的规则,但我正在努力解决如何解决它。

 location /french { include /home/fatpeter/conf/web/chambres.com.extra/other-cats-french.conf; } 

我的域的主要configuration是:

 server { listen 111.74.193.98:443 http2; listen [::]:443 http2; server_name example.com www.example.com; root /home/user/web/example.com/public_html; index index.php index.html index.htm; access_log /var/log/nginx/domains/example.com.log combined; access_log /var/log/nginx/domains/example.com.bytes bytes; error_log /var/log/nginx/domains/example.com.error.log error; ssl on; ssl_certificate /home/user/conf/web/ssl.example.com.pem; ssl_certificate_key /home/user/conf/web/ssl.example.com.key; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } if ($host = "example.com") { return 301 https://www.example.com$request_uri; } location / { include /home/user/conf/web/nginx.example.com.rules.conf*; ssi on; location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ { expires max; } location ~ \.cgi$ { gzip off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8181; } location ~ [^/]\.php(/|$) { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9002; fastcgi_index index.php; include /etc/nginx/fastcgi_params; } } error_page 403 /error/404.html; error_page 404 /error/404.html; error_page 500 502 503 504 /error/50x.html; location /error/ { alias /home/user/web/example.com/document_errors/; } location ~* "/\.(htaccess|htpasswd)$" { deny all; return 404; } location /vstats/ { alias /home/user/web/example.com/stats/; include /home/user/web/example.com/stats/auth.conf*; } include /etc/nginx/conf.d/phpmyadmin.inc*; include /etc/nginx/conf.d/phppgadmin.inc*; include /etc/nginx/conf.d/webmail.inc*; include /home/user/conf/web/nginx.example.com.conf*; } 

正如你所看到的,我们在这里有这个:

 include /home/user/conf/web/nginx.example.com.rules.conf*; 

该文件包含:

 include /home/user/conf/web/chambres.com.extra/301-redirects.conf; include /home/user/conf/web/chambres.com.extra/glinks-rules.conf; 

…然后在301-redirects.conf里面,我们有:

 location /french/Gites { include /home/user/conf/web/chambres.com.extra/gites-cats-french.conf; } location /Gites { include /home/user/conf/web/chambres.com.extra/gites-cats-english.conf; } location /french/Chambres_D_Hotes { include /home/user/conf/web/chambres.com.extra/chambres-cats-french.conf; } location /Chambres_D_Hotes { include /home/user/conf/web/chambres.com.extra/chambres-cats-english.conf; } location /french { include /home/user/conf/web/chambres.com.extra/other-cats-french.conf; } 

我之所以这么做,是因为我们有一个顶级类别的重写超过了3万个,另外一个是20000个规则 – 所以我想分解它以节省负担。

我周围发现的唯一方法是做:

 location /french { rewrite ^/french/.*/cats_([0-9]+)_([AZ])_([AZ])\.html$ /cgi-bin/hotels/related_cats.cgi?ID=$1&start=$2&end=$3&type=chambres&t=french last; include /home/fatpeter/conf/web/chambres.com.extra/other-cats-french.conf; } 

我想这是因为一个街区阻止了另一个街区的运行,所以他们需要在同一个街区?

机会是,nginx没有看到重写。 正则expression式看起来对我来说是正确的,所以我会看看它是否在你的nginxconfiguration中的正确位置,并且你的URI没有被其他位置或东西拾起。

编辑:根据你的使用情况的多个包括301的,我的build议是将所有这些移动到map 。 它可能会使它更快一点。

例如:

 map $ uri $ is_rewrite {
    默认no_redirect;
     “〜/ regex”“/ kitten”;
     #...
 };
服务器{
     #...
     if($ is_rewrite!= no_redirect){
        重写。  $ is_redirect永久;
     }
     #...
 }

最简单的方法就是将其从/french位置popup到包含的文件中。 只是以为我会提到地图,因为它可能会让你的生活更轻松:)