欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx中重定向的實現(xiàn)

 更新時間:2025年03月02日 10:48:11   作者:Jessica小戴  
本文主要介紹了Nginx中l(wèi)ocation匹配和rewrite重定向的規(guī)則與應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、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)先級

到此這篇關于nginx中重定向的實現(xiàn)的文章就介紹到這了,更多相關nginx 重定向內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后

相關文章

  • 升級nginx支持HTTP/2服務端推送的方法

    升級nginx支持HTTP/2服務端推送的方法

    這篇文章主要介紹了升級nginx支持HTTP/2服務端推送的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 使用nginx做負載均衡的模塊解讀

    使用nginx做負載均衡的模塊解讀

    這篇文章主要介紹了使用nginx做負載均衡的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • 使用Nginx實現(xiàn)灰度發(fā)布的使用

    使用Nginx實現(xiàn)灰度發(fā)布的使用

    這篇文章主要介紹了使用Nginx實現(xiàn)灰度發(fā)布的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • centos系統(tǒng)下LNMP環(huán)境一鍵安裝

    centos系統(tǒng)下LNMP環(huán)境一鍵安裝

    centos下的LNMP環(huán)境一鍵安裝實現(xiàn)方法,需要的朋友可以參考下。
    2010-06-06
  • 通過nginx反向代理來調(diào)試代碼的實現(xiàn)

    通過nginx反向代理來調(diào)試代碼的實現(xiàn)

    這篇文章主要介紹了通過nginx反向代理來調(diào)試代碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • Nginx搭建https服務器教程

    Nginx搭建https服務器教程

    這篇文章主要為大家詳細介紹了Nginx搭建https服務器教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Nginx的try_files指令使用實例

    Nginx的try_files指令使用實例

    這篇文章主要介紹了Nginx的try_files指令使用實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Nginx服務器添加Systemd自定義服務過程解析

    Nginx服務器添加Systemd自定義服務過程解析

    這篇文章主要介紹了Nginx服務器添加Systemd自定義服務過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • nginx做白名單和限流的完整過程

    nginx做白名單和限流的完整過程

    ??我們都知道nginx里面是可以用lua腳本做一些稍微復雜些的邏輯處理的,要使用lua腳本需要編譯lua解釋器,時間有限我直接用了openresty,它集成了lua和nginx,這篇文章主要介紹了nginx做白名單和限流,需要的朋友可以參考下
    2024-02-02
  • nginx訪問控制的兩種方法

    nginx訪問控制的兩種方法

    這篇文章主要介紹了關于nginx訪問控制的兩種方法,一種是基于Basic Auth認證,另一種是基于IP的訪問控制,文中介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03

最新評論