西西一個(gè)服務(wù)器本來(lái)是windows 系統(tǒng),采用ISAPI_Rewrite來(lái)進(jìn)行Url重寫,其中有一個(gè)規(guī)則是
RewriteRule ^/(.{6})(\d{3})(.+)/php/ http://www.ksks6.com/qq$2.apk [NC,L,NU]
中間用到了{(lán)6}指前面的字符得復(fù)6次,然后平移動(dòng)linux系統(tǒng)下面,采用nginx 的Rewrite來(lái)重寫url 結(jié)果加載nginx的時(shí)候提示報(bào)錯(cuò)
rewrite ^/(.{6})(\d{3})(.+)/php/ http://www.ksks6.com/qq$2.apk break;
找了好久的資源終于在 ken 的文章中找到了解決的辦法
將規(guī)則前半部分用英文雙引號(hào)包起來(lái)即可正常應(yīng)用
如: rewrite "^/(.{6})(\d{3})(.+)/php/" http://www.ksks6.com/qq$2.apk break;
就可以正常解析了
順便就把nginx支持正則式的資源全面整理一下,方便下次使用;
nginx rewrite 偽靜態(tài)配置參數(shù)和使用例子 附正則使用說(shuō)明
正則表達(dá)式匹配 :
~ 為區(qū)分大小寫匹配
~* 為不區(qū)分大小寫匹配
!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配
文件及目錄匹配,其中:
-f和!-f用來(lái)判斷是否存在文件
-d和!-d用來(lái)判斷是否存在目錄
-e和!-e用來(lái)判斷是否存在文件或目錄
-x和!-x用來(lái)判斷文件是否可執(zhí)行
flag標(biāo)記有:
last 相當(dāng)于Apache里的[L]標(biāo)記,表示完成rewrite
break 終止匹配, 不再匹配后面的規(guī)則
redirect 返回302臨時(shí)重定向 地址欄會(huì)顯示跳轉(zhuǎn)后的地址
permanent 返回301永久重定向 地址欄會(huì)顯示跳轉(zhuǎn)后的地址
$args 此變量與請(qǐng)求行中的參數(shù)相等
$content_length 等于請(qǐng)求行的“Content_Length”的值。
$content_type 等同與請(qǐng)求頭部的”Content_Type”的值
$document_root 等同于當(dāng)前請(qǐng)求的root指令指定的值
$document_uri 與$uri一樣
$host 與請(qǐng)求頭部中“Host”行指定的值或是request到達(dá)的server的名字(沒(méi)有Host行)一樣
$limit_rate 允許限制的連接速率
$request_method 等同于request的method,通常是“GET”或“POST”
$remote_addr 客戶端ip
$remote_port 客戶端port
$remote_user 等同于用戶名,由ngx_http_auth_basic_module認(rèn)證
$request_filename 當(dāng)前請(qǐng)求的文件的路徑名,由root或alias和URI request組合而成
$request_body_file
$request_uri 含有參數(shù)的完整的初始URI
$query_string 與$args一樣
$server_protocol 等同于request的協(xié)議,使用“HTTP/1.0”或“HTTP/1.1”
$server_addr request到達(dá)的server的ip,一般獲得此變量的值的目的是進(jìn)行系統(tǒng)調(diào)用。為了避免系統(tǒng)調(diào)用,有必要在listen指令中指明ip,并使用bind參數(shù)。
$server_name 請(qǐng)求到達(dá)的服務(wù)器名
$server_port 請(qǐng)求到達(dá)的服務(wù)器的端口號(hào)
$uri 等同于當(dāng)前request中的URI,可不同于初始值,例如內(nèi)部重定向時(shí)或使用index
結(jié)合QeePHP的例子
if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
多目錄轉(zhuǎn)成參數(shù)
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
目錄對(duì)換
/123456/xxxx -> /xxxx?id=123456
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
例如下面設(shè)定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
目錄自動(dòng)加“/”
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
禁止htaccess
location ~/\.ht {
deny all;
}
禁止多個(gè)目錄
location ~ ^/(cron|templates)/ {
deny all;
break;
}
禁止以/data開(kāi)頭的文件
可以禁止/data/下多級(jí)目錄下.log.txt等請(qǐng)求;
location ~ ^/data {
deny all;
}
禁止單個(gè)目錄
不能禁止.log.txt能請(qǐng)求
location /searchword/cron/ {
deny all;
}
禁止單個(gè)文件
location ~ /data/sql/data.sql {
deny all;
}
給favicon.ico和robots.txt設(shè)置過(guò)期時(shí)間;
這里為favicon.ico為99 天,robots.txt為7天并不記錄404錯(cuò)誤日志
location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
設(shè)定某個(gè)文件的過(guò)期時(shí)間;這里為600秒,并不記錄訪問(wèn)日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}
文件反盜鏈并設(shè)置過(guò)期時(shí)間
這里的return 412 為自定義的http狀態(tài)碼,默認(rèn)為403,方便找出正確的盜鏈的請(qǐng)求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問(wèn)日志,減輕壓力
“expires 3d”所有文件3天的瀏覽器緩存
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
只充許固定ip訪問(wèn)網(wǎng)站,并加上密碼
root /opt/htdocs/www;
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic "C1G_ADMIN";
auth_basic_user_file htpasswd;
將多級(jí)目錄下的文件轉(zhuǎn)成一個(gè)文件,增強(qiáng)seo效果
/job-123-456-789.html 指向/job/123/456/789.html
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;
將根目錄下某個(gè)文件夾指向2級(jí)目錄
如/shanghaijob/ 指向 /area/shanghai/
如果你將last改成permanent,那么瀏覽器地址欄顯是 /location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
上面例子有個(gè)問(wèn)題是訪問(wèn)/shanghai 時(shí)將不會(huì)匹配
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
這樣/shanghai 也可以訪問(wèn)了,但頁(yè)面中的相對(duì)鏈接無(wú)法使用,
如./list_1.html真實(shí)地址是/area /shanghia/list_1.html會(huì)變成/list_1.html,導(dǎo)至無(wú)法訪問(wèn)。
那我加上自動(dòng)跳轉(zhuǎn)也是不行咯
(-d $request_filename)它有個(gè)條件是必需為真實(shí)目錄,而我的rewrite不是的,所以沒(méi)有效果
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
知道原因后就好辦了,讓我手動(dòng)跳轉(zhuǎn)吧
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
文件和目錄不存在的時(shí)候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1/;
}
域名跳轉(zhuǎn)
server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
}
多域名轉(zhuǎn)向
server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "c1gstudio\.net") {
rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;
}
三級(jí)域名跳轉(zhuǎn)
if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1/;
break;
}
域名鏡向
server
{
listen 80;
server_name mirror.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
access_log off;
}
某個(gè)子目錄作鏡向
location ^~ /zhaopinhui {
rewrite ^.+ http://zph.c1gstudio.com/ last;
break;
}
discuz ucenter home (uchome) rewrite
rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;
rewrite ^/(space|network)\.html$ /$1.php last;
rewrite ^/([0-9]+)$ /space.php?uid=$1 last;
discuz 7 rewrite
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\=$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;
給discuz某版塊單獨(dú)配置域名
server_name bbs.c1gstudio.com news.c1gstudio.com;
location = / {
if ($http_host ~ news\.c1gstudio.com$) {
rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
break;
}
}
discuz ucenter 頭像 rewrite 優(yōu)化
location ^~ /ucenter {
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big\.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle\.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small\.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}
jspace rewrite
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~* ^/index.php/
{
rewrite ^/index.php/(.*) /index.php?$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
Nginx正則式說(shuō)明
^~ 標(biāo)識(shí)符后面跟一個(gè)字符串。Nginx將在這個(gè)字符串匹配后停止進(jìn)行正則表達(dá)式的匹配(location指令中正則表達(dá)式的匹配的結(jié)果優(yōu)先使用),如:location ^~ /images/,你希望對(duì)/images/這個(gè)目錄進(jìn)行一些特別的操作,如增加expires頭,防盜鏈等,但是你又想把除了這個(gè)目錄的圖片外的所有圖片只進(jìn)行增加expires頭的操作,這個(gè)操作可能會(huì)用到另外一個(gè)location,例如:location ~* \.(gif|jpg|jpeg)$,這樣,如果有請(qǐng)求/images/1.jpg,nginx如何決定去進(jìn)行哪個(gè)location中的操作呢?結(jié)果取決于標(biāo)識(shí)符^~,如果你這樣寫:location /images/,這樣nginx會(huì)將1.jpg匹配到location ~* \.(gif|jpg|jpeg)$這個(gè)location中,這并不是你需要的結(jié)果,而增加了^~這個(gè)標(biāo)識(shí)符后,它在匹配了/images/這個(gè)字符串后就停止搜索其它帶正則的location。
= 表示精確的查找地址,如location = /它只會(huì)匹配uri為/的請(qǐng)求,如果請(qǐng)求為/index.html,將查找另外的location,而不會(huì)匹配這個(gè),當(dāng)然可以寫兩個(gè)location,location = /和location /,這樣/index.html將匹配到后者,如果你的站點(diǎn)對(duì)/的請(qǐng)求量較大,可以使用這個(gè)方法來(lái)加快請(qǐng)求的響應(yīng)速度。
@ 表示為一個(gè)location進(jìn)行命名,即自定義一個(gè)location,這個(gè)location不能被外界所訪問(wèn),只能用于Nginx產(chǎn)生的子請(qǐng)求,主要為error_page和try_files。
~ 為區(qū)分大小寫的匹配。
~* 不區(qū)分大小寫的匹配(匹配firefox的正則同時(shí)匹配FireFox)。
!~ 不匹配的
!~* 不匹配的
. 匹配除換行符以外的任意字符
\w 匹配字母或數(shù)字或下劃線或漢字
\s 匹配任意的空白符
\d 匹配數(shù)字
\b 匹配單詞的開(kāi)始或結(jié)束
^ 匹配字符串的開(kāi)始
$ 匹配字符串的結(jié)束
* 重復(fù)零次或更多次
+ 重復(fù)一次或更多次
? 重復(fù)零次或一次
{n} 重復(fù)n次
{n,} 重復(fù)n次或更多次
{n,m} 重復(fù)n到m次
*? 重復(fù)任意次,但盡可能少重復(fù)
+? 重復(fù)1次或更多次,但盡可能少重復(fù)
?? 重復(fù)0次或1次,但盡可能少重復(fù)
{n,m}? 重復(fù)n到m次,但盡可能少重復(fù)
{n,}? 重復(fù)n次以上,但盡可能少重復(fù)
\W 匹配任意不是字母,數(shù)字,下劃線,漢字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非數(shù)字的字符
\B 匹配不是單詞開(kāi)頭或結(jié)束的位置
[^x] 匹配除了x以外的任意字符
[^aeiou] 匹配除了aeiou這幾個(gè)字母以外的任意字符
捕獲 (exp) 匹配exp,并捕獲文本到自動(dòng)命名的組里
(?<name>exp) 匹配exp,并捕獲文本到名稱為name的組里,也可以寫成(?'name'exp)
(?:exp) 匹配exp,不捕獲匹配的文本,也不給此分組分配組號(hào)
零寬斷言 (?=exp) 匹配exp前面的位置
(?<=exp) 匹配exp后面的位置
(?!exp) 匹配后面跟的不是exp的位置
(?<!exp) 匹配前面不是exp的位置
注釋 (?#comment) 這種類型的分組不對(duì)正則表達(dá)式的處理產(chǎn)生任何影響,用于提供注釋讓人閱讀