感謝觀看
- 一,Rewrite跳轉場景
- 二, Rwrite跳轉需求的實作方式
- 三,命令語法
- 四,location的分類
- 五,location優先級
- 六,實作跳轉
- 1.基于域名跳轉
- 步驟一.開局的nginx配置:
- 步驟二:重啟服務nginx驗證
- 2.基于ip訪問跳轉;(用于維護網頁時,維護企業網站操作!)
- 步驟二:重啟服務驗證!
- 3.基于舊域名跳轉到新域名后面目錄
- 步驟一:在舊域名nginx組態檔中添加,跳轉;
- 步驟二:重啟服務驗證
- 4.基于引數配置跳轉
- 步驟一:在舊域名nginx組態檔中添加,跳轉;
- 步驟二:重啟服務驗證
- 5.基于目錄下所有php結尾的檔案跳轉
- 步驟一:在舊域名nginx組態檔中添加,跳轉;
- 步驟二:重啟服務nginx驗證登錄
- 6.基于最普通的一條URL請求跳轉
- 步驟一:在舊域名nginx組態檔中添加,跳轉;
- 步驟二:重啟服務驗證登錄
本文主要介紹Rewrite如何跳轉,和基于跳轉我們可以做哪些操作,
如:維護企業頁面時,企業舊域名更改新域名,如何舊域名跳轉到新域名…
一,Rewrite跳轉場景
- URL看起來更規范,合理
- 企業會將動態URL地址偽裝成靜態地址提供服務
- 網站換新域名后,讓舊的訪問跳轉到新的域名上
- 服務端某些業務的調整
二, Rwrite跳轉需求的實作方式
- Nginx跳轉需求的實作方式
rewrite進行匹配跳轉
if匹配全域變數后跳轉
location匹配再跳轉 - rewrite放在server{} ,if{}, location{}段中
location只對域名后邊的出去傳遞引數外的字串起作用 - 對域名或引數字串
使用if全域變數匹配
使用proxy_pass反向代理
三,命令語法
rewrite <regex> <rplacement> [flag];
正則 跳轉后的內容 rewrite支持的flag標記
四,location的分類
分類:
location = patt {} 精準匹配
location patt {} 一般匹配
location ~ patt {} 正則匹配
五,location優先級
1.=型別
2^~型別運算式
3.正則運算式(和*)型別
4.常規字串匹配型別,按前綴匹配
5.通用匹配(/),如果沒有其他匹配,任何請求都會匹配到
5.1比較rewrite和location
相同點
- 都能實作跳轉
不同點 - rewrite是在同一域名內更改獲取資源的路徑
- location是對一類路徑做控制訪問或反向代理,還可以
proxy_pass到其他機器
rewrite會寫在location里,執行順序 - 執行server塊里面的rewrite指令
- 執行location匹配
- 執行選定的location中的rewrite指令
六,實作跳轉
1.基于域名跳轉
步驟一.開局的nginx配置:
vi /usr/local/nginx/conf/nginx.conf
在server段內添加跳轉:
location / {
#root html;
#index index.html index.htm;
if ($host = 'www.as.top') {
rewrite ^/(.*)$ http://www.hgg.top permanent;
}
}
2.保存,驗證組態檔語法
nginx -t
步驟二:重啟服務nginx驗證
重啟服務;
systemctl restart nginx
瀏覽器:輸入舊域名www.as.top—自動跳轉新域名www.hgg.top

抓包研究:

2.基于ip訪問跳轉;(用于維護網頁時,維護企業網站操作!)
步驟一:nginx.conf添加配置;
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.as.top;
charset utf-8;
access_log /var/log/nginx/www.as.top.accesss.log;
set $rewrite ture;
if ($remote_addr = '20.0.0.1') {
set $rewrite false;
}
if ($rewrite = ture) {
rewrite (.+) /wh.html;
}
location = /wh.html {
root /usr/local/nginx/html/;
}
location / {
root html;
index index.html index.htm;
}
步驟二:重啟服務驗證!
管理員ip(允許的ip):
20.0.0.1的ip登錄的話,可以正常訪問網頁!

其他ip:
顯示咱們之前指定跳轉的wh.html 網頁檔案!!!

3.基于舊域名跳轉到新域名后面目錄
步驟一:在舊域名nginx組態檔中添加,跳轉;
vi /usr/local/nginx/conf/nginx.conf
server模塊下:
location /post { #一般匹配/post匹配條件
#root html;
#index index.html index.htm;
rewrite (.+) http://www.hgg.top/bbs$1 permanent;
}
步驟二:重啟服務驗證
重啟服務
輸入www.as.top/post 舊域名檔案
說明:因為新域名bbs目錄下咱們沒有創建,所以顯示不出來,但是可以看到網頁已經跳轉了;

4.基于引數配置跳轉
步驟一:在舊域名nginx組態檔中添加,跳轉;
vi /usr/local/nginx/conf/nginx.conf
在server段內,訪問日志下行添加;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.hgg.top permanent;
}
步驟二:重啟服務驗證
輸入www.as.top/100-或者100|200-和任意數字.html
驗證:輸入–www.as.top/100-100-100.html

5.基于目錄下所有php結尾的檔案跳轉
步驟一:在舊域名nginx組態檔中添加,跳轉;
vi /usr/local/nginx/conf/nginx.conf
在server段內:
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.hgg.top permanent;
}
驗證語法:nginx -t
步驟二:重啟服務nginx驗證登錄
重啟服務;
輸入www.as.top/upload/1.php
www.as.top/upload/as.php

6.基于最普通的一條URL請求跳轉
步驟一:在舊域名nginx組態檔中添加,跳轉;
vi /usr/local/nginx/conf/nginx.conf
location ~* /1/as.html {
rewrite (.+) http://www.hgg.top permanent;
}
步驟二:重啟服務驗證登錄
重啟服務
驗證登錄:輸入www.as.top/1/as.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/1345.html
標籤:其他
上一篇:理論+實驗:Nginx Rewrite(基于域名跳轉、ip地址跳轉、舊域名跳新域名、引數匹配跳轉、基于目錄php結尾檔案跳轉、基于url跳轉、報錯代碼跳轉)
