nginx rewrite重寫

最多見的: 靜態地址重定向到帶參數的動態地址php

rewrite "^(.*)/service/(.*)\.html$" $1/service.php?sid=$2 permanent;  html

反過來: 帶參數的動態地址重定向到靜態地址nginx

if ($query_string ~* id=(.*)) { 
set $id $1; 
rewrite "^(.*)/article.asp$" $1/article/$id.htm last; 



泛域名解析 

view plaincopy to clipboardprint?
server_name www.w3cgroup.com *.w3cgroup.com; 
server_name_in_redirect off; 
#設置默認root 
set $rootdir /usr/local/nginx/html/w3cgroup/;正則表達式

#匹配三級或三級以上的域名服務器

if ($host ~* ^(.+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$)  {cookie

}
#匹配三級域名 
if ($host ~* ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$) { 
#三級域名中有訪問指定的目錄則重定向到相應的二級域名下 
rewrite "^.+upload/?(.*)$" http://upload.w3cgroup.com/$1 permanent; 
rewrite "^.+ijc/?(.*)$" http://ijc.w3cgroup.com/$1 permanent; 
break; 

#匹配二級域名 
if ($host ~* ^([^\.]+)\.([^\.]+)\.([^\.]+)$) { 
set $rs1 $1; 

#設置www時root 
if ($rs1 ~* ^www$) { 
set $rootdir /usr/local/nginx/html/platform_ig/; 
#二級域名中有訪問指定的目錄則重定向到相應的二級域名下,注意,這裏要使用last 
rewrite "^.+upload/?(.*)$" upload/$1 last; 
rewrite "^.+ijc/?(.*)$" ijc/$1 last; 
break; 

#設置非www二級域名時root 
if ($rs1 !~* ^www$) { 
set $rootdir /usr/local/nginx/html/w3cgroup/$rs1; 
#二級域名中有訪問指定的目錄則重定向到相應的二級域名下 
rewrite "^.+upload/?(.*)$" http://upload.w3cgroup.com/$1 permanent; 
rewrite "^.+ijc/?(.*)$" http://ijc.w3cgroup.com/$1 permanent; 
break; 

#應用root 
root $rootdir; 
index index.php index.html; 
error_page 404 http://$host/; 

注意:if () {} 之間須要空格,不然Nginx.conf會報unknow directive 錯誤! 

參考: 

Nginx Rewrite Flags 
* last 至關於Apache裏的[L]標記,表示完成rewrite
* break 終止匹配, 再也不匹配後面的規則
* redirect 返回302臨時重定向
* permanent 返回301永久重定向

Nginx正則表達式匹配 
* ~ 爲區分大小寫匹配
* ~* 爲不區分大小寫匹配
* !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配

Nginx文件及目錄匹配 
* -f和!-f用來判斷是否存在文件
* -d和!-d用來判斷是否存在目錄
* -e和!-e用來判斷是否存在文件或目錄
* -x和!-x用來判斷文件是否可執行

url

全局變量
下面是能夠用做if判斷的全局變量spa

    • $args : #這個變量等於請求行中的參數,同$query_string
    • $content_length : 請求頭中的Content-length字段。
    • $content_type : 請求頭中的Content-Type字段。
    • $document_root : 當前請求在root指令中指定的值。
    • $host : 請求主機頭字段,不然爲服務器名稱。
    • $http_user_agent : 客戶端agent信息
    • $http_cookie : 客戶端cookie信息
    • $limit_rate : 這個變量能夠限制鏈接速率。
    • $request_method : 客戶端請求的動做,一般爲GET或POST。
    • $remote_addr : 客戶端的IP地址。
    • $remote_port : 客戶端的端口。
    • $remote_user : 已經通過Auth Basic Module驗證的用戶名。
    • $request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。
    • $scheme : HTTP方法(如http,https)。
    • $server_protocol : 請求使用的協議,一般是HTTP/1.0或HTTP/1.1。
    • $server_addr : 服務器地址,在完成一次系統調用後能夠肯定這個值。
    • $server_name : 服務器名稱。
    • $server_port : 請求到達服務器的端口號。
    • $request_uri : 包含請求參數的原始URI,不包含主機名,如:」/foo/bar.php?arg=baz」。
    • $uri : 不帶請求參數的當前URI,$uri不包含主機名,如」/foo/bar.html」。
    • $document_uri : 與$uri相同。

例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php日誌

2.3 經常使用正則

  • . : 匹配除換行符之外的任意字符
  • ? : 重複0次或1次
  • + : 重複1次或更屢次
  • * : 重複0次或更屢次
  • \d :匹配數字
  • ^ : 匹配字符串的開始
  • $ : 匹配字符串的介紹
  • {n} : 重複n次
  • {n,} : 重複n次或更屢次
  • [c] : 匹配單個字符c
  • [a-z] : 匹配a-z小寫字母的任意一個

小括號()之間匹配的內容,能夠在後面經過$1來引用,$2表示的是前面第二個()裏的內容。正則裏面容易讓人困惑的是\轉義特殊字符。code

 1 http {
 2     # 定義image日誌格式
 3     log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
 4     # 開啓重寫日誌
 5     rewrite_log on;
 6  
 7     server {
 8         root /home/www;
 9  
10         location / {
11                 # 重寫規則信息
12                 error_log logs/rewrite.log notice; 
13                 # 注意這裏要用‘’單引號引發來,避免{}
14                 rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
15                 # 注意不能在上面這條規則後面加上「last」參數,不然下面的set指令不會執行
16                 set $image_file $3;
17                 set $image_type $4;
18         }
19  
20         location /data {
21                 # 指定針對圖片的日誌格式,來分析圖片類型和大小
22                 access_log logs/images.log mian;
23                 root /data/images;
24                 # 應用前面定義的變量。判斷首先文件在不在,不在再判斷目錄在不在,若是還不在就跳轉到最後一個url裏
25                 try_files /$arg_file /image404.html;
26         }
27         location = /image404.html {
28                 # 圖片不存在返回特定的信息
29                 return 404 "image not found\n";
30         }
31 }

對形如/images/ef/uh7b3/test.png的請求,重寫到/data?file=test.png,因而匹配到location /data,先看/data/images/test.png文件存不存在,若是存在則正常響應,若是不存在則重寫tryfiles到新的image404 location,直接返回404狀態碼。

例2

1
rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;

 

對形如/images/bla_500x400.jpg的文件請求,重寫到/resizer/bla.jpg?width=500&height=400地址,並會繼續嘗試匹配location。

相關文章
相關標籤/搜索