天天看點

nginx php rewrite,nginx 僞靜态Rewrite正則資源彙總

本站一個伺服器本來是windows 系統,采用ISAPI_Rewrite來進行Url重寫,其中有一個規則是

RewriteRule ^/(.{6})(\d{3})(.+)/php/ http://www.xxx.com/qq$2.apk [NC,L,NU]

中間用到了{6}指前面的字元得複6次,然後平移動linux系統下面,采用nginx 的Rewrite來重寫url 結果加載nginx的時候提示報錯

rewrite ^/(.{6})(\d{3})(.+)/php/ http://www.xxx.com/qq$2.apk break;

找了好久的資源終于在 ken  的文章中找到了解決的辦法

将規則前半部分用英文雙引号包起來即可正常應用

如: rewrite "^/(.{6})(\d{3})(.+)/php/" http://www.xxx.com/qq$2.apk break;

就可以正常解析了

順便就把nginx支援正則式的資源全面整理一下,友善下次使用;

nginx rewrite 僞靜态配置參數和使用例子 附正則使用說明

正規表達式比對 :

~ 為區分大小寫比對

~* 為不區分大小寫比對

!~和!~*分别為區分大小寫不比對及不區分大小寫不比對

檔案及目錄比對,其中:

-f和!-f用來判斷是否存在檔案

-d和!-d用來判斷是否存在目錄

-e和!-e用來判斷是否存在檔案或目錄

-x和!-x用來判斷檔案是否可執行

flag标記有:

last 相當于Apache裡的[L]标記,表示完成rewrite

break 終止比對, 不再比對後面的規則

redirect 傳回302臨時重定向 位址欄會顯示跳轉後的位址

permanent 傳回301永久重定向 位址欄會顯示跳轉後的位址

$args 此變量與請求行中的參數相等

$content_length 等于請求行的“Content_Length”的值。

$content_type 等同與請求頭部的”Content_Type”的值

$document_root 等同于目前請求的root指令指定的值

$document_uri 與$uri一樣

$host 與請求頭部中“Host”行指定的值或是request到達的server的名字(沒有Host行)一樣

$limit_rate 允許限制的連接配接速率

$request_method 等同于request的method,通常是“GET”或“POST”

$remote_addr 用戶端ip

$remote_port 用戶端port

$remote_user 等同于使用者名,由ngx_http_auth_basic_module認證

$request_filename 目前請求的檔案的路徑名,由root或alias和URI request組合而成

$request_body_file

$request_uri 含有參數的完整的初始URI

$query_string 與$args一樣

$server_protocol 等同于request的協定,使用“HTTP/1.0”或“HTTP/1.1”

$server_addr request到達的server的ip,一般獲得此變量的值的目的是進行系統調用。為了避免系統調用,有必要在listen指令中指明ip,并使用bind參數。

$server_name 請求到達的伺服器名

$server_port 請求到達的伺服器的端口号

$uri 等同于目前request中的URI,可不同于初始值,例如内部重定向時或使用index

結合PHP的例子

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;

多目錄轉成參數

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;

}

目錄對換

/123456/xxxx -> /xxxx?id=123456

rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

例如下面設定nginx在使用者使用ie的使用重定向到/nginx-ie目錄下:

if ($http_user_agent ~ MSIE) {

rewrite ^(.*)$ /nginx-ie/$1 break;

}

目錄自動加“/”

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

禁止htaccess

location ~/\.ht {

deny all;

}

禁止多個目錄

location ~ ^/(cron|templates)/ {

deny all;

break;

}

禁止以/data開頭的檔案

可以禁止/data/下多級目錄下.log.txt等請求;

location ~ ^/data {

deny all;

}

禁止單個目錄

不能禁止.log.txt能請求

location /searchword/cron/ {

deny all;

}

禁止單個檔案

location ~ /data/sql/data.sql {

deny all;

}

給favicon.ico和robots.txt設定過期時間;

這裡為favicon.ico為99 天,robots.txt為7天并不記錄404錯誤日志

location ~(favicon.ico) {

log_not_found off;

expires 99d;

break;

}

location ~(robots.txt) {

log_not_found off;

expires 7d;

break;

}

設定某個檔案的過期時間;這裡為600秒,并不記錄通路日志

location ^~ /html/scripts/loadhead_1.js {

access_log off;

root /opt/lampp/htdocs/web;

expires 600;

break;

}

