一、常用的Nginx 正則運算式

二、訪問路由location
2.1location的分類
location 大致可以分為三類:
- 精準匹配:location = / {}
- 一般匹配:location / {}
- 正則匹配:location ~ / {}
2.2location 常用的匹配規則

2.3location 優先級
- 首先精確匹配 =
- 其次前綴匹配 ^~
- 其次是按檔案中順序的正則匹配 或*
- 然后匹配不帶任何修飾的前綴匹配
- 最后是交給 / 通用匹配
2.4location 示例說明
(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 /)
location匹配
首先看優先級:精確>前綴>正則>一般>通用
優先級相同:正則看上下順序,上面的優先;一般則看長度,最長匹配優先
精確、前綴、正則、一般都沒有匹配到就看通用
2.5實際網站使用中的三個匹配規則定義
2.5.1 第一個必選規則
直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,比如說官網,這里是直接轉發給后端應用服務器了,也可以是一個靜態首頁,
location = / { proxy_pass http://tomcat_server/; }
2.5.2 第二個必選規則是處理靜態檔案請求
這是nginx作為http服務器的強項!有兩種配置模式,目錄匹配或后綴匹配,任何其一或搭配使用,
location ^~ /static/ { root /webroot/static/; } location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; }
2.5.3 第三個規則就是通用規則
比如用來轉發帶.php、.jsp后綴的動態請求到后端應用服務器,非靜態檔案請求就默認是動態請求,
location / { proxy_pass http://tomcat_server; }
三、訪問重新rewrite
3.1rewrite的概述
rewrite功能就是,使用nginx提供的全域變數或自己設定的變數,結合正則運算式和標志位實作url重寫以及重定向,
rewrite只能放在server{},location{},if{}中,并且默認只能對域名后邊的除去傳遞的引數外的字串起作用,
例如:
http://www.fzr.com/zzj/index.php?id=1&u=str 只對/zzj/index.php重寫,
3.2rewrite 執行順序如下
(1) 執行 server 塊里面的 rewrite 指令
(2) 執行 location 匹配
(3) 執行選定的 location 中的 rewrite 指令
語法: rewrite [flag];
- regex :表示正則匹配規則
- replacement :表示跳轉后的內容
- flag :表示 rewrite 支持的 flag 標記
###flag標記說明###
- last :本條規則匹配完成后,繼續向下匹配新的location URI規則,一般用在 server 和 if 中
- break :本條規則匹配完成即終止,不再匹配后面的任何規則,一般使用在 location 中
- redirect:回傳302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址
- permanent:回傳301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址,
四、rewrite 示例
4.1基于域名跳轉
4.1.1 基于域名跳轉——操作步驟
現在公司舊域名www.fzr.com有業務需求變更,需要使用新域名www.zzj.com代替,但是舊域名不能廢除,需要跳轉到新域名上,而且后面的引數保持不變,
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.fzr.com-access.log; #日志修改 location / { #添加域名重定向 if ($host = 'www.fzr.com'){ #$host為rewrite全域變數,代表請求主機頭欄位或主機名 rewrite ^/(.*)$ http://www.zzj.com/$1 permanent; #$1為正則匹配的內容,即域名后邊的字串 } root html; index index.html index.htm; } } echo "192.168.10.10 www.fzr.com www.zzj.com" >> /etc/hosts systemctl restart nginx #重啟服務
瀏覽器輸入模擬訪問 http://www.fzr.com/test/index.html會跳轉到www.zzj.com/test/index.html,查看元素可以看到回傳301,實作了永久重定向跳轉,而且域名后的引數也正常跳轉
4.1.2 實體操作:基于域名跳轉
(1)修改主組態檔


(2)重啟服務并添加映射關系

![]()
(3)創建網頁


(4)瀏覽器中訪問測驗


4.2基于客戶端 IP 訪問跳轉
4.2.1 基于客戶端 IP 訪問跳轉的操作步驟
要求:今天公司業務新版本上線,要求所有 IP 訪問任何內容都顯示一個固定維護頁面,只有公司 IP:192.168.10.10訪問正常
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.fzr.com-access.log; #日志修改 #設定是否合法的IP標記 set $rewrite true; #設定變數$rewrite,變數值為boole值true #判斷是否為合法IP if ($remote_addr = "192.168.10.10"){ #當客戶端IP為192.168.10.10時,將變數值設為false,不進行重寫 set $rewrite false; } #除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面 if ($rewrite = true){ #當變數值為true時,進行重寫 rewrite (.+) /index.html; #重寫在訪問IP后邊插入/index.html,例如192.168.10.10/index.html } location = /index.html { root /var/www/html; #網頁回傳/var/www/html/index.html的內容 } location / { root html; index index.html index.htm; } } mkdir -p /var/www/html/ echo "<h1>正在維護</h1>" > /var/www/html/index.html systemctl restart nginx
4.2.2 實體操作:基于客戶端 IP 訪問跳轉
(1)修改組態檔
![]()

(2)檢查組態檔并重啟服務
![]()
(3)創建跳轉后的網頁目錄和內容

(4)瀏覽器訪問測驗
本機訪問:

其它主機訪問:
現在域名IP地址做映射:

然后再訪問:

4.3基于舊域名跳轉到新域名后面加目錄
4.3.1基于舊域名跳轉到新域名后面加目錄的操作步驟
現在訪問的是 http://bbs.fzr.com/test,現在需要將這個域名下面的訪問都跳轉到http://www.fzr.com/bbs/test
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name bbs.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/bbs.fzr.com-access.log; #添加 location /test { rewrite (.+) http://www.fzr.com/bbs$1 permanent; #這里的$1為位置變數,代表/test } location / { root html; index index.html index.htm; } } mkdir -p /usr/local/nginx/html/bbs/post echo "this is web" >> /usr/local/nginx/html/bbs/post/1.html echo "192.168.10.10 bbs.fzr.com www.fzr.com" >> /etc/hosts systemctl restart nginx
4.3.2實體操作:基于舊域名跳轉到新域名后面加目錄
(1)修改主組態檔
![]()

(2)重啟服務并創建網頁檔案

![]()

(3)添加映射關系并使用瀏覽器訪問測驗

本機測驗:
![]()

4.4基于引數匹配的跳轉
4.4.1 基于引數匹配的跳轉的步驟
訪問http://www.fzr.com/100-(100|200)-100.html 跳轉到http://www.fzr.com頁面
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/fzr.kgc.com-access.log; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.*) http://www.fzr.com permanent; } location / { root html; index index.html index.htm; } systemctl restart nginx
解釋:
$request_uri: 包含請求引數的原始URI,不包含主機名,如: http://www.fzr.com/abc/bbs/index.html?a=1&b=2中的/abc/bbs/index.php?a=1&b=2
$uri:這個變數指當前的請求URI,不包括任何引數,如: /abc/bbs/index.html
$document_uri: 與$uri相同, 這個變數指當前的請求URI,不包括任何傳遞引數,如:/abc/bbs/index.html
用瀏覽器訪問 http://www.fzr.com/100-200-100.html 或 http://www.fzr.com/100-100-100.html 跳轉到http://www.fzr.com頁面
4.4.2 實體操作:基于引數匹配的跳轉
(1)修改組態檔
![]()


