nginx 重写不起作用,但不知道原因

nginx 重写不起作用,但不知道原因

我肯定错过了一些非常愚蠢的东西,但我看不出来。我有以下 URL 格式:

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

...还有这条规则:

rewrite ^/french/.*/cats_([0-9]+)_([A-Z])_([A-Z])\.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"

更新:好的,我已经找到了有问题的规则,但我正在尝试找出如何解决它。

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

我对域名的主要配置是:

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-重定向.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;
}

我这样做的原因是,我们对一个顶级类别有超过 30,000 次重写,而另一个顶级类别中有 20,000 条规则 - 所以我想将其拆分以节省负载。

我发现解决这个问题的唯一方法是:

location /french {
    rewrite ^/french/.*/cats_([0-9]+)_([A-Z])_([A-Z])\.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;
}

我猜是因为一个块阻止了另一个块的运行,所以它们需要在同一个块中?

答案1

很有可能,nginx 没有看到重写。正则表达式在我看来是正确的,所以我会查看它是否位于 nginx 配置中的正确位置,以及您的 URI 是否未被其他位置或其他位置拾取。

编辑: 根据您对 301 的多个包含的使用情况,我的建议是将所有这些都移至map。它也可能使其更快一些。

例如:

映射$uri$is_rewrite{
    默认无重定向;
    “〜/ regex” “ /小猫”;
    #...
};
服务器 {
    #...
    如果 ($is_rewrite != no_redirect) {
        重写 .? $is_redirect 永久;
    }
    #...
}

最简单的方法是将其弹出到您要从该/french位置包含的文件中。我只是想提一下地图,因为它可能会让您的生活更轻松 :)

相关内容