Nginx 重写规则,有些有效,有些无效

Nginx 重写规则,有些有效,有些无效

以下是两个重写规则:

这个有效

rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last;

这个没有

rewrite ^/announcements/([0-9]+)/[a-z0-9_-]+.html$ /./announcements.php?id=$1 last;

据我所知,这两者没有区别。公告需要重写的 URL 是:

/announcements/2/New-Site-Design.html

并应重写为:

/announcements.php?id=2

我真的不明白公告与知识库相比有什么用处。任何提示都将不胜感激。

=== 添加配置文件 - 我的域名

#HTTP
server {

    listen 80;
    server_name portal2.website.co.uk website.co.uk www.website.co.uk;

    access_log /var/log/nginx/website.access_log;
    error_log /var/log/nginx/website.error_log;

    root /var/www/website.co.uk/www;
    index index.php index.htm index.html;

    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/website.co.uk/www$fastcgi_script_name;
        include fastcgi_params;
    }

    rewrite ^/announcements$ /./announcements.php last;
    rewrite ^/announcements/([0-9]+)/[a-z0-9_-]+.html$ /./announcements.php?id=$1 last;

    rewrite ^/knowledgebase$ /./knowledgebase.php last;
    rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last;
    rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last;

    rewrite ^/downloads$ /./downloads.php last;
    rewrite ^/downloads/([0-9]+)/([^/]*)$ /./downloads.php?action=displaycat&catid=$1 last;

}

在上面的配置文件中,我用“网站”替换了对我的域名的引用

=== 编辑

这是错误日志行

2012/06/28 11:40:04 [error] 29095#0: *7 open() "/var/www/website.co.uk/www/announcements/6/Moving-to-a-new-server.html" failed (2: No such file or directory), client: 92.232.232.25, server: portal2.website.co.uk, request: "GET /announcements/6/Moving-to-a-new-server.html HTTP/1.1", host: "portal2.website.co.uk"

因此,重写规则不适用于 /announcements/id/page-title.html

答案1

Moving-to-a-new-server包含大写字母M,最后我记得,重写规则区分大小写。

我正在猜测,但是当你测试它时,knowledgebase你只提供了小写的后半部分。

如果是的话,试试这个:

rewrite ^/announcements/([0-9]+)/[a-zA-Z0-9_-]+.html$ /./announcements.php?id=$1 last;

并对 进行类似的更改knowledgebase。您可以直接使用.*?而不是第二部分的特定表达式,因为您实际上不会在任何地方使用它(尽管它会匹配更多符号)。

答案2

Jay 对大写的回答是正确的M。写入在知识库上起作用的原因是因为这是匹配的重写:

rewrite ^/knowledgebase/([0-9]+)/([^/]*)$ /./knowledgebase.php?action=displaycat&catid=$1 last;

而不是这个:

rewrite ^/knowledgebase/([0-9]+)/[a-z0-9_-]+.html$ /./knowledgebase.php?action=displayarticle&id=$1 last;

[^/]是负匹配,即匹配所有内容不是匹配/,这解释了为什么你的请求/knowledgebase/1/General-Terms-of-Service.html被重写,但没有/announcements/2/New-Site-Design.html

相关内容