rewrit的專案心得與總結
一、rewrite跳轉場景
1.1、URL看起來更規範、合理
1.2、企業會將動態URL地址偽裝成靜態地址提供服務
1.3、網站換新域名後,讓舊的訪問跳轉到新的域名上
1.4、服務端某些業務調整
二、rewrite的跳轉實現
三、rewrite的實現場景
1、Nginx跳轉需求的實現方式
2、使用rewrite進行匹配跳轉
3、使用if匹配全域性變數後跳轉
4、使用location匹配再跳轉
5、rewrite放在server{},if{},location{}段中
6、location只對域名後邊的除去傳遞引數外的字串起作用
7、對域名或引數字串
8、使用if全域性變數匹配
9、使用proxy_pass反向代理
四、rewrite命令
1 rewrite <regex> <replacement> [flag]; 2 <regex>:正則表示式 3 <replacement>:跳轉後的內容 4 [flag]rewrite支援的flag標記
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的分類
1 location = patt{} [精準匹配] 2 location patt {} [一般匹配] 3 location ~ patt {} [正則匹配]
正則匹配的常用表示式
標記 |
說明 |
~ |
執行一個正則匹配,區分大小寫 |
~* |
執行一個正則匹配,不區分大小寫 |
!~ |
執行一個正則匹配,區分大小寫不匹配 |
!~* |
執行一個正則匹配,不區分大小寫不匹配 |
^~ |
普通字元匹配,使用字首匹配,如果匹配成功,則不再匹配其他location |
= |
普通字元精確匹配,也就是完全匹配 |
@ |
定義一個命名的location,使用再內部定向時 |
location的優先順序
相同型別的表示式,字串長的會優先匹配
按優先順序排列
1 = 型別 2 ^~ 型別表示式 3 正則表示式(~和~*)型別 4 常規字串匹配型別,按字首匹配 5 通用匹配(/),如果沒有其他匹配,熱河請求都會匹配到
location優先順序的示例
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 } 13location ^~ /images/ { ①以/images/開頭的地址,匹配符合後,停止往下匹配 14[configuration E] 15} 16ocation ~* \.(gif|jpg|jpeg)$ { ②匹配所有以gif,jpg或jpeg結尾的請求,因為^~的優先順序最高 17[configuration F] 18} 19location ~ /images/abc { ③以/images/abc開頭的,優先順序次之 20[configuration G] 21} 22 location /images/abc/1.html { ④如果和正則~ /images/abc/1.html相比,正則優先順序最高 23 [configuration H] 24 } 25 location /images/abc { ⑤最長字元匹配到/images/abc,優先順序最低 26 [configuration I] 27 }
六、對location和rewrite進行比較
1、相同點:都能實現跳轉
2、不同點:rewrite是在同一個域名內更改獲取資源的路徑
location是對一類路徑控制訪問或反向代理,還可以proxy_pass到其他機器
3、rewrite會寫在location裡,執行順序
①、執行server快裡面的rewrite指令
②、執行location匹配
③、執行選定的location中的rewrite指令
4、location優先順序規則
匹配某個具體檔案
(location = 完整路徑)>(location ^~ 完整路徑)>(location ~* 完整路徑)=(location ~ 完整路徑)>(location /)
用目錄做匹配訪問某個檔案
(location = 目錄)>(location ^~ 目錄)>(location ~* 目錄)=(location ~ 目錄)>(location /)
六、例項說明
1、基於域名的跳轉
公司的舊域名www.old.com 因為業務需求變更,需要使用新域名www.new.com代替
不能廢除舊域名
從舊域名跳轉到新域名,切保持其引數不變
2、修改新站點配置檔案(基於LNMP)
vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.old.com; charset utf-8; access_log logs/host.access.log main; if ($host = 'www.old.com') { rewrite ^/(.*)$ http://www.new.com/$1 permanent; } location / { root /usr/local/share/html; index index.html index.htm;
檢查nginx配置檔案並重啟nginx
[root@server3 ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server3 ~]# systemctl restart nginx
建立新站點的網頁,新增主機對映
vi /etc/hosts 20.0.0.30 www.old.com www.new.com [root@server3 ~]# mkdir -p /usr/local/share/html [root@server3 ~]# cd /usr/local/share/html/ [root@server3 html]# vi index.html [root@server3 html]# vi /usr/local/nginx/conf/nginx.conf <html><body><h1>this is new</h1></body></html>
客戶端測試
2、基於客戶端IP訪問跳轉
今天我們公司新業務版本上線。所有ip訪問任何內容都會顯示一個正在維護的頁面,只有公司的ip才能正常訪問,
修改配置檔案
vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.old.com; set $rewrite true; if ($remote_addr = "20.0.0.30"){ set $rewrite false; } if ($rewrite = true){ rewrite (.+) /maintenance.html; } location = /maintenance.html { root /usr/local/nginx/html; }
檢查配置檔案,重啟nginx
nginx -t systemctl restart nginx
修改重定向的網頁
vi /usr/local/nginx/html/maintenance.html <html><body><h1>website is maintaing,please visit later</h1></body></html>
測試
3、基於新域名,舊域名跳轉並加目錄
修改配置檔案
vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.old.com; charset utf-8; access_log logs/host.access.log main; location /post { rewrite (.+) http://www.new.com/bbs$1 permanent; } 末尾新增 server { listen 80; server_name www.new.com; charset utf-8; access_log logs/host.access.log main; location / { root /usr/local/share/html; index index.html index.htm; } }
檢查配置檔案 並重啟nginx
nginx -t systemctl restart nginx
建立BBS目錄
cd /usr/local/share/html/ mkdir bbs mv index.html bbs/ cd bbs/ mkdir post mv index.html post
測試
4、基於引數匹配的跳轉
修改配置檔案
vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.old.com; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.+) http://www.old.com permanent; } #charset koi8-r; #access_log logs/host.access.log main;
j檢查配置檔案,重啟nginx
nginx -t systemctl restart nginx
進行測試