顛末網(wǎng)上查閱和測試,發(fā)明Nginx的Rewrite法則和Apache的Rewite法則不同不是很大,險(xiǎn)些可以直接利用。好比在Apache中這樣寫法則
rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;
而在Nginx中寫成這樣寫是無法啟動(dòng)的,辦理的步伐是加上兩個(gè)雙引號(hào):
rewrite "^/([0-9]{5}).html$" /viewthread.php?tid=$1 last;
同時(shí)將RewriteRule為Rewrite,,根基就實(shí)現(xiàn)了Nginx的Rewrite法則到Apache的Rewite法則的轉(zhuǎn)換。
Rewrite的Flags
last - 根基上都用這個(gè)Flag。
break - 中止Rewirte,不在繼承匹配
redirect - 返回姑且重定向的HTTP狀態(tài)302
permanent - 返回永久重定向的HTTP狀態(tài)301
WordPress的Rewrite
其實(shí)在Nginx下設(shè)置WordPress的Rewrite照舊較量簡樸的,在location /{..................}內(nèi)里插手
if (!-f $request_filename){
rewrite (.*) /index.php;
}
下面是一個(gè)完整的vhost的設(shè)置文件
server {
listen 80;
server_name server110.com www.server110.com;
location / {
index index.html index.htm index.php;
root /www/wwwroot/server110.com;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8787;
fastcgi_param SCRIPT_FILENAME /www/wwwroot/server110.com$fastcgi_script_name;
}
location /server110-status {
stub_status on;
access_log off;
}
}
Discuz!的Rewrite
下面的Rewrite中百分號(hào)前面多了個(gè)轉(zhuǎn)移字符“”,這在Apache中是需要的,而在Nginx中則是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
正確的應(yīng)該是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
這個(gè)錯(cuò)誤在根基上今朝所有利用Nginx作為處事器,而且開啟了Rewrite的網(wǎng)站上存在。包羅Discuz!官方,今朝已經(jīng)給cnteacher反饋了。
Nginx實(shí)例代碼
server {
listen 80;
server_name www.server110.com server110.com;
location / {
index index.html index.htm index.php;
root /www/www.server110.com;
rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8694;
fastcgi_param SCRIPT_FILENAME /www/www.server110.com$fastcgi_script_name;
}
location /www.server110.com-status {
stub_status on;
access_log off;
}
}