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

Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例

 更新時(shí)間:2024年04月23日 08:44:35   作者:幸存者?·?KXY  
Nginx?不支持傳統(tǒng)的四層負(fù)載均衡,但可以通過(guò)stream模塊配合TCP實(shí)現(xiàn)類(lèi)似的功能,本文主要介紹了Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下

1、Nginx四層負(fù)載均衡

1.1 負(fù)載均衡概述

  • 負(fù)載均衡是一種分布式計(jì)算技術(shù),用于將網(wǎng)絡(luò)流量和用戶請(qǐng)求分散到多臺(tái)服務(wù)器上,以此來(lái)提高網(wǎng)絡(luò)服務(wù)的可用性和可靠性。它通過(guò)優(yōu)化資源使用、最大化吞吐量以及最小化響應(yīng)時(shí)間,增強(qiáng)了網(wǎng)絡(luò)、服務(wù)器和數(shù)據(jù)中心的伸縮性和靈活性。

  • Nginx的負(fù)載均衡功能主要通過(guò)其反向代理模式實(shí)現(xiàn)。當(dāng)客戶端發(fā)送請(qǐng)求到Nginx服務(wù)器時(shí),Nginx會(huì)根據(jù)預(yù)設(shè)的負(fù)載均衡策略將請(qǐng)求轉(zhuǎn)發(fā)給后端服務(wù)器,并將后端服務(wù)器的響應(yīng)返回給客戶端。Nginx作為代理服務(wù)器,有效地分?jǐn)偭苏?qǐng)求壓力,提高了系統(tǒng)的處理能力。

1.2 負(fù)載均衡的目的

  • 提高可用性:通過(guò)將請(qǐng)求分散到多個(gè)服務(wù)器,即使部分服務(wù)器出現(xiàn)故障,整個(gè)系統(tǒng)仍然可以繼續(xù)提供服務(wù)。

  • 增強(qiáng)性能:負(fù)載均衡可以提高系統(tǒng)處理大量并發(fā)請(qǐng)求的能力,從而提升整體性能。

  • 故障轉(zhuǎn)移:當(dāng)一臺(tái)服務(wù)器發(fā)生故障時(shí),負(fù)載均衡器可以自動(dòng)將流量轉(zhuǎn)移到其他健康的服務(wù)器上,以避免服務(wù)中斷。

  • 降低延遲:通過(guò)選擇最佳的服務(wù)器來(lái)處理請(qǐng)求,減少數(shù)據(jù)傳輸?shù)难舆t。

  • 資源優(yōu)化:合理分配請(qǐng)求到各個(gè)服務(wù)器,避免某些服務(wù)器過(guò)載而其他服務(wù)器空閑,優(yōu)化資源使用。

1.3 Nginx的負(fù)載均衡調(diào)度算法

1.3.1 輪詢(Round Robin)

  • 輪詢是最簡(jiǎn)單的負(fù)載均衡算法,它將請(qǐng)求按順序分發(fā)到每個(gè)服務(wù)器。當(dāng)一個(gè)請(qǐng)求被發(fā)送到一個(gè)服務(wù)器后,下一個(gè)請(qǐng)求將被發(fā)送到列表中的下一個(gè)服務(wù)器。這種方法簡(jiǎn)單易行使用,但可能不適合所有場(chǎng)景,特別是當(dāng)某些服務(wù)器的處理能力不同時(shí)。

1.3.2 最少連接(Least Connections)

  • 最少的連接算法將請(qǐng)求發(fā)送到當(dāng)前連接數(shù)最少的服務(wù)器。這種方法可以更有效地利用服務(wù)器資源,因?yàn)樗耆苊膺^(guò)載服務(wù)器。然而,這種方法可能會(huì)導(dǎo)致負(fù)載不均衡,特別是在服務(wù)器性能差異方面更大的情況發(fā)生。

