nginx中重定向的實現(xiàn)
一、location
1、 location匹配
location匹配的就是后面的URI
/wordpress
192.168.100.11/wordpress
2、 location匹配的分類
2.1 精確匹配
location = / 對字符串進行完全匹配,必須完全符合
2.2 正則匹配
^~ 前綴匹配,以什么為開頭
~ 區(qū)分大小寫的匹配
~* 不區(qū)分大小寫
!~:區(qū)分大小寫的取反
!~*:不區(qū)分大小寫的取反
2.3 一般匹配(通用匹配)
location /字符串
3、location匹配優(yōu)先級*
精確匹配的優(yōu)先級最高、其次正則,最后一般匹配
3.1 匹配優(yōu)先級實驗
systemctl stop firewalld
setenforce 0
systemctl restart nginx
cd /usr/local/nginx/html/
把照片拖進去
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 #查看只有一個static1
mkdir static2 static3 #查看此時有static1 static2 static3
cd /usr/local/nginx/static1/
把1.jpg拖進去
cd /usr/local/nginx/static2/
把2.jpg拖進去
cd /usr/local/nginx/static3/
把3.jpg拖進去
[root@localhost static3]# mv 3.jpg 1.jpg
[root@localhost static3]# ls
1.jpg
頁面訪問
192.168.100.11/1.jpg # 此時訪問的是1.jpg的圖片
頁面訪問
把精確匹配注釋掉
nginx -t
systemctl restart nginx
192.168.100.11/1.jpg # 此時訪問的是3.jpg的圖片
3.2 優(yōu)先級總結(jié)
location = 完整路徑 = 1.jpg即完整的一個字也不能少
location ^~、location ~ location ~* 、location /(字符串)部分起始位置、location /
4、實際網(wǎng)站中的使用規(guī)則
第一個必選規(guī)則
網(wǎng)站首頁用精確等于杠(= /)
1.1 網(wǎng)站首頁
location = / {
root html;
index index.html index.htm index.php;
}
# = / 表示家目錄的開頭,訪問= /相當于訪問網(wǎng)站首頁
第二個必選規(guī)則
處理靜態(tài)請求的頁面
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;
}
第三個必須規(guī)則
一般是通用規(guī)則,用來轉(zhuǎn)發(fā).php .js為后綴的動態(tài)請求到后端服務器(數(shù)據(jù)庫)
1.4 轉(zhuǎn)發(fā)后端請求和負載均衡
location / {
proxy_pass
}
二、rewrite重定向
1、概念
rewrite就是把當前訪問得頁面跳轉(zhuǎn)到其他頁面。
2、工作方式
通過nginx的全局變量或者是自定義變量,結(jié)合正則表達式和標志位實現(xiàn)url的重定向。
3、nginx的變量
$uri:客戶端請求的url地址
$host:請求的主機名
#http_user_agent:客戶端請求的瀏覽器和操作系統(tǒng)
$http_referer:請求頭的referer信息,表示當前頁面來源的url
$remote_addr:客戶端的ip地址
$remote_port:客戶端的端口
$server_addr:服務端的ip地址
$server_port:服務端的端口
$request_method:獲取客戶端請求的方法
$scheme:請求的協(xié)議,要么是http要么是https
x_forwarded_for:用來獲取請求頭當中客戶端的真實ip地址。代理服務器添加,在代理服務器當中只是客戶端的ip地址。
X_Real_IP:客戶端的真實ip地址
4、nginx真實ip地址訪問設置
vim nginx.conf proxy_set_header X Real-IP $remote_addr # 加上這字段,客戶端的真實ip地址就會傳遞給后端服務器
操作
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
#此時顯示的是本機地址 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、標志位(flag)
permanent:永久重定向,返回碼是301,瀏覽器地址欄會顯示跳轉(zhuǎn)后的url地址
redirect:臨時重定向,返回碼是302,瀏覽器地址欄會顯示跳轉(zhuǎn)后的url地址
break:永久重定向,返回碼是301,但是她匹配到規(guī)則之后不會再向下匹配其他規(guī)則,url也不會發(fā)生變化
last:重定向,但是會繼續(xù)向下匹配其他的location規(guī)則
6、rewrite的執(zhí)行順序
6.1 server模塊的rewrite的優(yōu)先級最高
6.2 匹配location的規(guī)則
6.3 執(zhí)行選定的location規(guī)則
7、rewrite的語法
rewrite 正則表達式 跳轉(zhuǎn)后的內(nèi)容 標志位;
7.1 實驗
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
此時再打開一臺192.168.100.11的shell終端
[root@localhost opt]# tail -f /usr/local/nginx/logs/error.log
頁面繼續(xù)訪問 192.168.100.11/test1
xshell報錯中出現(xiàn)以下一句話:rewrite or internal redirection cycle while processing
解釋:
# 表示在重定向的過程中,使用了last方式進行重定向,但是,沒有結(jié)束語,陷入了死循環(huán),nginx會自動循環(huán)10次,last匹配最多只能執(zhí)行10次,超過10次沒有結(jié)束,就會停止,然后報錯500。
7.2 基于域名進行跳轉(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
頁面訪問(在虛擬機)
www.xy102.com
# 打印結(jié)果為 this is new page 網(wǎng)頁地址跳轉(zhuǎn)www.xiaodai.com
7.3 基于客戶端的ip進行跳轉(zhuǎn),公司有新業(yè)務上線,測試階段,其他ip只能顯示維護中,只有192.1168.100.11能正常訪問
vim /usr/local/nginx/cong/ngin.conf
在server模塊的access_log下添加
set $rewrite true;
# 設置一個變量名,rewrite,值是ture
# 來進行判斷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)頁維護中" > error.html
nginx -t
systemctl restart nginx
頁面訪問
192.168.100.11 #打印結(jié)果為 頁面維護中
總結(jié)·:
location匹配的優(yōu)先級
到此這篇關(guān)于nginx中重定向的實現(xiàn)的文章就介紹到這了,更多相關(guān)nginx 重定向內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后
相關(guān)文章
?Nginx Ingress網(wǎng)關(guān)吞吐量極限測試全過程
本文介紹了如何進行NginxIngress的吞吐量極限測試,涵蓋工具選擇、指標定義、配置優(yōu)化及實戰(zhàn)步驟,旨在幫助團隊精準評估系統(tǒng)性能邊界,確保高并發(fā)場景下的系統(tǒng)穩(wěn)定性和可靠性2026-01-01
Nginx如何根據(jù)前綴路徑轉(zhuǎn)發(fā)到不同的Flask服務
這篇文章主要介紹了Nginx如何根據(jù)前綴路徑轉(zhuǎn)發(fā)到不同的Flask服務,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
centos7系統(tǒng)下nginx安裝并配置開機自啟動操作
這篇文章主要介紹了centos7系統(tǒng)下nginx安裝并配置開機自啟動操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
nginx實現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請求的實戰(zhàn)經(jīng)歷
這篇文章主要給大家介紹了一次關(guān)于nginx實現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請求的實戰(zhàn)經(jīng)歷,文中通過示例代碼介紹的非常詳細,對大家學習或者使用nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-11-11

