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

NGINX基于cookie針對同一域名進行分流轉發(fā)

 更新時間:2025年07月13日 11:58:16   作者:遇見火星  
本文介紹了利用NGINX基于cookie進行多環(huán)境分流的方法,通過在Docker中部署兩個后端NGINX容器,并在前端NGINX配置中設置map規(guī)則,根據(jù)cookie值將請求分發(fā)到不同后端,感興趣的可以了解一下

最新了解到的姿勢,結合著新接觸 Mac 電腦,第一次做實驗,學習之后,特別記錄一下。

1,說明

很多時候,測試環(huán)境可能會有好多套環(huán)境,這個時候,如果每套都配置一個對應的域名,會非常麻煩,但是很多時候針對這個問題似乎又沒有特別好的方案,新公司新氣象,學到新的思路是在 NGINX 層面基于 cookie 來進行不同環(huán)境的分流轉發(fā),今天就來做一下這個實驗。

2,環(huán)境準備

因為在新環(huán)境,還沒有個人自用的測試服務器,Mac 當中做實驗又不習慣,于是只能通過 docker 來進行了。

所以需要先安裝 docker 環(huán)境,這個就不在這里贅述了。

那么,docker 環(huán)境準備完畢之后,就可以開始實驗了,所謂,docker 在手,天下我有。

3,思路說明

首先跑兩個 NGINX 的容器,訪問之后會返回不同的結果,然后前端再添加一層 NGINX,代理所有的外部請求,根據(jù) cookie 的不同,分發(fā)到不同的后端容器去。

4,開始操作

1,先啟兩個后端容器

準備工作:

$ mkdir -p /Users/liqilong/docker/nginx
$ cd /Users/liqilong/docker/nginx
$ mkdir  test1 test2
$ echo test1 > test1/index.html
$ echo test2 > test2/index.html

啟動容器:

$ docker pull daocloud.io/library/nginx:1.15.9-alpine-perl
$ docker run --name test1 -v /Users/liqilong/docker/nginx/test1:/usr/share/nginx/html:ro -d -p 8080:80  daocloud.io/library/nginx:1.15.9-alpine-perl
$ docker run --name test2 -v /Users/liqilong/docker/nginx/test2:/usr/share/nginx/html:ro -d -p 8081:80  daocloud.io/library/nginx:1.15.9-alpine-perl

訪問驗證:

$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 18:65:90:cc:52:a5
    inet6 fe80::1cf4:9734:2fa8:8234%en0 prefixlen 64 secured scopeid 0x5
    inet 172.16.29.170 netmask 0xfffffc00 broadcast 172.16.31.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
$ curl 172.16.29.170:8080
test1
$ curl 172.16.29.170:8081
test2

2,再啟動一個前端 NGINX

因為要做一些相對的配置工作,我這里就用了自己配置的 centos 鏡像來做了,事實上仍舊可以利用剛剛那個 NGINX 鏡像來做接下來的實驗。

$ docker pull registry.cn-hangzhou.aliyuncs.com/eryajf/centos:7.4
$ docker run -itd --name eryajf registry.cn-hangzhou.aliyuncs.com/eryajf/centos:7.4

接下來的操作就是進入此容器內部進行了。

$ docker exec -it eryajf sh
sh-4.2# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sh-4.2# yum -y install nginx

添加如下 NGINX 配置:

cat >> /etc/nginx/conf.d/test.conf << EOF
upstream test01 {       #此處可以單獨寫,也可以寫在下邊map的內容中
    server 172.16.29.170:8080 weight=1 max_fails=1 fail_timeout=30s;
}
upstream test02 {
    server 172.16.29.170:8081 weight=1 max_fails=1 fail_timeout=30s;
}
upstream root {
    server 172.16.29.170:8080 weight=1 max_fails=1 fail_timeout=30s;
}
map $COOKIE_testenv $group {    #$COOKIE_testenv的前半部分$COOKIE_是固定格式,后邊的testenv則是cookie的key,$group是別名
    test1 test01;   #表示cookie的value=test1,則轉發(fā)給test1
    test2 test02;
    default root;
}
server {
    listen 81;
    server_name localhost;
    access_log  logs/access_log;
    error_log   logs/error_log;
    location / {
        proxy_pass http://$group$request_uri;   #注意此處url的拼接
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}
EOF

然后啟動 NGINX:

nginx -t
nginx

3,訪問測試

這個時候可以通過命令行來模擬請求,然后查看效果。

sh-4.2# curl localhost:81
test1
sh-4.2# curl localhost:81 --cookie "testenv=test1"
test1
sh-4.2# curl localhost:81 --cookie "testenv=test2"
test2

此處只要是有一個 cookie 名稱與內容是符合 nginx 定義的規(guī)則的,那么如上規(guī)則就是成立的。

sh-4.2# curl localhost:81 --cookie "testenv=test1;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=test2;user=root;pass=123"
test2

5,其他方面

另外除了上邊的比較固定的方式之外,還有比較靈活的控制方案,主要集中在 url 的匹配上。

1,匹配結尾關鍵字

需求就是匹配到 cookie 的指定結尾進行分流轉發(fā)。NGINX 配置如下:

map $COOKIE_testenv $group {
    ~*1$  172.16.29.170:8080;
    ~*2$  172.16.29.170:8081;
    default 172.16.29.170:8080;
}
server {
    listen 81;
    server_name localhost;
    access_log  logs/access_log;
    error_log   logs/error_log;
    location / {
        proxy_pass http://$group$request_uri;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

然后重新加載 NGINX 配置,請求一下驗證效果:

sh-4.2# curl localhost:81 --cookie "testenv=dfhg;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=dfhg1;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=dfhg2;user=root;pass=123"
test2

2,匹配開頭關鍵字

與上邊的道理是一致的,只不過配置內容更改一下即可。

map $COOKIE_testenv $group {
    ~*^1  172.16.29.170:8080;
    ~*^2  172.16.29.170:8081;
    default 172.16.29.170:8080;
}
server {
    listen 81;
    server_name localhost;
    access_log  logs/access_log;
    error_log   logs/error_log;
    location / {
        proxy_pass http://$group$request_uri;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

然后請求一下,驗證一下效果:

sh-4.2# curl localhost:81 --cookie "testenv=dfhg;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=1dfhg;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=2dfhg;user=root;pass=123"
test2

3,匹配包含關鍵字

還有一種比較靈活的策略,就是只要包含指定的關鍵字標識,就往不同的后端進行分流轉發(fā),配置如下:

map $COOKIE_testenv $group {
    ~*.*eryajf1.*  172.16.29.170:8080;
    ~*.*eryajf2.*  172.16.29.170:8081;
    default 172.16.29.170:8080;
}
server {
    listen 81;
    server_name localhost;
    access_log  logs/access_log;
    error_log   logs/error_log;
    location / {
        proxy_pass http://$group$request_uri;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

然后請求一下,驗證一下效果:

sh-4.2# curl localhost:81 --cookie "testenv=A3fklj;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=A3fkeryajf1lj;user=root;pass=123"
test1
sh-4.2# curl localhost:81 --cookie "testenv=A3fkeryajf2lj;user=root;pass=123"
test2

到此這篇關于NGINX基于cookie針對同一域名進行分流轉發(fā)的文章就介紹到這了,更多相關nginx cookie分流轉發(fā)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • nginx配置https的雙向認證方式

    nginx配置https的雙向認證方式

    文章主要介紹了如何配置Nginx雙向認證的詳細步驟,包括生成證書、配置Nginx等,并提供了個人經(jīng)驗供參考
    2024-11-11
  • CentOS 7.3.1611編譯安裝Nginx1.10.3+MySQL5.7.16+PHP7.1.2

    CentOS 7.3.1611編譯安裝Nginx1.10.3+MySQL5.7.16+PHP7.1.2

    這篇文章主要介紹了CentOS 7.3.1611編譯安裝Nginx1.10.3+MySQL5.7.16+PHP7.1.2,需要的朋友可以參考下
    2018-01-01
  • nginx請求時找路徑問題解決

    nginx請求時找路徑問題解決

    當你安裝了nginx的時候,為nginx配置了如下的location,想要去訪問路徑下面的內容,可是總是出現(xiàn)404,找不到文件,這是什么原因呢,今天我們就來解決這個問題,感興趣的朋友一起看看吧
    2023-10-10
  • 使用LDAP實現(xiàn)Nginx用戶認證的示例

    使用LDAP實現(xiàn)Nginx用戶認證的示例

    本文主要使用Nginx和LDAP實現(xiàn)用戶認證,通過配置Nginx和安裝nginx-auth-ldap模塊,可以實現(xiàn)基于LDAP的認證邏輯,下面就來介紹一下,感興趣的可以了解一下
    2024-12-12
  • nginx 目錄密碼保護的設置方法

    nginx 目錄密碼保護的設置方法

    比如要對 網(wǎng)站目錄下的 test 文件夾 進行加密認證
    2010-12-12
  • 詳解Nginx 出現(xiàn) 403 Forbidden 的解決辦法

    詳解Nginx 出現(xiàn) 403 Forbidden 的解決辦法

    本篇文章主要介紹了詳解Nginx 出現(xiàn) 403 Forbidden 的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 寶塔使用Nginx?Proxy?Manager申請SSL的實現(xiàn)

    寶塔使用Nginx?Proxy?Manager申請SSL的實現(xiàn)

    本文主要介紹了寶塔使用Nginx?Proxy?Manager申請SSL,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-02-02
  • 如何利用map實現(xiàn)Nginx允許多個域名跨域

    如何利用map實現(xiàn)Nginx允許多個域名跨域

    這篇文章主要給大家介紹了關于如何利用map實現(xiàn)Nginx允許多個域名跨域的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 使用Nginx實現(xiàn)端口轉發(fā)TCP代理的實現(xiàn)示例

    使用Nginx實現(xiàn)端口轉發(fā)TCP代理的實現(xiàn)示例

    本文主要介紹了使用Nginx實現(xiàn)端口轉發(fā)TCP代理的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Nginx 訪問 /root/下 403 Forbidden問題解決

    Nginx 訪問 /root/下 403 Forbidden問題解決

    在使用Nginx作為Web服務器時,可能會遇到403 Forbidden錯誤,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-05-05

最新評論