nginx中重定向的實(shí)現(xiàn)
一、location
1、 location匹配
location匹配的就是后面的URI
/wordpress
192.168.100.11/wordpress
2、 location匹配的分類
2.1 精確匹配
location = / 對(duì)字符串進(jìn)行完全匹配,必須完全符合
2.2 正則匹配
^~
前綴匹配,以什么為開頭
~
區(qū)分大小寫的匹配
~*
不區(qū)分大小寫
!~
:區(qū)分大小寫的取反
!~*
:不區(qū)分大小寫的取反
2.3 一般匹配(通用匹配)
location /字符串
3、location匹配優(yōu)先級(jí)*
精確匹配的優(yōu)先級(jí)最高、其次正則,最后一般匹配
3.1 匹配優(yōu)先級(jí)實(shí)驗(yàn)
systemctl stop firewalld setenforce 0 systemctl restart nginx cd /usr/local/nginx/html/ 把照片拖進(jìn)去 vim /usr/local/nginx/conf/nginx.conf location = /1.jpg { root /data/nginx/static1; index index.html; } location /1.jpg { root /data/nginx/static2; index index.html; } location ~*\.(jpglgif)$ { root /data/nginx/static3; index index.html; } wq! nginx -t systemctl restart nginx mkdir -p /data/nginx/static1 cd /data/nginx #查看只有一個(gè)static1 mkdir static2 static3 #查看此時(shí)有static1 static2 static3 cd /usr/local/nginx/static1/ 把1.jpg拖進(jìn)去 cd /usr/local/nginx/static2/ 把2.jpg拖進(jìn)去 cd /usr/local/nginx/static3/ 把3.jpg拖進(jìn)去 [root@localhost static3]# mv 3.jpg 1.jpg [root@localhost static3]# ls 1.jpg 頁面訪問 192.168.100.11/1.jpg # 此時(shí)訪問的是1.jpg的圖片 頁面訪問 把精確匹配注釋掉 nginx -t systemctl restart nginx 192.168.100.11/1.jpg # 此時(shí)訪問的是3.jpg的圖片
3.2 優(yōu)先級(jí)總結(jié)
location = 完整路徑 = 1.jpg即完整的一個(gè)字也不能少
location ^~
、location ~
location ~*
、location /(字符串)部分起始位置、location /
4、實(shí)際網(wǎng)站中的使用規(guī)則
第一個(gè)必選規(guī)則
網(wǎng)站首頁用精確等于杠(= /)
1.1 網(wǎng)站首頁
location = / { root html; index index.html index.htm index.php; } # = / 表示家目錄的開頭,訪問= /相當(dāng)于訪問網(wǎng)站首頁
第二個(gè)必選規(guī)則
處理靜態(tài)請(qǐng)求的頁面
1.2 用來匹配靜態(tài)頁面
location ^~ /static/ { root /web/static/; index index.html index.htm; }
1.3 訪問圖片或者指定的后綴名
location ~* \.(jpg|gif|jpeg|png|css)$ { root /web/pictures/; index index.html index.htm; }
第三個(gè)必須規(guī)則
一般是通用規(guī)則,用來轉(zhuǎn)發(fā).php .js為后綴的動(dòng)態(tài)請(qǐng)求到后端服務(wù)器(數(shù)據(jù)庫(kù))
1.4 轉(zhuǎn)發(fā)后端請(qǐng)求和負(fù)載均衡
location / { proxy_pass }
二、rewrite重定向
1、概念
rewrite就是把當(dāng)前訪問得頁面跳轉(zhuǎn)到其他頁面。
2、工作方式
通過nginx的全局變量或者是自定義變量,結(jié)合正則表達(dá)式和標(biāo)志位實(shí)現(xiàn)url的重定向。
3、nginx的變量
$uri
:客戶端請(qǐng)求的url地址
$host
:請(qǐng)求的主機(jī)名
#http_user_agent
:客戶端請(qǐng)求的瀏覽器和操作系統(tǒng)
$http_referer
:請(qǐng)求頭的referer信息,表示當(dāng)前頁面來源的url
$remote_addr
:客戶端的ip地址
$remote_port
:客戶端的端口
$server_addr
:服務(wù)端的ip地址
$server_port
:服務(wù)端的端口
$request_method
:獲取客戶端請(qǐng)求的方法
$scheme
:請(qǐng)求的協(xié)議,要么是http要么是https
x_forwarded_for
:用來獲取請(qǐng)求頭當(dāng)中客戶端的真實(shí)ip地址。代理服務(wù)器添加,在代理服務(wù)器當(dāng)中只是客戶端的ip地址。
X_Real_IP
:客戶端的真實(shí)ip地址
4、nginx真實(shí)ip地址訪問設(shè)置
vim nginx.conf proxy_set_header X Real-IP $remote_addr # 加上這字段,客戶端的真實(shí)ip地址就會(huì)傳遞給后端服務(wù)器
操作
vim /usr/local/nginx/conf/nginx.conf 在server模塊修改,修改如下 location / { root html; default_type text/plain; return 200 "ip:$remote_addr"; } wq! systemctl restart nginx 頁面訪問 192.168.100.11 #此時(shí)顯示的是本機(jī)地址 ip 192.168.100.1 # return 200 "ip:$remote_addr\ntest1:$host\nport:$remote_port\nxieyi:$scheme"; systemctl restart nginx 頁面訪問 # ip:192.168.100.1 # test1:192.168.100.11 # port:51360 # xieyi:http
5、標(biāo)志位(flag)
permanent
:永久重定向,返回碼是301,瀏覽器地址欄會(huì)顯示跳轉(zhuǎn)后的url地址
redirect
:臨時(shí)重定向,返回碼是302,瀏覽器地址欄會(huì)顯示跳轉(zhuǎn)后的url地址
break
:永久重定向,返回碼是301,但是她匹配到規(guī)則之后不會(huì)再向下匹配其他規(guī)則,url也不會(huì)發(fā)生變化
last
:重定向,但是會(huì)繼續(xù)向下匹配其他的location規(guī)則
6、rewrite的執(zhí)行順序
6.1 server模塊的rewrite的優(yōu)先級(jí)最高
6.2 匹配location的規(guī)則
6.3 執(zhí)行選定的location規(guī)則
7、rewrite的語法
rewrite 正則表達(dá)式 跳轉(zhuǎn)后的內(nèi)容 標(biāo)志位;
7.1 實(shí)驗(yàn)
cd /usr/local/nginx/html mkdir test1 mkdir xy102 cd /usr/local/nginx/html/test1 echo 123 > index.html cd /usr/local/nginx/html/xy102 echo 456 > index.html # 查看一下test1和xy102目錄下的index.html是否創(chuàng)建 (1) vim /usr/local/nginx/conf/nginx.conf 在server模塊修改,修改如下 location / { root html; rewrite /test1/(.*) /xy102/$1 permanent; #192.168.100.11/test1/index.html 192.168.100.11/xy102/index.html $1就是捕獲組 index index.html; } wq! nginx -t systemctl restart nginx 頁面訪問 192.168.100.11/test1 # 訪問結(jié)果為456,網(wǎng)站ip跳轉(zhuǎn)為192.168.100.11/xy102 (2) vim /usr/local/nginx/conf/nginx.conf 在server模塊修改,修改如下 location /test1 { root html; rewrite /test1/(.*) /test2/$1 last; index index.html; } location /test2 { root html; rewrite /test2/(.*) /test1/$1 last; index index.html; } wq! nginx -t systemctl restart nginx cd /usr/local/nginx/html mkdir test2 cd /usr/local/nginx/html/test2 echo 789 > index.html 頁面訪問 192.168.100.11/test1 # 訪問結(jié)果為500 此時(shí)再打開一臺(tái)192.168.100.11的shell終端 [root@localhost opt]# tail -f /usr/local/nginx/logs/error.log 頁面繼續(xù)訪問 192.168.100.11/test1 xshell報(bào)錯(cuò)中出現(xiàn)以下一句話:rewrite or internal redirection cycle while processing 解釋: # 表示在重定向的過程中,使用了last方式進(jìn)行重定向,但是,沒有結(jié)束語,陷入了死循環(huán),nginx會(huì)自動(dòng)循環(huán)10次,last匹配最多只能執(zhí)行10次,超過10次沒有結(jié)束,就會(huì)停止,然后報(bào)錯(cuò)500。
7.2 基于域名進(jìn)行跳轉(zhuǎn),老的不用了的域名,但是依然可以訪問,統(tǒng)統(tǒng)跳轉(zhuǎn)到新的域名
vim /usr/local/nginx/conf/nginx.conf 在server模塊修改,修改如下 # 修改 server_name如下 server_name www.xy102.com; # 取消charset koi8-r;的注釋,修改如下 charset ulf-8; location / { root html; if ( $host = 'www.xy102.com' ) { rewrite ^/(.*)$ http://www.xiaodai.com/$1 permanent; } index index.html; } wq! nginx -t systemctl restart nginx 做域名映射 vim /etc/hosts 192.168.100.11 www.xy102.com www.xiaodai.com wq! # 訪問www.xy102.com/index.html就是訪問www.xiaodai.com/index.html cd /usr/local/nginx/html echo "this is new page" > index.html 頁面訪問(在虛擬機(jī)) www.xy102.com # 打印結(jié)果為 this is new page 網(wǎng)頁地址跳轉(zhuǎn)www.xiaodai.com
7.3 基于客戶端的ip進(jìn)行跳轉(zhuǎn),公司有新業(yè)務(wù)上線,測(cè)試階段,其他ip只能顯示維護(hù)中,只有192.1168.100.11能正常訪問
vim /usr/local/nginx/cong/ngin.conf 在server模塊的access_log下添加 set $rewrite true; # 設(shè)置一個(gè)變量名,rewrite,值是ture # 來進(jìn)行判斷ip是否是合法ip if ( $remote_addr = "192.168.100.11" ) { set $rewrite false; } if ( $rewrite = true ) { rewrite (.+) /error.html; # 重定向,192.168.100.13/error.html } location = /error.html { root html; } wq! cd /usr/local/nginx/html echo "網(wǎng)頁維護(hù)中" > error.html nginx -t systemctl restart nginx 頁面訪問 192.168.100.11 #打印結(jié)果為 頁面維護(hù)中
總結(jié)·:
location匹配的優(yōu)先級(jí)
到此這篇關(guān)于nginx中重定向的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nginx 重定向內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后
- nginx 老網(wǎng)站域名重定向到新網(wǎng)站的方法(親測(cè))
- 在Nginx中實(shí)現(xiàn)URL重寫與重定向
- Nginx地址重定向的實(shí)現(xiàn)
- nginx的服務(wù)配置及重定向問題
- nginx重寫和重定向關(guān)系與配置方式
- nginx重定向解決(rewrite or internal redirection cycle)
- Nginx重定向后請(qǐng)求參數(shù)丟失的原因分析及解決方案
- nginx反向代理后無限重定向的問題解決方法
- Nginx中rewrite(地址重定向)的深入剖析
- 利用Nginx實(shí)現(xiàn)URL重定向的簡(jiǎn)單方法
- 配置nginx 重定向到系統(tǒng)維護(hù)頁面
相關(guān)文章
升級(jí)nginx支持HTTP/2服務(wù)端推送的方法
這篇文章主要介紹了升級(jí)nginx支持HTTP/2服務(wù)端推送的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05使用Nginx實(shí)現(xiàn)灰度發(fā)布的使用
這篇文章主要介紹了使用Nginx實(shí)現(xiàn)灰度發(fā)布的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03centos系統(tǒng)下LNMP環(huán)境一鍵安裝
centos下的LNMP環(huán)境一鍵安裝實(shí)現(xiàn)方法,需要的朋友可以參考下。2010-06-06通過nginx反向代理來調(diào)試代碼的實(shí)現(xiàn)
這篇文章主要介紹了通過nginx反向代理來調(diào)試代碼的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Nginx服務(wù)器添加Systemd自定義服務(wù)過程解析
這篇文章主要介紹了Nginx服務(wù)器添加Systemd自定義服務(wù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11