1.3.3 IP哈希(IP Hash)

  • IP哈希算法根據(jù)客戶端的IP地址哈希值將請(qǐng)求發(fā)送到服務(wù)器。該方法可以保證來(lái)自相同客戶端的這種請(qǐng)求總是被發(fā)送到相同服務(wù)器,這對(duì)于需要會(huì)話保持的應(yīng)用程序很有用。該方法可能會(huì)導(dǎo)致負(fù)載不均衡,特別是在服務(wù)器數(shù)量變化時(shí)。

1.3.4 加權(quán)輪詢(Weighted Round Robin)

  • 加權(quán)輪詢是輪詢算法的一個(gè)變體,它允許為每個(gè)服務(wù)器分配重權(quán)。權(quán)重?fù)p失的服務(wù)器將接收到更多的請(qǐng)求。這種方法可以根據(jù)服務(wù)器的性能和負(fù)載情況動(dòng)態(tài)調(diào)整負(fù)載分配。

1.3.5 加權(quán)最少連接(Weighted Least Connections)

  • 加權(quán)最小連接算法結(jié)合了最小連接和加權(quán)輪詢的概念。它根據(jù)服務(wù)器的權(quán)重和當(dāng)前連接數(shù)來(lái)選擇服務(wù)器,以實(shí)現(xiàn)更平衡的負(fù)載分配。

1.3.6 哈希(Hash)

  • 哈希算法根據(jù)請(qǐng)求的某些屬性(如 URL 或請(qǐng)求頭)來(lái)選擇服務(wù)器。這種方法可以保證相同的請(qǐng)求總是被發(fā)送到相同的服務(wù)器,但需要注意的是,如果服務(wù)器數(shù)量發(fā)生變化,可能會(huì)導(dǎo)致負(fù)載不均衡。

1.4 Nginx四層負(fù)載均衡基本配置

主機(jī)IPV4地址版本
proxy (代理)192.168.110.31/24Rocky-Linux 8
web-01 (主機(jī)-01)192.168.110.32/24Rocky-Linux 8
web-02 (主機(jī)-02)192.168.110.33/24Rocky-Linux 8
web-03 (主機(jī)-03)192.168.110.34/24Rocky-Linux 8
client (客戶端)192.168.110.35/24Rocky-Linux 8

1.4.1 后端主機(jī)配置

1.4.1.1 web-01配置

[root@web-01 ~]# mkdir -p /nginx/web
[root@web-01 ~]# echo "This is `hostname` IP=`hostname -I`" >> /nginx/web/index.html
[root@web-01 ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.32:80;
        root /nginx/web;
?
        location /{
                index index.html;
        }
}      
?
[root@web-01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-01 ~]# nginx -s reload
[root@web-01 ~]# curl 192.168.110.32
This is web-01 IP=192.168.110.32

1.4.1.2 web-02配置

[root@web-02 ~]# mkdir -p /nginx/web
[root@web-02 ~]# echo "This is `hostname` IP=`hostname -I`" >> /nginx/web/index.html
[root@web-02 ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
        listen 192.168.110.33:80;
        root /nginx/web;
?
        location /{
                index index.html;
        }
}
?
[root@web-02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-02 ~]# nginx -s reload
[root@web-02 web]# curl 192.168.110.33
This is web-02 IP=192.168.110.33

1.4.1.3 web-03配置

[root@web-03 ~]# mkdir -p /nginx/web
[root@web-03 ~]# echo "This is `hostname` IP=`hostname -I`" >> /nginx/web/index.html
[root@web-03 ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
        listen 192.168.110.34:80;
        root /nginx/web;
?
        location /{
                index index.html;
        }
}
?
[root@web-03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-03 ~]# nginx -s reload
[root@web-03 ~]# curl 192.168.110.34:80
This is web-03 IP=192.168.110.34

