Nginx中的location比對與rewrite重寫跳轉
- 一、常見的Nginx正規表達式
- 二、location比對
-
- 2.1 location常用的比對規則:
- 2.2 location 優先級
- 2.3 location 示例說明
- 2.4 實際網站使用中,至少有三個比對規則定義
-
- 第一個必選規則
- 第二個必選規則
- 第三個規則
- 三、rewrite重寫跳轉
-
- 3.1 rewrite 跳轉實作
- 3.2 rewrite執行順序
- 3.3 rewrite文法格式
- 3.4 flag标記說明
- 四、rewrite 示例
-
- 4.1 基于域名的跳轉
- 4.2 基于用戶端 IP 通路跳轉
- 4.3 基于舊域名跳轉到新域名後面加目錄
- 4.4 基于參數比對的跳轉
- 4.5 基于目錄下所有 php 結尾的檔案跳轉
- 4.6 基于最普通一條 url 請求的跳轉
一、常見的Nginx正規表達式
^ :比對輸入字元串的起始位置
$ :比對輸入字元串的結束位置
* :比對前面的字元零次或多次。如“ol*”能比對“o”及“ol”、“oll”
+ :比對前面的字元一次或多次。如“ol+”能比對“ol”及“oll”、“olll”,但不能比對“o”
? :比對前面的字元零次或一次,例如“do(es)?”能比對“do”或者“does”,”?”等效于”{0,1}”
. :比對除“\n”之外的任何單個字元,若要比對包括“\n”在内的任意字元,請使用諸如“[.\n]”之類的模式
\ :将後面接着的字元标記為一個特殊字元或一個原義字元或一個向後引用。如“\n”比對一個換行符,而“\$”則比對“$”
\d :比對純數字
{n} :重複 n 次
{n,} :重複 n 次或更多次
{n,m} :重複 n 到 m 次
[] :定義比對的字元範圍
[c] :比對單個字元 c
[a-z] :比對 a-z 小寫字母的任意一個
[a-zA-Z0-9] :比對所有大小寫字母或數字
() :表達式的開始和結束位置
| :或運算符
二、location比對
**location大緻可以分為三類:
精準比對:location = / {}
一般比對:location / {}
正則比對:location ~ / {}**
2.1 location常用的比對規則:
= :進行普通字元精确比對,也就是完全比對。
^~ :表示普通字元比對。使用字首比對。如果比對成功,則不再比對其它 location。
~ :區分大小寫的比對。
~* :不區分大小寫的比對。
!~ :區分大小寫的比對取非。
!~* :不區分大小寫的比對取非。
2.2 location 優先級
首先精确比對 =
其次字首比對 ^~
其次是按檔案中順序的正則比對 ~或~*
然後比對不帶任何修飾的字首比對
最後是交給 / 通用比對
2.3 location 示例說明
(1)location = / {}
=為精确比對 / ,主機名後面不能帶任何字元串,比如通路 / 和 /data,則 / 比對,/data 不比對
再比如 location = /abc,則隻比對/abc ,/abc/或 /abcd不比對。若 location /abc,則即比對/abc 、/abcd/ 同時也比對 /abc/。
(2)location / {}
因為所有的位址都以 / 開頭,是以這條規則将比對到所有請求 比如通路 / 和 /data, 則 / 比對, /data 也比對,
但若後面是正規表達式會和最長字元串優先比對(最長比對)
(3)location /documents/ {}
比對任何以 /documents/ 開頭的位址,比對符合以後,還要繼續往下搜尋其它 location
隻有其它 location後面的正規表達式沒有比對到時,才會采用這一條
(4)location /documents/abc {}
比對任何以 /documents/abc 開頭的位址,比對符合以後,還要繼續往下搜尋其它 location
隻有其它 location後面的正規表達式沒有比對到時,才會采用這一條
(5)location ^~ /images/ {}
比對任何以 /images/ 開頭的位址,比對符合以後,停止往下搜尋正則,采用這一條
(6)location ~* \.(gif|jpg|jpeg)$ {}
比對所有以 gif、jpg或jpeg 結尾的請求
然而,所有請求 /images/ 下的圖檔會被 location ^~ /images/ 處理,因為 ^~ 的優先級更高,是以到達不了這一條正則
(7)location /images/abc {}
最長字元比對到 /images/abc,優先級最低,繼續往下搜尋其它 location,會發現 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
比對以/images/abc 開頭的,優先級次之,隻有去掉 location ^~ /images/ 才會采用這一條
(9)location /images/abc/1.html {}
比對/images/abc/1.html 檔案,如果和正則 ~ /images/abc/1.html 相比,正則優先級更高
優先級總結:
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (location /)
2.4 實際網站使用中,至少有三個比對規則定義
第一個必選規則
直接比對網站根,通過域名通路網站首頁比較頻繁,使用這個會加速處理,比如說官網。
這裡是直接轉發給後端應用伺服器了,也可以是一個靜态首頁
location = / {
proxy_pass http://tomcat_server/;
}
第二個必選規則
處理靜态檔案請求,這是nginx作為http伺服器的強項
有兩種配置模式,目錄比對或字尾比對,任選其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
第三個規則
通用規則,比如用來轉發帶.php、.jsp字尾的動态請求到後端應用伺服器
非靜态檔案請求就預設是動态請求
location / {
proxy_pass http://tomcat_server;
}
三、rewrite重寫跳轉
rewrite功能就是使用nginx提供的全局變量或自己設定的變量,結合正規表達式和标記實作URL重寫以及重定向。
例如:更換域名後需要保持舊的域名能夠轉到新的域名上、某網頁發生改變需要跳轉到新的頁面、網站防盜鍊等等需求。
rewrite隻能放在server{},location{},if{}中,并且預設隻能對域名後面的除去傳遞的參數外的字元串起作用。
例如:http://www.meng.com/a/we/index.php?id=1&u=str 隻對/a/we/index.php重寫。
3.1 rewrite 跳轉實作
Nginx:通過ngx_http_rewrite_module子產品支援URL重寫、支援if條件判斷,但不支援else
跳轉:從一個location跳轉到另一個location,循環最多可以執行10次,超過後nginx将傳回500錯誤
PCRE支援:perl相容正規表達式的文法規則比對
重寫子產品set指令:建立新的變量并為其指派
3.2 rewrite執行順序
1、執行server塊裡面的rewrite指令
2、執行location比對
3、執行標明的location中的rewrite指令
3.3 rewrite文法格式
文法rewrite<regex><replacement><flag>;
regex:表示正則比對規則
replacement:表示跳轉後的内容
flag:表示rewrite支援的flag标記
3.4 flag标記說明
last :本條規則比對完成後,繼續向下比對新的location URI規則,一般用在 server 和 if 中。
break :本條規則比對完成即終止,不再比對後面的任何規則,一般使用在 location 中。
redirect :傳回302臨時重定向,浏覽器位址會顯示跳轉後的URL位址。
permanent :傳回301永久重定向,浏覽器位址欄會顯示跳轉後的URL位址。
四、rewrite 示例
4.1 基于域名的跳轉
現在公司舊域名www.meng.com有業務需求變更,需要使用新域名www.kiki.com代替,但是舊域名不能廢除,需要跳轉到新域名上,而且後面的參數保持不變。
vim /usr/local/nginx/conf/nginx.conf #修改nginx配置檔案
server {
listen 80;
server_name www.meng.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.meng.com.access.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.meng.com'){ #$host為rewrite全局變量,代表請求主機頭字段或主機名
rewrite ^/(.*)$ http://www.kiki.com/$1 permanent; #$1為正則比對的内容,即域名後邊的字元串
}
root html;
index index.html index.htm;
}
}
mkdir -p /var/log/nginx #嵌套建立nginx目錄
systemctl restart nginx.service #重新開機nginx服務
echo "192.168.28.10 www.meng.com www/kiki.com >> /etc/hosts"
systemctl restart nginx.service #重新開機nginx服務
浏覽器輸入模拟通路 http://www.meng.com/test/1.html
會跳轉到www.kiki.com/test/1.html,檢視元素可以看到傳回301,實作了永久重定向跳轉,而且域名後的參數也正常跳轉。
mkdir -p /usr/local/nginx/html/test/
echo 'hello test!' > /usr/local/nginx/html/test/1.html
4.2 基于用戶端 IP 通路跳轉
今天公司業務新版本上線,要求所有 IP 通路任何内容都顯示一個固定維護頁面,隻有公司 IP :192.168.28.10通路正常。
vim /usr/local/nginx/conf/nginx.conf #編輯nginx配置檔案
server {
listen 80;
server_name www.megn.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.meng.com-access.log main; #日志修改
#設定是否合法的IP标記
set $rewrite true; #設定變量$rewrite,變量值為boole值true
#判斷是否為合法IP
if ($remote_addr = "192.168.28.10"){ #當用戶端IP為192.168.28.10時,将變量值設為false,不進行重寫
set $rewrite false;
}
#除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面
if ($rewrite = true){ #當變量值為true時,進行重寫
rewrite (.+) /weihu.html; #重寫在通路IP後邊插入/weihu.html,例如192.168.28.11/weihu.html
}
location = /weihu.html {
root /var/www/html; #網頁傳回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/html/
echo "<h1>We are maintaining now!</h1>" > /var/www/html/weihu.html
systemctl restart nginx
浏覽器通路
隻有 IP 為 192.168.184.10 能正常通路,其它位址都是維護頁面
4.3 基于舊域名跳轉到新域名後面加目錄
現在通路的是 http://kiki.lic.com,現在需要将這個域名下面的通路都跳轉到http://www.meng.com/kiki
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name kiki.meng.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.meng.com-access.log;
#添加
location /post {
rewrite (.+) http://www.meng.com/kiki$1 permanent; #這裡的$1為位置變量,代表/post
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /usr/local/nginx/html/kiki/post 嵌套建立目錄
systemctl restart nginx.service 重新開機服務
使用浏覽器通路
http://kiki.meng.com/post/1.html 跳轉到 http://www.megn.com/kiki/post/1.html
echo 'this is 1.html' > /usr/local/nginx/html/kiki/post/1.html
systemctl restart nginx.service
4.4 基于參數比對的跳轉
現在通路http://www.meng.com/100-(100|200)-100.html 跳轉到http://www.meng.com頁面。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.lic.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.lic.com-access.log main;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.lic.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏覽器通路
http://www.lic.com/100-200-100.html 或 http://www.lic.com/100-100-100.html 跳轉到http://www.lic.com頁面。
4.5 基于目錄下所有 php 結尾的檔案跳轉
要求通路 http://www.lic.com/upload/123.php 跳轉到首頁。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.meng.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.meng.com-access.log main;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.meng.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏覽器通路
http://www.meng.com/upload/123.php 跳轉到http://www.meng.com頁面。
4.6 基于最普通一條 url 請求的跳轉
要求通路一個具體的頁面如 http://www.meng.com/abc/123.html 跳轉到首頁
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.meng.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.meng.com-access.log main;
location ~* ^/abc/123.html {
rewrite (.+) http://www.meng.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏覽器通路
http://www.meng.com/abc/123.html 跳轉到www.meng.com