最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx四層負載均衡的實現示例

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

1、Nginx四層負載均衡

1.1 負載均衡概述

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

  • Nginx的負載均衡功能主要通過其反向代理模式實現。當客戶端發(fā)送請求到Nginx服務器時,Nginx會根據預設的負載均衡策略將請求轉發(fā)給后端服務器,并將后端服務器的響應返回給客戶端。Nginx作為代理服務器,有效地分攤了請求壓力,提高了系統(tǒng)的處理能力。

1.2 負載均衡的目的

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

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

  • 故障轉移:當一臺服務器發(fā)生故障時,負載均衡器可以自動將流量轉移到其他健康的服務器上,以避免服務中斷。

  • 降低延遲:通過選擇最佳的服務器來處理請求,減少數據傳輸的延遲。

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

1.3 Nginx的負載均衡調度算法

1.3.1 輪詢(Round Robin)

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

1.3.2 最少連接(Least Connections)

  • 最少的連接算法將請求發(fā)送到當前連接數最少的服務器。這種方法可以更有效地利用服務器資源,因為它完全避免過載服務器。然而,這種方法可能會導致負載不均衡,特別是在服務器性能差異方面更大的情況發(fā)生。

1.3.3 IP哈希(IP Hash)

  • IP哈希算法根據客戶端的IP地址哈希值將請求發(fā)送到服務器。該方法可以保證來自相同客戶端的這種請求總是被發(fā)送到相同服務器,這對于需要會話保持的應用程序很有用。該方法可能會導致負載不均衡,特別是在服務器數量變化時。

1.3.4 加權輪詢(Weighted Round Robin)

  • 加權輪詢是輪詢算法的一個變體,它允許為每個服務器分配重權。權重損失的服務器將接收到更多的請求。這種方法可以根據服務器的性能和負載情況動態(tài)調整負載分配。

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

  • 加權最小連接算法結合了最小連接和加權輪詢的概念。它根據服務器的權重和當前連接數來選擇服務器,以實現更平衡的負載分配。

1.3.6 哈希(Hash)

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

1.4 Nginx四層負載均衡基本配置

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

1.4.1 后端主機配置

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 代理服務器配置