1.4.2 代理服務(wù)器配置

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
upstream wwwPools {
        server 192.168.110.32;
        server 192.168.110.33;
        server 192.168.110.34;
}
?
server {        
        location / {     
        proxy_pass http://wwwPools;              
        }
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload

1.4.3 客戶端端訪問(wèn)測(cè)試

[root@client ~]# for ((i=1;i<=9;i++)); do curl http://www.proxy.com; done   #默認(rèn)算法為RR
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33 
This is web-03 IP=192.168.110.34 
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33 
This is web-03 IP=192.168.110.34 
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33 
This is web-03 IP=192.168.110.34

1.4.4 upstream模塊參數(shù)解釋

參數(shù)說(shuō)明
server定義后端服務(wù)器的IP地址或域名,可選地指定端口號(hào),默認(rèn)為80端口。
weight服務(wù)器權(quán)重,默認(rèn)為1。權(quán)重越高,處理的請(qǐng)求比例越大。
max failsNginx嘗試連接后端服務(wù)器失敗的次數(shù),默認(rèn)為1。超過(guò)此次數(shù),Nginx將服務(wù)器標(biāo)記為失敗。
fail timeoutmax fails定義的失敗次數(shù)后,距離下次檢查的間隔時(shí)間,默認(rèn)為10秒。
backup熱備配置,僅當(dāng)所有激活的服務(wù)器失敗后,請(qǐng)求才會(huì)被轉(zhuǎn)發(fā)到標(biāo)記為backup的服務(wù)器。
down標(biāo)記服務(wù)器永遠(yuǎn)不可用,通常用于維護(hù)或測(cè)試。配合ip_hash使用時(shí),服務(wù)器不能被標(biāo)記為down。

1.4.5 增加權(quán)重

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
upstream wwwPools {
        server 192.168.110.32 weight=1;
        server 192.168.110.33 weight=2;
        server 192.168.110.34 weight=3 down;
}
?
server {
        location / {
        proxy_pass http://wwwPools;
        }
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload
?
訪問(wèn)測(cè)試
[root@client ~]# for ((i=1;i<=9;i++)); do curl http://www.proxy.com; done  #web-01和web-02為2:1 ,web-03處于維護(hù)中
This is web-02 IP=192.168.110.33 
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33 
This is web-02 IP=192.168.110.33 
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33 
This is web-02 IP=192.168.110.33 
This is web-01 IP=192.168.110.32 
This is web-02 IP=192.168.110.33

1.5 多臺(tái)虛擬主機(jī)之間基于端口實(shí)現(xiàn)負(fù)載均衡

1.5.1 后端主機(jī)配置

1.5.1.1 web-01配置

[root@web-01 ~]# echo "This is `hostname` IP:`hostname -I` port=80" >> /nginx/web/index80.html
[root@web-01 ~]# echo "This is `hostname` IP:`hostname -I` port=81" >> /nginx/web/index81.html
[root@web-01 ~]# echo "This is `hostname` IP:`hostname -I` port=82" >> /nginx/web/index82.html
[root@web-01 ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.32:80;
        root /nginx/web;
?
        location /{ 
                index index80.html;
        }
}
?
server {
        listen 192.168.110.32:81;
        root /nginx/web;
?
        location /{
                index index81.html;
        }
}
?
server {
        listen 192.168.110.32:82;
        root /nginx/web;
?
        location /{
                index index82.html;
        }
}
?
[root@web-01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-01 ~]# nginx -s reload
[root@web-01 ~]# curl 192.168.110.32:80
This is web-01 IP:192.168.110.32  port=80
[root@web-01 ~]# curl 192.168.110.32:81
This is web-01 IP:192.168.110.32  port=81
[root@web-01 ~]# curl 192.168.110.32:82
This is web-01 IP:192.168.110.32  port=82

1.5.1.2 web-02配置

[root@web-02 web]# echo "This is `hostname` IP:`hostname -I` port=80" >> /nginx/web/index80.html
[root@web-02 web]# echo "This is `hostname` IP:`hostname -I` port=81" >> /nginx/web/index81.html
[root@web-02 web]# echo "This is `hostname` IP:`hostname -I` port=82" >> /nginx/web/index82.html
[root@web-02 web]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.33:80;
        root /nginx/web;
?
        location / {
                index index80.html;
        }
}
?
server {
        listen 192.168.110.33:81;
        root /nginx/web;
?
        location / {
                index index81.html;
        }
}
?
server {
        listen 192.168.110.33:82;
        root /nginx/web;
?
        location / {
                index index82.html;
        }
}
?
?
[root@web-02 web]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-02 web]# nginx -s reload
[root@web-02 ~]# curl 192.168.110.33:80
This is web-02 IP:192.168.110.33  port=80
[root@web-02 ~]# curl 192.168.110.33:81
This is web-02 IP:192.168.110.33  port=81
[root@web-02 ~]# curl 192.168.110.33:82
This is web-02 IP:192.168.110.33  port=82

