为什么不应用这个重写规则(nginx)? (试图设置WordPress的多站点)

我试图用nginx设置Wordpress多站点(子文件夹结构),但是这个重写规则有问题。

下面是Apache的.htaccess,我必须翻译成nginxconfiguration。

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" 

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

我可以忽略明显的东西吗?

谢谢!

首先,我不认为你会在你的重写规则[_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的全部内容,导致基本上没有重写,因为我在第一段中提到了不精确的通配符。