为什么不应用此重写规则(nginx)?(尝试设置 Wordpress 多站点)

为什么不应用此重写规则(nginx)?(尝试设置 Wordpress 多站点)

我正在尝试使用 nginx 设置 Wordpress 多站点(子文件夹结构),但这个重写规则出现了问题。

下面是 Apache 的 .htaccess,我必须将其转化为 nginx 配置。

RewriteEngine On
RewriteBase /blogs/
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

以下是我的想法:

server {
    listen 80;
    server_name example.com;
    server_name_in_redirect off;
    expires 1d;

    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;

    root /srv/www/example.com/public;
    index index.html;

    try_files $uri $uri/ /index.html;

    # rewriting uploaded files
    rewrite ^/blogs/(.+/)?files/(.+) /blogs/wp-includes/ms-files.php?file=$2 last;

    # add a trailing slash to /wp-admin
    rewrite ^/blogs/(.+/)?wp-admin$ /blogs/$1wp-admin/ permanent;

    if (!-e $request_filename) {
        rewrite ^/blogs/(.+/)?(wp-(content|admin|includes).*) /blogs/$2 last;
        rewrite ^/blogs/(.+/)?(.*\.php)$ /blogs/$2 last;
    }

    location /blogs/ {
        index index.php;
        #try_files $uri $uri/ /blogs/index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public$fastcgi_script_name;
    }

    # static assets
    location ~* ^.+\.(manifest)$ {
        access_log /srv/www/example.com/logs/static.log; 
    }

    location ~* ^.+\.(ico|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        # only set expires max IFF the file is a static file and exists
        if (-f $request_filename) {
            expires max;
            access_log /srv/www/example.com/logs/static.log; 
        }
    }
}

在上面的代码中,我相信rewrite ^/blogs/(.+/)?(.*\.php)$ /blogs/$2 last;没有效果,因为当我查看access_log文件时,我看到以下行:

2010/09/15 01:14:55 [error] 10166#0: *8 "/srv/www/example.com/public/blogs/test/index.php" is not found (2: No such file or directory), request: "GET /blogs/test/ HTTP/1.1"

(这里,“test”是使用多站点功能创建的第二个博客)我期望的是 /blogs/test/index.php 被重写为 /blogs/index.php,但它似乎并没有这样做......

我是否忽略了一些显而易见的事情?

谢谢!

答案1

首先,我不认为你会改变[_0-9a-zA-Z-]+重写.+规则,但我也不认为这是困扰你的事情。

该规则rewrite ^/blogs/(.+/)?(.*\.php)$ /blogs/$2 last;不匹配,/blogs/test/index.php因为请求实际上是针对/blogs/test/index.php直到四行之后,当 Nginx 查找该目录的索引文件时,才会添加 。如果我没记错的话,/blogs/test/不会被重写,但/blogs/test/index.php(如浏览器位置栏中所示)会被重写。

话虽如此,我认为即使在那时,您仍可能会发现(.+/)?请求的一部分与任何内容都不匹配,而部分(.*\.php)与全部匹配,test/index.php导致基本上根本没有重写,因为正如我在第一段中提到的那样,您的通配符不精确。

相关内容