1.5.1.3 web-03配置

[root@web-03 ~]# echo "This is `hostname` IP:`hostname -I` port=80" >> /nginx/web/index80.html
[root@web-03 ~]# echo "This is `hostname` IP:`hostname -I` port=81" >> /nginx/web/index81.html
[root@web-03 ~]# echo "This is `hostname` IP:`hostname -I` port=82" >> /nginx/web/index82.html
[root@web-03 ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.34:80;
        root /nginx/web;
?
        location / {
                index index80.html;
        }
}
?
server {
        listen 192.168.110.34:81;
        root /nginx/web;
?
        location / {
                index index81.html;
        }
}
?
server {
        listen 192.168.110.34:82;
        root /nginx/web;
?
        location / {
                index index82.html;
        }
}
?
[root@web-03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-03 ~]# nginx -s reload
[root@web-03 ~]# curl 192.168.110.34:80
This is web-03 IP:192.168.110.34  port=80
[root@web-03 ~]# curl 192.168.110.34:81
This is web-03 IP:192.168.110.34  port=81
[root@web-03 ~]# curl 192.168.110.34:82
This is web-03 IP:192.168.110.34  port=82

1.5.2 代理服務(wù)器配置

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf 
upstream wwwPools {
        server 192.168.110.32:80;
        server 192.168.110.33:80;
        server 192.168.110.34:80;
        server 192.168.110.32:81;
        server 192.168.110.33:81;
        server 192.168.110.34:81;
        server 192.168.110.32:82;
        server 192.168.110.33:82;
        server 192.168.110.34:82;
?
}
?
server {
        location / {
        proxy_pass http://wwwPools;
        }
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload

1.5.3 客戶端訪問(wèn)測(cè)試

[root@client ~]# for ((i=1;i&lt;=12;i++)); do curl http://www.proxy.com; done
This is web-01 IP:192.168.110.32  port=80
This is web-02 IP:192.168.110.33  port=80
This is web-03 IP:192.168.110.34  port=80
This is web-01 IP:192.168.110.32  port=81
This is web-02 IP:192.168.110.33  port=81
This is web-03 IP:192.168.110.34  port=81
This is web-01 IP:192.168.110.32  port=82
This is web-02 IP:192.168.110.33  port=82
This is web-03 IP:192.168.110.34  port=82
This is web-01 IP:192.168.110.32  port=80
This is web-02 IP:192.168.110.33  port=80
This is web-03 IP:192.168.110.34  port=80

1.6 反向代理多虛擬主機(jī)節(jié)點(diǎn)服務(wù)器

1.6.1 模塊主要參數(shù)

在 Nginx 的配置中使用 proxy_set_header 指令是為了在 Nginx 作為反向代理服務(wù)器時(shí),向真實(shí)的后端服務(wù)器傳遞客戶端的原始信息。以下是對(duì) proxy_set_header 指令的詳細(xì)解釋?zhuān)约八绾斡绊懘碚?qǐng)求:

proxy_set_header host $host;

這條指令的作用是:

  • proxy_set_header:這是 Nginx 用來(lái)設(shè)置由代理服務(wù)器發(fā)送給后端服務(wù)器的 HTTP 請(qǐng)求頭的指令。

  • host:這是 HTTP 請(qǐng)求頭中的一個(gè)字段,通常包含了客戶端請(qǐng)求的原始主機(jī)信息,如域名或 IP 地址。

  • $host:這是 Nginx 變量,它代表客戶端請(qǐng)求中的 Host 頭部的值。

