文章目錄
- 一、常見的Nginx正規表達式
- 二、location
-
-
- 1、location大緻可以分為三類
- 2、location常用的比對規則
- 3、location 優先級
- 4、location 示例說明
-
- 三、rewrite
-
-
- 1、rewrite 跳轉實作
- 2、rewrite執行順序
- 3、rewrite文法格式
- 4、flag标記說明
- 5、rewrite 示例
- (2)基于用戶端 IP 通路跳轉
- 3)基于舊域名跳轉到新域名後面加目錄
- (4)基于參數比對的跳轉
- (5)基于目錄下所有 php 結尾的檔案跳轉
-
- 總結
一、常見的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
1、location大緻可以分為三類
- 精準比對:location = / {}
- 一般比對:location / {}
- 正則比對:location ~ / {}
2、location常用的比對規則
= :進行普通字元精确比對,也就是完全比對。
^~ :表示普通字元比對。使用字首比對。如果比對成功,則不再比對其它 location。
~ :區分大小寫的比對。
~* :不區分大小寫的比對。
!~ :區分大小寫的比對取非。
!~* :不區分大小寫的比對取非。
3、location 優先級
首先精确比對 =
其次字首比對 ^~
其次是按檔案中順序的正則比對 ~或~*
然後比對不帶任何修飾的字首比對
最後是交給 / 通用比對
4、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 /)
5、實際網站使用中,至少有三個比對規則定義
第一個必選規則
直接比對網站根,通過域名通路網站首頁比較頻繁,使用這個會加速處理,比如說官網。
這裡是直接轉發給後端應用伺服器了,也可以是一個靜态首頁
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.lisi.com/a/we/index.php?id=1&u=str 對/a/we/index.php重寫。
1、rewrite 跳轉實作
Nginx:通過ngx_http_rewrite_module子產品支援URL重寫、支援if條件判斷,但不支援else
跳轉:從一個location跳轉到另一個location,循環最多可以執行10次,超過後nginx将傳回500錯誤
PCRE支援:perl相容正規表達式的文法規則比對
重寫子產品set指令:建立新的變量并為其指派
2、rewrite執行順序
第一優先:執行server塊裡面的rewrite指令
第二優先:執行location比對
第三優先:執行標明的location中的rewrite指令
3、rewrite文法格式
文法rewrite;
regex:表示正則比對規則
replacement:表示跳轉後的内容
flag:表示rewrite支援的flag标記
4、flag标記說明
last :本條規則比對完成後,繼續向下比對新的location URI規則,一般用在 server 和 if 中。
break :本條規則比對完成即終止,不再比對後面的任何規則,一般使用在 location 中。
redirect :傳回302臨時重定向,浏覽器位址會顯示跳轉後的URL位址。
permanent :傳回301永久重定向,浏覽器位址欄會顯示跳轉後的URL位址。
5、rewrite 示例
1)基于域名的跳轉
現在公司舊域名www.test.com有業務需求變更,需要使用新域名www.lisi.com代替,但是舊域名不能廢除,需要跳轉到新域名上,而且後面的參數保持不變。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log /var/log/nginx/www.test.com.access.log;
location / {
if ($host = 'www.test.com'){
rewrite ^/(.*)$ http://www.lisi.com/$1 permanent;
}
root html;
index index.html index.htm;
}
}
echo "192.168.206.11 www.lisi.com www.test.com" >> /etc/hosts
systemctl restart nginx

浏覽器輸入模拟通路 http://www.test.com/1.html
會跳轉到www.lisi.com/1.html,檢視元素可以看到傳回301,實作了永久重定向跳轉,而且域名後的參數也正常跳轉。
(2)基于用戶端 IP 通路跳轉
今天公司業務新版本上線,要求所有 IP 通路任何内容都顯示一個固定維護頁面,隻有公司 IP:192.168.206.11通路正常。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log /var/log/nginx/www.test.com.access.log;
#設定是否合法的IP标記;設定變量$rewrite,變量值為boole值true
set $rewrite true;
#判斷是否為合法IP;當用戶端IP為192.168.206.11時,将變量值設為false,不進行重寫
if ($remote_addr = "192.168.206.11"){
set $rewrite false;
}
#除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面
#當變量值為true時,進行重寫
if ($rewrite = true){
#重寫在通路IP後邊插入/weihu.html,例如192.168.206.11/weihu.html
rewrite (.+) /weihu.html;
}
location = /weihu.html {
#網頁傳回/var/www/html/weihu.html的内容
root /var/www/html;
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/html/
echo "<h1>As server maintenance, please visit later, thank you.</h1>" > /var/www/html/weihu.html
echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx
浏覽器通路
隻有 IP 為 192.168.206.11 能正常通路,其它位址都是維護頁面
3)基于舊域名跳轉到新域名後面加目錄
現在通路的是 http://lisi.test.com,現在需要将這個域名下面的通路都跳轉到http://www.test.com/lisi
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name lisi.test.com;
charset utf-8;
access_log /var/log/nginx/lisi.test.com.access.log;
#添加;這裡的$1為位置變量,代表/post
location /post {
rewrite (.+) http://www.test.com/lisi$1 permanent;
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /usr/local/nginx/html/lisi/post
echo "this is 1.html" > /usr/local/nginx/html/lisi/post/1.html
echo "192.168.206.11 lisi.test.com www.test.com" >> /etc/hosts
systemctl restart nginx.service
使用浏覽器通路
http://lisi.test.com/post/1.html 跳轉到 http://www.test.com/lisi/post/1.html
(4)基于參數比對的跳轉
現在通路http://www.test.com/100-(100|200)-100(任意數字).html 跳轉到http://www.test.com頁面。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log /var/log/nginx/www.test.com.access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.test.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx
浏覽器通路
http://www.test.com/100-200-100.html 或
http://www.test.com/100-100-100.html 跳轉到http://www.test.com頁面。
(5)基于目錄下所有 php 結尾的檔案跳轉
要求通路 http://www.test.com/upload/abc.php 跳轉到首頁。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log /var/log/nginx/www.test.com.access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.test.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx
浏覽器通路
http://www.test.com/upload/abc.php 跳轉到http://www.test.com頁面。
總結
從功能看 rewrite 和 location 似乎有點像,都能實作跳轉,主要差別在于
rewrite 是在同一域
location 是對一類路徑做控制通路或反向代理,還可以 proxy_pass 到其他機器