(2)瀏覽器訪問測驗


4.5基于目錄下所有 php 結尾的檔案跳轉
要求訪問 http://www.fzr.com/upload/123.php 跳轉到首頁,
4.5.1 基于目錄下所有 php 結尾的檔案跳轉的操作步驟
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.fzr.com-access.log main; location ~* /upload/.*\.php$ { rewrite (.+) http://www.fzr.com permanent; } location / { root html; index index.html index.htm; } } systemctl restart nginx
瀏覽器訪問 http://www.fzr.com/upload/888.php 跳轉到http://www.fzr.com頁面,
4.5.2 實體操作:基于目錄下所有 php 結尾的檔案跳轉
(1)修改組態檔
![]()


(2)瀏覽器訪問測驗


4.6基于最普通一條 url 請求的跳轉
要求訪問一個具體的頁面如 http://www.fzr.com/abc/888.html 跳轉到首頁
4.6.1 基于最普通一條 url 請求的跳轉的操作步驟
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.fzr.com; #域名修改 charset utf-8; access_log /var/log/nginx/www.fzr.com-access.log; location ~* ^/abc/888.html { rewrite (.+) http://www.fzr.com permanent; } location / { root html; index index.html index.htm; } } systemctl restart nginx
瀏覽器訪問 http://www.fzr.com/abc/888.html 跳轉到http://www.fzr.com頁面,
4.6.2 實體操作:基于最普通一條 url 請求的跳轉
(1)修改組態檔



(2)瀏覽器訪問測驗


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/532592.html
標籤:其他
