西西一個服務器本來是windows 系統(tǒng),采用ISAPI_Rewrite來進行Url重寫,其中有一個規(guī)則是
RewriteRule ^/(.{6})(\d{3})(.+)/php/ http://elephantinaurance.com/qq$2.apk [NC,L,NU]
中間用到了{6}指前面的字符得復6次,然后平移動linux系統(tǒng)下面,采用nginx 的Rewrite來重寫url 結果加載nginx的時候提示報錯
rewrite ^/(.{6})(\d{3})(.+)/php/ http://elephantinaurance.com/qq$2.apk break;
找了好久的資源終于在 ken 的文章中找到了解決的辦法
將規(guī)則前半部分用英文雙引號包起來即可正常應用
如: rewrite "^/(.{6})(\d{3})(.+)/php/" http://elephantinaurance.com/qq$2.apk break;
就可以正常解析了
順便就把nginx支持正則式的資源全面整理一下,方便下次使用;
nginx rewrite 偽靜態(tài)配置參數(shù)和使用例子 附正則使用說明
正則表達式匹配 :
~ 為區(qū)分大小寫匹配
~* 為不區(qū)分大小寫匹配
!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配
文件及目錄匹配,其中:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執(zhí)行
flag標記有:
last 相當于Apache里的[L]標記,表示完成rewrite
break 終止匹配, 不再匹配后面的規(guī)則
redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址
permanent 返回301永久重定向 地址欄會顯示跳轉后的地址
$args 此變量與請求行中的參數(shù)相等
$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 含有參數(shù)的完整的初始URI
$query_string 與$args一樣
$server_protocol 等同于request的協(xié)議,使用“HTTP/1.0”或“HTTP/1.1”
$server_addr request到達的server的ip,一般獲得此變量的值的目的是進行系統(tǒng)調用。為了避免系統(tǒng)調用,有必要在listen指令中指明ip,并使用bind參數(shù)。
$server_name 請求到達的服務器名
$server_port 請求到達的服務器的端口號
$uri 等同于當前request中的URI,可不同于初始值,例如內(nèi)部重定向時或使用index
結合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;
多目錄轉成參數(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;
}
目錄對換
/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狀態(tài)碼,默認為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訪問網(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;
將多級目錄下的文件轉成一個文件,增強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 優(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正則式說明
^~ 標識符后面跟一個字符串。Nginx將在這個字符串匹配后停止進行正則表達式的匹配(location指令中正則表達式的匹配的結果優(yōu)先使用),如: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產(chǎn)生的子請求,主要為error_page和try_files。
~ 為區(qū)分大小寫的匹配。
~* 不區(qū)分大小寫的匹配(匹配firefox的正則同時匹配FireFox)。
!~ 不匹配的
!~* 不匹配的
. 匹配除換行符以外的任意字符
\w 匹配字母或數(shù)字或下劃線或漢字
\s 匹配任意的空白符
\d 匹配數(shù)字
\b 匹配單詞的開始或結束
^ 匹配字符串的開始
$ 匹配字符串的結束
* 重復零次或更多次
+ 重復一次或更多次
? 重復零次或一次
{n} 重復n次
{n,} 重復n次或更多次
{n,m} 重復n到m次
*? 重復任意次,但盡可能少重復
+? 重復1次或更多次,但盡可能少重復
?? 重復0次或1次,但盡可能少重復
{n,m}? 重復n到m次,但盡可能少重復
{n,}? 重復n次以上,但盡可能少重復
\W 匹配任意不是字母,數(shù)字,下劃線,漢字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非數(shù)字的字符
\B 匹配不是單詞開頭或結束的位置
[^x] 匹配除了x以外的任意字符
[^aeiou] 匹配除了aeiou這幾個字母以外的任意字符
捕獲 (exp) 匹配exp,并捕獲文本到自動命名的組里
(?<name>exp) 匹配exp,并捕獲文本到名稱為name的組里,也可以寫成(?'name'exp)
(?:exp) 匹配exp,不捕獲匹配的文本,也不給此分組分配組號
零寬斷言 (?=exp) 匹配exp前面的位置
(?<=exp) 匹配exp后面的位置
(?!exp) 匹配后面跟的不是exp的位置
(?<!exp) 匹配前面不是exp的位置
注釋 (?#comment) 這種類型的分組不對正則表達式的處理產(chǎn)生任何影響,用于提供注釋讓人閱讀