Nginx 中的多站点 WordPress 重写规则
Posted
技术标签:
【中文标题】Nginx 中的多站点 WordPress 重写规则【英文标题】:Multi Site WordPress rewrite rules in Nginx 【发布时间】:2011-03-30 07:23:44 【问题描述】:我正在尝试使用 WordPress 和 nginx 运行多域博客安装。最后一步是在.htaccess
(仅限apache)中为网络服务器配置一些重写规则。如何将其转换为 Nginx 重写规则?
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %REQUEST_FILENAME -f [OR]
RewriteCond %REQUEST_FILENAME -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
【问题讨论】:
【参考方案1】:nginx rewrite rules example for Wordpress 3:
server
server_name *.example.com;
listen 80;
#on server block
##necessary if using a multi-site plugin
server_name_in_redirect off;
##necessary if running Nginx behind a reverse-proxy
port_in_redirect off;
access_log /var/log/nginx/example-com-access.log;
location /
root /var/www/example.com/wordpress;
index index.html index.htm index.php;
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename)
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
root /var/www/example.com/wordpress;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
location ~ wp\-.*\.php|wp\-admin|\.php$
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/wordpress$fastcgi_script_name;
【讨论】:
【参考方案2】:重写后上面缺少一个空格 ^./files/(.(html|jpg|.......你需要在 $ 之后和 /wp-includes 之前有一个空格/ms-files.php。干杯!
【讨论】:
【参考方案3】:这个 WordPress 的 nginx 重写规则完美解决了我的问题,包括将 ex.com/wp-admin 更改为 ex.com/wp-admin/。
但是我发现里面有一个bug,浪费了我几个小时,如下:
如果静态 url 包括 .html
就像 /%category%/%post_id%.html
一样,你必须插入:
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
root /var/www/example.com/wordpress;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
改成:
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
root /var/www/example.com/wordpress;
rewrite ^/.*(/wp-.*/.*\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ /wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
或者,你会在内容页面中出现“404 page not found”错误!
【讨论】:
【参考方案4】:代替:
location ~ wp\-.*\.php|wp\-admin|\.php$
改成:
location ~ .php$
让它指挥
blog.com/wp-admin
到
blog.com/wp-admin/
【讨论】:
以上是关于Nginx 中的多站点 WordPress 重写规则的主要内容,如果未能解决你的问题,请参考以下文章
Nginx位置正则表达式来处理/ sub /目录中多个WordPress站点的永久链接
从 apache 到 nginx:wordpress 重写规则