1.6.2 為什么需要 proxy_set_header host $host;

  • 當(dāng) Nginx 作為反向代理時(shí),它默認(rèn)會(huì)將客戶端的請(qǐng)求轉(zhuǎn)發(fā)給配置好的后端服務(wù)器,但是在這個(gè)過(guò)程中,原始的 Host 請(qǐng)求頭可能會(huì)丟失或被修改。這可能導(dǎo)致后端服務(wù)器無(wú)法正確識(shí)別客戶端請(qǐng)求的虛擬主機(jī),尤其是在后端服務(wù)器配置了多個(gè)虛擬主機(jī)時(shí)。

  • 例如,如果客戶端發(fā)送了一個(gè)帶有 Host: www.yunjisuan.com 的請(qǐng)求,但是 Nginx 在轉(zhuǎn)發(fā)請(qǐng)求時(shí)沒(méi)有保留這個(gè) Host 頭部,后端服務(wù)器可能會(huì)默認(rèn)提供一個(gè)它配置的第一個(gè)虛擬主機(jī)的響應(yīng),而不是客戶端請(qǐng)求的那個(gè)特定的虛擬主機(jī)。

1.6.3 配置示例

1.6.3.1 web-01配置

[root@web-01 ~]# mkdir -p /nginx/web-{1..3}
[root@web-01 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-1" >> /nginx/web-1/index.html
[root@web-01 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-2" >> /nginx/web-2/index.html
[root@web-01 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-3" >> /nginx/web-3/index.html
[root@web-01 ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.32:80;
        server_name www.web-01.com;
        root /nginx/web-1;
?
        location /{ 
                index index.html;
        }
}
?
server {
        listen 192.168.110.32:80;
        server_name www.web-02.com;
        root /nginx/web-2;
?
        location /{
                index index.html;
        }
}
?
server {
        listen 192.168.110.32:80;
        server_name www.web-03.com;
        root /nginx/web-3;
?
        location /{
                index index.html;
        }
}
?
[root@web-01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-01 ~]# nginx -s reload

1.6.3.2 web-02配置

[root@web-02 ~]# mkdir -p /nginx/web-{1..3}
[root@web-02 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-1" >> /nginx/web-1/index.html
[root@web-02 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-2" >> /nginx/web-2/index.html
[root@web-02 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-3" >> /nginx/web-3/index.html
[root@web-02 ~]# vim /etc/nginx/conf.d/VirtualHost.conf 
server {
        listen 192.168.110.33:80;
        server_name www.web-01.com;
        root /nginx/web-1;
?
        location /{ 
                index index.html;
        }
}
?
server {
        listen 192.168.110.33:80;
        server_name www.web-02.com;
        root /nginx/web-2;
?
        location /{
                index index.html;
        }
}
?
server {
        listen 192.168.110.33:80;
        server_name www.web-03.com;
        root /nginx/web-3;
?
        location /{
                index index.html;
        }
}
?
[root@web-02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-02 ~]# nginx -s reload

1.6.3.3 web-03配置

[root@web-03 ~]# mkdir -p /nginx/web-{1..3}
[root@web-03 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-1" >> /nginx/web-1/index.html
[root@web-03 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-2" >> /nginx/web-2/index.html
[root@web-03 ~]# echo "This is `hostname`  IP=`hostname -I` dir=/nginx/web-3" >> /nginx/web-3/index.html
[root@web-03 ~]# vim /etc/nginx/conf.d/VirtualHost.conf
server {
        listen 192.168.110.34:80;
        server_name www.web-01.com;
        root /nginx/web-1;
?
        location /{ 
                index index.html;
        }
}
?
server {
        listen 192.168.110.34:80;
        server_name www.web-02.com;
        root /nginx/web-2;
?
        location /{
                index index.html;
        }
}
?
server {
        listen 192.168.110.34:80;
        server_name www.web-03.com;
        root /nginx/web-3;
?
        location /{
                index index.html;
        }
}
?
[root@web-03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-03 ~]# nginx -s reload

1.6.3.4 代理服務(wù)器配置(不加proxy_set_header host $host;)

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf 
upstream wwwPools {
        server 192.168.110.32:80;
        server 192.168.110.33:80;
        server 192.168.110.34:80;
}
?
server {
        listen 80;
        server_name www.web-01.com;
?
        location / {
                proxy_pass http://wwwPools;
        }
}
?
server {
        listen 80;
        server_name www.web-02.com;
?
        location / {
                proxy_pass http://wwwPools;
        }
}
?
server {
        listen 80;
        server_name www.web-03.com;
?
        location / {
                proxy_pass http://wwwPools;
        }
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload

1.6.3.5 客戶端訪問(wèn)

[root@client ~]# echo '192.168.110.31 www.web-01.com www.web-02.com www.web-03.com' >> /etc/hosts   #添加hosts解析,ip為proxy的ip
[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-01.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-1
This is web-02  IP=192.168.110.33  dir=/nginx/web-1
This is web-03  IP=192.168.110.34  dir=/nginx/web-1
[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-02.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-1
This is web-02  IP=192.168.110.33  dir=/nginx/web-1
This is web-03  IP=192.168.110.34  dir=/nginx/web-1
[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-03.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-1
This is web-02  IP=192.168.110.33  dir=/nginx/web-1
This is web-03  IP=192.168.110.34  dir=/nginx/web-1
?
注:無(wú)論訪問(wèn)那個(gè)域名,都是第一臺(tái)虛擬主機(jī)提供服務(wù)

注意:若后端有多臺(tái)虛擬主機(jī)如果不添加proxy_set_header host $host;的話代理服務(wù)器無(wú)法判斷代理的是哪個(gè)虛擬主機(jī),所以不管訪問(wèn)的是哪個(gè)域名返還的都是第一臺(tái)虛擬主機(jī)的內(nèi)容

1.6.3.6 代理服務(wù)器配置包含proxy_set_header host $host;

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf 
upstream wwwPools {
        server 192.168.110.32:80;
        server 192.168.110.33:80;
        server 192.168.110.34:80;
}
?
server {
        listen 80;
        server_name www.web-01.com;
?
        location / {
                proxy_pass http://wwwPools;
                proxy_set_header host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
}
?
server {
        listen 80;
        server_name www.web-02.com;
?
        location / {
                proxy_pass http://wwwPools;
                proxy_set_header host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
}
?
server {
        listen 80;
        server_name www.web-03.com;
?
        location / {
                proxy_pass http://wwwPools;
                proxy_set_header host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
}
?
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@proxy ~]# nginx -s reload
指令參數(shù)說(shuō)明
proxy_set_headerhost $host設(shè)置代理請(qǐng)求中的Host頭部,值為原始請(qǐng)求中的Host頭部。這確保了后端服務(wù)器能夠識(shí)別客戶端請(qǐng)求的虛擬主機(jī)。
proxy_set_headerX-Forwarded-For $remote_addr設(shè)置代理請(qǐng)求中的X-Forwarded-For頭部,值為客戶端的IP地址。這個(gè)頭部用于記錄客戶端真實(shí)的IP地址,支持識(shí)別通過(guò)HTTP代理或負(fù)載均衡器發(fā)送的請(qǐng)求。

1.6.3.6 客戶端訪問(wèn)

[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-01.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-1
This is web-02  IP=192.168.110.33  dir=/nginx/web-1
This is web-03  IP=192.168.110.34  dir=/nginx/web-1
[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-02.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-2
This is web-02  IP=192.168.110.33  dir=/nginx/web-2
This is web-03  IP=192.168.110.34  dir=/nginx/web-2
[root@client ~]# for ((i=1;i<=3;i++)); do curl http://www.web-03.com; done
This is web-01  IP=192.168.110.32  dir=/nginx/web-3
This is web-02  IP=192.168.110.33  dir=/nginx/web-3
This is web-03  IP=192.168.110.34  dir=/nginx/web-3

1.6.3.7 查看訪問(wèn)日志

[root@web-01 ~]# tail -3 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:15:31:38 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:39 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:41 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
?
[root@web-02 ~]# tail -3 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:15:31:38 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:39 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:41 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
?
[root@web-03 ~]# tail -3 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:15:31:38 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:39 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"
192.168.110.31 - - [21/Apr/2024:15:31:41 +0800] "GET / HTTP/1.0" 200 52 "-" "curl/7.61.1" "192.168.110.35"

到此這篇關(guān)于Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Nginx四層負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Nginx之proxy_redirect使用詳解

    Nginx之proxy_redirect使用詳解

    這篇文章主要介紹了Nginx之proxy_redirect使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • 如何使用nginx充當(dāng)mysql的負(fù)載均衡器

    如何使用nginx充當(dāng)mysql的負(fù)載均衡器

    這篇文章主要介紹了使用nginx充當(dāng)mysql的負(fù)載均衡器過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-06-06
  • nginx安裝圖解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    nginx安裝圖解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了nginx安裝的圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Nginx支持websocket的配置詳解

    Nginx支持websocket的配置詳解

    本文主要介紹了Nginx支持websocket的配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Nginx 502 Bad Gateway的原因及解決方法

    Nginx 502 Bad Gateway的原因及解決方法

    這篇文章主要給大家介紹了Nginx 502 Bad Gateway 錯(cuò)誤的原因及解決方法,在php服務(wù)當(dāng)中,有兩個(gè)參數(shù)非常的重要:max_requestst和max_children,具體的原因必須要查看日志才可以弄明白,接下就和小編一起來(lái)看看具體原因及解決方法吧
    2023-08-08
  • Nginx下Wordpress的永久鏈接實(shí)現(xiàn)(301,404等)

    Nginx下Wordpress的永久鏈接實(shí)現(xiàn)(301,404等)

    經(jīng)過(guò)多番測(cè)試,終于在nginx下實(shí)現(xiàn)了rewrite的功能,WrodPress的永久鏈接終于生效了
    2012-09-09
  • Nginx配置文件的具體使用

    Nginx配置文件的具體使用

    本文主要介紹了Nginx配置文件的具體使用,其配置文件通常位于?/etc/nginx/nginx.conf?或?/usr/local/nginx/conf/nginx.conf,下面就來(lái)介紹一下,感興趣的可以了解一下
    2024-08-08
  • Nginx服務(wù)器的location指令匹配規(guī)則詳解

    Nginx服務(wù)器的location指令匹配規(guī)則詳解

    這篇文章主要介紹了Nginx服務(wù)器的location指令匹配規(guī)則,文中介紹了一種動(dòng)靜態(tài)地址分離的方法示例,需要的朋友可以參考下
    2015-12-12
  • 解決nginx報(bào)錯(cuò)信息 client intended to send too large body: 1331696 bytes

    解決nginx報(bào)錯(cuò)信息 client intended to send too large body: 1331696

    這篇文章主要介紹了解決nginx報(bào)錯(cuò) client intended to send too large body: 1331696 bytes的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Nginx配置SSL證書(shū)的全流程

    Nginx配置SSL證書(shū)的全流程

    文章詳細(xì)介紹了如何通過(guò)阿里云或騰訊云申請(qǐng)免費(fèi)SSL證書(shū),并在Nginx中配置SSL以啟用HTTPS,配置包括設(shè)置SSL會(huì)話緩存、超時(shí)、加密套件、優(yōu)先級(jí)以及指定證書(shū)和密鑰的位置,配置完成后,通過(guò)驗(yàn)證語(yǔ)法并重啟Nginx,網(wǎng)站將啟用HTTPS,用戶訪問(wèn)時(shí)會(huì)看到瀏覽器地址欄的鎖圖標(biāo)
    2025-02-02

最新評(píng)論