一、Rewrite跳轉場景
1.1、URL看起來更規范、合理
1.2、企業會將動態URL地址偽裝成靜態地址提供服務
1.3、網站換新域名后,讓舊的訪問跳轉到新的域名上
1.4、服務端某些業務調整
二、Rewrite跳轉實現
三、Rewrite實際場景
3.1、Nginx跳轉需求的實現方式
3.1.1、使用rewrite進行匹配跳轉
3.1.2、使用if匹配全局變量后跳轉
3.1.3、使用location匹配再跳轉
3.2、rewrite放在server{},if{},location{}段中
3.2.1、location只對域名后邊的除去傳遞參數外的字符串起作用
3.3、對域名或參數字符串
3.3.1、使用if全局變量匹配
3.3.2、使用proxy_pass反向代理
四、Nginx正則表達式
常用的正則表達式元字符
字符 |
說明 |
^ |
匹配輸入字符串的起始位置 |
$ |
匹配輸入字符串的結束位置 |
* |
匹配前面的字符零次或多次 |
+ |
匹配前面的字符一次或多次 |
? |
匹配前面的字符零次或一次 |
. |
匹配除”\n”之外的任何單個字符 |
\ |
將后面接着的字符標記為一個特殊字符或一個原義字符或一個向后引用 |
\d |
匹配純數字 |
{n} |
重復n次 |
{n,} |
重復n次或更多次 |
[c] |
匹配單個字符c |
[a-z] |
匹配a-z小寫字母的任意一個 |
[a-zA-Z] |
匹配a-z小寫字母或A-Z大寫字母的任意一個 |
五、Rewrite命令
5.1、Rewrite命令語法
1 rewrite <regex> <replacement> [flag]; 2 <regex>:正則表達式 3 <replacement>:跳轉后的內容 4 [flag]rewrite支持的flag標記
5.2、flag標記說明
標記 |
說明 |
last |
相當於Apache的[L]標記,表示完成rewrite |
break |
本條規則匹配完成即終止,不再匹配后面的任何規則 |
redirect |
返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址,爬蟲不會更新url |
permanent |
返回301永久重定向,瀏覽器地址會顯示跳轉后的URL地址,爬蟲更新url |
1)last:url重寫后,馬上發起一個新請求,再次進入server塊,重試location匹配,超過10次匹配不到報500錯誤,地址欄不變
2)break:url重寫后,直接使用當前資源,不再使用location余下的語句,完成本次請求,地址欄不變
總結:last和break再重定向后,地址欄都不會發生變化,這是他們的相同點,不同點在於last會寫在server和if中,break是寫在location中,last不會終止重寫后的url匹配,break會終止重寫后的url匹配
六、location分類
6.1、分類
1 location = patt{} [精准匹配] 2 location patt {} [一般匹配] 3 location ~ patt {} [正則匹配]
6.2、正則匹配的常用表達式
標記 |
說明 |
~ |
執行一個正則匹配,區分大小寫 |
~* |
執行一個正則匹配,不區分大小寫 |
!~ |
執行一個正則匹配,區分大小寫不匹配 |
!~* |
執行一個正則匹配,不區分大小寫不匹配 |
^~ |
普通字符匹配,使用前綴匹配,如果匹配成功,則不再匹配其他location |
= |
普通字符精確匹配,也就是完全匹配 |
@ |
定義一個命名的location,使用再內部定向時 |
6.3、location優先級
6.3.1、相同類型的表達式,字符串長的會優先匹配
6.3.2、按優先級排列
1 = 類型 2 ^~ 類型表達式 3 正則表達式(~和~*)類型 4 常規字符串匹配類型,按前綴匹配 5 通用匹配(/),如果沒有其他匹配,任何請求都會匹配到
6.3.3、location優先級的示例1
1 location = / { ①精確匹配/,主機名后面不能帶任何字符串 2 [configuration A] 3 } 4 location ~ /documents/abc { ②匹配任何以/documents/abc開頭的地址,當后面的正則表達式沒有匹配到時,才會起作用 5 [configuration B] 6 } 7 location /documents/ { ③匹配任何以/documents/開頭的地址,當后面的正則表達式沒有匹配到時,才起作用 8 [configuration C] 9 } 10 location / { ④所有的地址都以/開頭,這條規則將匹配到所有請求,但正則表達式和最長字符會優先匹配 11 [configuration D] 12 }
1 location ^~ /images/ { ①以/images/開頭的地址,匹配符合后,停止往下匹配 2 [configuration E] 3 } 4 location ~* \.(gif|jpg|jpeg)$ { ②匹配所有以gif,jpg或jpeg結尾的請求,因為^~的優先級最高 5 [configuration F] 6 } 7 location ~ /images/abc { ③以/images/abc開頭的,優先級次之 8 [configuration G] 9 } 10 location /images/abc/1.html { ④如果和正則~ /images/abc/1.html相比,正則優先級最高 11 [configuration H] 12 } 13 location /images/abc { ⑤最長字符匹配到/images/abc,優先級最低 14 [configuration I] 15 }
6.4、比較rewrite和location
6.4.1、相同點:都能實現跳轉
6.4.2、不同點:
①、rewrite是在同一域名內更改獲取資源的路徑
②、location是對一類路徑控制訪問或反向代理,還可以proxy_pass到其他機器
6.4.3、rewrite會寫在location里,執行順序
①、執行server塊里面的rewrite指令
②、執行location匹配
③、執行選定的location中的rewrite指令
6.5、location優先級規則
6.5.1、匹配某個具體文件
(location = 完整路徑)>(location ^~ 完整路徑)>(location ~* 完整路徑)=(location ~ 完整路徑)>(location /)
6.5.2、用目錄做匹配訪問某個文件
(location = 目錄)>(location ^~ 目錄)>(location ~* 目錄)=(location ~ 目錄)>(location /)
七、實例說明
7.1、基於域名的跳轉
公司舊域名www.aaa.com,因業務需求有變更,需要使用新域名www.bbb.com代替
不能廢除舊域名
從舊域名跳轉到新域名,且保持其參數不變
7.1.1、修改新站點配置文件(基於LNMP架構),如果沒有PHP,可以直接創建index.html文件進行訪問
1 vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 80; 4 server_name www.aaa.com; 5
6 charset utf-8; 7
8 access_log logs/host.access.log main; 9 if ($host = 'www.aaa.com') #添加rewrite功能
10 {
11 rewrite ^/(.*)$ http://www.bbb.com/$1 permanent;
12 } 13 省略部分內容 14 }
7.1.2、在配置文件末尾添加新域名www.bbb.com的站點位置(保證添加的內容在http括號內)
1 server { 2 listen 80; 3 server_name www.bbb.com; 4
5 charset utf-8; 6
7 access_log /var/log/www.bbb.com.access.log main; 8 location / { 9 root /usr/local/share/html; 10 index index.html index.htm; 11 } 12 location ~ \.php$ { 13 root /usr/local/share/html; 14 fastcgi_pass 127.0.0.1:9000; 15 fastcgi_index index.php; 16 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 17 include fastcgi.conf; 18 }
7.1.3、重啟nginx
1 nginx -t #查看配置文件語法 2 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok #輸出語法內容 3 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful #輸出文件配置是否正常 4 systemctl restart nginx #重啟
7.1.4、創建新站點的網頁
1 mkdir -p /usr/local/share/html/php 2 vi /usr/local/share/html/php/index.php 3 <?php 4 phpinfo(); 5 ?>
7.1.5、客戶端測試
注:只要是www.bbb.com可以訪問的網頁都是可以跳轉的
7.2、基於客戶端IP訪問跳轉
今天公司業務版本上線,所有IP訪問任何內容都顯示一個固定維護頁面,只有公司IP訪問正常
7.2.1、修改默認站點配置文件
1 vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 80; 4 server_name www.aaa.com; 5 set $rewrite true; 6 if ($remote_addr = "20.0.0.10"){ 7 set $rewrite false; 8 } 9 if ($rewrite = true){ 10 rewrite (.+) /maintenance.html; 11 } 12 location = /maintenance.html { 13 root /usr/local/nginx/html; 14 } 15 省略部分內容 16 }
7.2.2、將上個項目里的末尾添加的內容刪除,修改重定向的網頁
1 vi /usr/local/nginx/html/maintenance.html 2 <html><body><h1>website is maintaing,please visit later</h1></body></html>
7.2.3、重啟並測試
1 nginx -t 2 systemctl restart nginx
7.3、基於舊、新域名跳轉並加目錄
將域名http://www.aaa.com下面的發帖都跳轉到http://www.bbb.com/bbs,且域名跳轉后保持參數不變
7.3.1、修改默認站點配置文件
1 vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 80; 4 server_name www.aaa.com; 5
6 charset utf-8; 7
8 access_log logs/host.access.log main; 9 location /post 10 { 11 rewrite (.+) http://www.bbb.com/bbs$1 permanent;
12 } 13 location / { 14 root html; 15 index index.html index.htm; 16 } 17 省略部分內容 18 } 19 下面的內容在末尾添加(保證添加的內容在http括號內) 20 server { 21 listen 80; 22 server_name www.bbb.com; 23
24 charset utf-8; 25
26 access_log logs/host.access.log main; 27 location /
28 { 29 root /usr/local/share/html; 30 index index.html index.htm; 31 } 32 }
7.3.2、創建bbs目錄
1 cd /usr/local/share/html/
2 mkdir bbs 3 mv index.html bbs/
4 cd bbs/
5 mkdir post 6 mv index.html post/
7.3.3、重啟並測試
1 nginx -t 2 systemctl restart nginx
7.4、基於參數匹配的跳轉
7.4.1、修改默認找點配置文件
1 vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 80; 4 server_name www.aaa.com; 5 if ($request_uri ~ ^/100-(100|200)-(\d+).html$) 6 { 7 rewrite (.+) http://www.aaa.com permanent;
8 } 9
10 charset utf-8; 11
12 access_log logs/host.access.log main; 13 location / { 14 root html; 15 index index.html index.htm; 16 } 17 省略部分內容 18 }
7.4.2、重啟並測試
1 nginx -t 2 systemctl restart nginx
7.5、基於目錄下所有php文件跳轉
7.5.1、修改默認站點配置文件
1 vi /usr/local/nginx/conf/nginx.conf 2 server { 3 listen 80; 4 server_name www.aaa.com; 5 location ~* /upload/.*\.php$ 6 { 7 rewrite (.+) http://www.aaa.com permanent;
8 } 9
10 charset utf-8; 11
12 access_log logs/host.access.log main; 13 location / { 14 root html; 15 index index.html index.htm; 16 } 17 省略部分內容 18 }
7.5.2、重啟並測試
1 nginx -t
2 systemctl restart nginx