檔案反盜鍊并設定過期時間

這裡的return 412 為自定義的http狀态碼,預設為403,友善找出正确的盜鍊的請求

“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”顯示一張防盜鍊圖檔

“access_log off;”不記錄通路日志,減輕壓力

“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通路網站,并加上密碼

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;

将多級目錄下的檔案轉成一個檔案,增強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;

将根目錄下某個檔案夾指向2級目錄

如/shanghaijob/ 指向 /area/shanghai/

如果你将last改成permanent,那麼浏覽器位址欄顯是 /location/shanghai/

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有個問題是通路/shanghai 時将不會比對

rewrite ^/([0-9a-z]+)job$ /area/$1/ last;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

這樣/shanghai 也可以通路了,但頁面中的相對連結無法使用,

如./list_1.html真實位址是/area /shanghia/list_1.html會變成/list_1.html,導至無法通路。

那我加上自動跳轉也是不行咯

(-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,是以沒有效果

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

知道原因後就好辦了,讓我手動跳轉吧

rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

檔案和目錄不存在的時候重定向:

if (!-e $request_filename) {

proxy_pass http://127.0.0.1/;

}

域名跳轉

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;

}

多域名轉向

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;

}

三級域名跳轉

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;

}

某個子目錄作鏡向

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某版塊單獨配置域名

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 優化

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正則式說明

^~     辨別符後面跟一個字元串。Nginx将在這個字元串比對後停止進行正規表達式的比對(location指令中正規表達式的比對的結果優先使用),如:location ^~ /images/,你希望對/images/這個目錄進行一些特别的操作,如增加expires頭,防盜鍊等,但是你又想把除了這個目錄的圖檔外的所有圖檔隻進行增加expires頭的操作,這個操作可能會用到另外一個location,例如:location ~* \.(gif|jpg|jpeg)$,這樣,如果有請求/images/1.jpg,nginx如何決定去進行哪個location中的操作呢?結果取決于辨別符^~,如果你這樣寫:location /images/,這樣nginx會将1.jpg比對到location ~* \.(gif|jpg|jpeg)$這個location中,這并不是你需要的結果,而增加了^~這個辨別符後,它在比對了/images/這個字元串後就停止搜尋其它帶正則的location。

=      表示精确的查找位址,如location = /它隻會比對uri為/的請求,如果請求為/index.html,将查找另外的location,而不會比對這個,當然可以寫兩個location,location = /和location /,這樣/index.html将比對到後者,如果你的站點對/的請求量較大,可以使用這個方法來加快請求的響應速度。

@      表示為一個location進行命名,即自定義一個location,這個location不能被外界所通路,隻能用于Nginx産生的子請求,主要為error_page和try_files。

~      為區分大小寫的比對。

~*     不區分大小寫的比對(比對firefox的正則同時比對FireFox)。

!~     不比對的

!~*    不比對的

.     比對除換行符以外的任意字元

\w     比對字母或數字或下劃線或漢字

\s     比對任意的空白符

\d     比對數字

\b     比對單詞的開始或結束

^     比對字元串的開始

$     比對字元串的結束

*     重複零次或更多次

+     重複一次或更多次

?     重複零次或一次

{n}     重複n次

{n,}     重複n次或更多次

{n,m}     重複n到m次

*?     重複任意次,但盡可能少重複

+?     重複1次或更多次,但盡可能少重複

??     重複0次或1次,但盡可能少重複

{n,m}?     重複n到m次,但盡可能少重複

{n,}?     重複n次以上,但盡可能少重複

\W     比對任意不是字母,數字,下劃線,漢字的字元

\S     比對任意不是空白符的字元

\D     比對任意非數字的字元

\B     比對不是單詞開頭或結束的位置

[^x]     比對除了x以外的任意字元

[^aeiou]     比對除了aeiou這幾個字母以外的任意字元

捕獲     (exp)     比對exp,并捕獲文本到自動命名的組裡

(?exp)     比對exp,并捕獲文本到名稱為name的組裡,也可以寫成(?'name'exp)

(?:exp)     比對exp,不捕獲比對的文本,也不給此分組配置設定組号

零寬斷言     (?=exp)     比對exp前面的位置

(?<=exp)     比對exp後面的位置

(?!exp)     比對後面跟的不是exp的位置

(?

注釋     (?#comment)     這種類型的分組不對正規表達式的處理産生任何影響,用于提供注釋讓人閱讀