[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 客戶端端訪問測試

[root@client ~]# for ((i=1;i<=9;i++)); do curl http://www.proxy.com; done   #默認算法為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模塊參數解釋

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

1.4.5 增加權重

[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
?
訪問測試
[root@client ~]# for ((i=1;i<=9;i++)); do curl http://www.proxy.com; done  #web-01和web-02為2:1 ,web-03處于維護中
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 多臺虛擬主機之間基于端口實現負載均衡

1.5.1 后端主機配置

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 代理服務器配置

[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 客戶端訪問測試

[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 反向代理多虛擬主機節(jié)點服務器

1.6.1 模塊主要參數

在 Nginx 的配置中使用 proxy_set_header 指令是為了在 Nginx 作為反向代理服務器時,向真實的后端服務器傳遞客戶端的原始信息。以下是對 proxy_set_header 指令的詳細解釋,以及它如何影響代理請求:

proxy_set_header host $host;

這條指令的作用是:

  • proxy_set_header:這是 Nginx 用來設置由代理服務器發(fā)送給后端服務器的 HTTP 請求頭的指令。

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

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

1.6.2 為什么需要 proxy_set_header host $host;

  • 當 Nginx 作為反向代理時,它默認會將客戶端的請求轉發(fā)給配置好的后端服務器,但是在這個過程中,原始的 Host 請求頭可能會丟失或被修改。這可能導致后端服務器無法正確識別客戶端請求的虛擬主機,尤其是在后端服務器配置了多個虛擬主機時。

  • 例如,如果客戶端發(fā)送了一個帶有 Host: www.yunjisuan.com 的請求,但是 Nginx 在轉發(fā)請求時沒有保留這個 Host 頭部,后端服務器可能會默認提供一個它配置的第一個虛擬主機的響應,而不是客戶端請求的那個特定的虛擬主機。

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 代理服務器配置(不加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 客戶端訪問

[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
?
注:無論訪問那個域名,都是第一臺虛擬主機提供服務

注意:若后端有多臺虛擬主機如果不添加proxy_set_header host $host;的話代理服務器無法判斷代理的是哪個虛擬主機,所以不管訪問的是哪個域名返還的都是第一臺虛擬主機的內容

1.6.3.6 代理服務器配置包含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
指令參數說明
proxy_set_headerhost $host設置代理請求中的Host頭部,值為原始請求中的Host頭部。這確保了后端服務器能夠識別客戶端請求的虛擬主機。
proxy_set_headerX-Forwarded-For $remote_addr設置代理請求中的X-Forwarded-For頭部,值為客戶端的IP地址。這個頭部用于記錄客戶端真實的IP地址,支持識別通過HTTP代理或負載均衡器發(fā)送的請求。

1.6.3.6 客戶端訪問

[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 查看訪問日志

[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"

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

相關文章

  • docker鏡像導入導出備份遷移的操作

    docker鏡像導入導出備份遷移的操作

    這篇文章主要介紹了docker鏡像導入導出備份遷移操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 解決國內k8s的ingress-nginx鏡像無法正常pull拉取問題

    解決國內k8s的ingress-nginx鏡像無法正常pull拉取問題

    本文主要介紹了解決國內k8s的ingress-nginx鏡像無法正常pull拉取問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-03-03
  • nginx+php出現No input file specified解決辦法

    nginx+php出現No input file specified解決辦法

    這篇文章主要介紹了nginx+php出現No input file specified解決辦法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 解決nginx:[emerg]?getpwnam(“nginx“)failed報錯問題

    解決nginx:[emerg]?getpwnam(“nginx“)failed報錯問題

    編譯安裝nginx時,啟動服務報錯nginx:[emerg]getpwnam("nginx")failed,原因是沒有為nginx創(chuàng)建用戶,解決方法是創(chuàng)建一個nginx用戶,該用戶是你在編譯時指定的用戶
    2025-02-02
  • 詳解如何在Nginx中設置文件上傳大小限制

    詳解如何在Nginx中設置文件上傳大小限制

    在使用 Nginx 進行文件上傳時,我們可能需要對上傳文件的大小進行限制,以防止用戶上傳過大的文件導致服務器負載過高,本文將介紹如何在 Nginx 中設置文件上傳大小限制,需要的朋友可以參考下
    2023-07-07
  • Nginx結合keepalived實現集群

    Nginx結合keepalived實現集群

    本文主要介紹了Nginx結合keepalived實現集群,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • 詳解Nginx如何代理UDP連接

    詳解Nginx如何代理UDP連接

    這篇文章主要為大家介紹了Nginx如何代理UDP連接的實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Nginx實現不同域名輸出不同的服務器頭信息方法

    Nginx實現不同域名輸出不同的服務器頭信息方法

    這篇文章主要介紹了Nginx實現不同域名輸出不同的服務器頭信息方法,本文使用了一個ngx_headers_more模塊實現這個特殊需求,需要的朋友可以參考下
    2015-02-02
  • Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉詳解

    Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉詳解

    訪問重寫 rewrite 是 Nginx HTTP 請求處理過程中的一個重要功能,下面這篇文章主要給大家介紹了Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉的相關資料,需要的朋友可以參考下
    2022-03-03
  • nginx 偽靜態(tài)化rewrite規(guī)則

    nginx 偽靜態(tài)化rewrite規(guī)則

    用Nginx的朋友可以參考,加到nginx.conf相應主機server段配置中即可!
    2009-10-10

最新評論

扬州市| 扶绥县| 内江市| 萍乡市| 大丰市| 丰镇市| 观塘区| 肇源县| 海晏县| 郯城县| 巴里| 桂平市| 株洲市| 综艺| 开江县| 喀喇| 金湖县| 吉木萨尔县| 高台县| 禹州市| 深州市| 滨海县| 淮安市| 南城县| 桂东县| 全州县| 中方县| 阳朔县| 巩义市| 德化县| 包头市| 黄大仙区| 阜城县| 疏勒县| 清涧县| 海丰县| 高淳县| 额尔古纳市| 隆化县| 宜良县| 岳西县|