nginx 基礎5 rewrite 重新

1.開啓rewrite日誌

rewrite_log on; #http 段加入

error_log logs/xxxerror.log notice; #在將錯誤日誌級別調低

2.跳轉域名

 location / {
         rewrite / https://www.baidu.com;
        }

#表示,只要訪問這個域名直接跳轉到 baidu
nginx 基礎5 rewrite 重新
#查看日誌能看到記錄用"/"訪問了"111.com",跳轉到了"baidu"
修改下代碼

location /rewrite/ {
         rewrite / https://www.baidu.com;
        }

#表示只有用域名後面跟着"/rewrite/",文件夾纔會跳轉(只有/rewrite/纔會觸發跳轉,其他文件夾正常轉發)
如下圖
nginx 基礎5 rewrite 重新
#我這邊用"www.111.com/rewrite/123.com" 訪問才跳轉了

3.使用正則跳轉

例子1,
www.111.com/111/index.html 跳轉到 www.111.com/222/index.html

location / {
         rewrite ^/111/(.*)$  /222/$1 ;
        }

#"^"表示根的意思,就表示 www.111.com 的的意思,(.*) 匹配所有的意思,後面"$1"調用,
nginx 基礎5 rewrite 重新
#日誌,能就看出/111/index.html 跳轉到 /222/index.html

4.rewrite指令

last    終止在本location塊中處理接收到的URI,並將此處重寫的URI作爲新的URI使用其他location進行處理。(只是終止當前location的處理)
break   將此處重寫的URI作爲一個新的URI在當前location中繼續執行,並不會將新的URI轉向其他location。
redirect    將重寫後的URI返回個客戶端,狀態碼是302,表明臨時重定向,主要用在replacement字符串不以“http://”,“ https://”或“ $scheme” 開頭;
permanent   將重寫的URI返回客戶端,狀態碼爲301,指明是永久重定向;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章