WordPress伪静态配置nginx和apache

wordpress站点从虚拟空间迁移到VPS后,如果站点采用了伪静态页面,那么nginx或apache是需要自己动手配置相关的重写规则的。由于测试了nginx服务器和apache服务器的性能在小内存(512MB)VPS上的差异,尝试了两种配置最后还是选择了nignx+php-fpm的架构。下面将配置贴出来:

1、apache配置先开启网站根目录的重启,然后再在虚拟主机配置相关重写规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
DocumentRoot "/opt/www"

<span style="color: #ff0000;">#将AllowOverride由None改成 All</span>
<Directory />
Options FollowSymLinks
AllowOverride <span style="color: #ff0000;">All</span>
</Directory>
#虚拟主机配置部分
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin xxxxxxxxxxxx@xxx.com
DocumentRoot /opt/www/myblog
ServerName www.xiaomastack.com
ServerName blog.xiaomastack.com
ServerName xiaomastack.com
ErrorLog /opt/www/log/httpd/www.xiaomastack.com-error_log
CustomLog /opt/www/log/httpd/www.xiaomastack.com-access_log common
#添加额外的重写规则
<span style="color: #ff0000;"><IfModule mod_rewrite.c></span>
<span style="color: #ff0000;"> RewriteRule ^index\.php$ - [L]</span>
<span style="color: #ff0000;"> RewriteCond %{REQUEST_FILENAME} !-f</span>
<span style="color: #ff0000;"> RewriteCond %{REQUEST_FILENAME} !-d</span>
<span style="color: #ff0000;"> RewriteRule . /index.php [L]</span>
<span style="color: #ff0000;"> </IfModule></span>
</VirtualHost>

2、nginx的重写配置如下,发现配置好重写后,post数据时还会发生405错误(这都是采用伪静态的后遗症,不过也好解决),配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 last;
}
if (-d $request_filename) {
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
#防止post数据时出现405错误
error_page 405 =200 @405;
location @405 {
root /opt/www/myblog;
}
----------------本文结束 感谢阅读----------------