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

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

 更新時間:2024年04月24日 08:52:49   作者:幸存者 · KXY  
七層負(fù)載均衡它是在應(yīng)用層,那么它可以完成很多應(yīng)用方面的協(xié)議請求,本文主要介紹了Nginx七層負(fù)載均衡的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下

七層負(fù)載均衡介紹

Nginx七層負(fù)載均衡是在應(yīng)用層(HTTP/HTTPS)上進行的,可以根據(jù)HTTP請求的具體內(nèi)容,如URL、Cookie、Header等,來決定將請求轉(zhuǎn)發(fā)到哪個后端服務(wù)器。這種方式不僅能夠均衡服務(wù)器的計算負(fù)載,還能實現(xiàn)更復(fù)雜的路由策略,例如:

  • 會話粘性(Sticky Sessions):確保用戶的會話請求始終被定向到同一個后端服務(wù)器。

  • 基于內(nèi)容的路由:根據(jù)請求的內(nèi)容(如URL、頭部信息)將請求分發(fā)到不同的服務(wù)器。

四層與七層負(fù)載均衡的區(qū)別

1.1 四層負(fù)載均衡(Layer 4 Load Balancing)

  • 在傳輸層(Transport Layer)上進行。

  • 關(guān)注網(wǎng)絡(luò)層面的信息,如源和目標(biāo)IP地址、端口號等。

  • 根據(jù)網(wǎng)絡(luò)信息決定將數(shù)據(jù)包轉(zhuǎn)發(fā)到哪個服務(wù)器。

  • 不深入檢查數(shù)據(jù)包的內(nèi)容。

  • 主要適用于基于TCP/UDP的流量,如HTTP和HTTPS。

1.2 七層負(fù)載均衡(Layer 7 Load Balancing)

  • 在應(yīng)用層(Application Layer)上進行。

  • 深入檢查網(wǎng)絡(luò)流量的內(nèi)容,如HTTP請求頭、URL、Cookie等。

  • 根據(jù)流量內(nèi)容做復(fù)雜的路由決策。

  • 可以處理多種應(yīng)用層協(xié)議,不僅限于HTTP。

2 七層負(fù)載均衡配置

服務(wù)器類型IP地址發(fā)行版
代理服務(wù)器(proxy)192.168.110.31/24Rocky Linux 8
靜態(tài)地址服務(wù)器(static)192.168.110.32/24Rocky Linux 8
默認(rèn)地址服務(wù)器(default)192.168.110.33/24Rocky Linux 8
動態(tài)地址服務(wù)器(upload)192.168.110.34/24Rocky Linux 8

2.1 后端節(jié)點配置

2.1.1 靜態(tài)地址服務(wù)器(static)

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

2.1.2 默認(rèn)地址服務(wù)器(default)

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

2.1.3 動態(tài)地址服務(wù)器(upload)

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

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

[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
upstream static_pools {
        server 192.168.110.32;
}
?
upstream default_pools {
        server 192.168.110.33;
}
?
upstream upload_pools {
        server 192.168.110.34;
}
?
server {
        listen 80;
        server_name www.nginx.com;
?
        location /static {
                proxy_pass http://static_pools;
                proxy_set_header host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
?
        location /default {
                proxy_pass http://default_pools;
                proxy_set_header host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
?
        location /upload {
                proxy_pass http://upload_pools;
                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

注意:這里location的寫法,這里的主要難點就在這。例如:如果站點目錄寫 root /nginx/ststic; 那最后訪問 http://www.nginx.com/static/ 時轉(zhuǎn)發(fā)路徑就為http://www.nginx.com/static/static。所以寫/nginx就行了

2.3 客戶端測試訪問

[root@client ~]# echo '192.168.110.31 www.nginx.com' >> /etc/hosts
[root@client ~]# curl http://www.nginx.com
This is default page IP=192.168.110.33 
[root@client ~]# curl http://www.nginx.com/static/
This is static page IP=192.168.110.32 
[root@client ~]# curl http://www.nginx.com/upload/
This is upload page IP=192.168.110.34

2.4 查看各節(jié)點訪問日志

[root@static ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:34 +0800] "GET /static/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35"
?
[root@default ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:32 +0800] "GET / HTTP/1.0" 200 40 "-" "curl/7.61.1" "192.168.110.35"
?
[root@upload ~]# tail -1 /var/log/nginx/access.log
192.168.110.31 - - [21/Apr/2024:17:07:36 +0800] "GET /upload/ HTTP/1.0" 200 39 "-" "curl/7.61.1" "192.168.110.35"

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

相關(guān)文章

  • nginx如何實現(xiàn)同個ip、端口訪問不同的項目(以路徑區(qū)分項目)

    nginx如何實現(xiàn)同個ip、端口訪問不同的項目(以路徑區(qū)分項目)

    這篇文章主要介紹了nginx如何實現(xiàn)同個ip、端口訪問不同的項目(以路徑區(qū)分項目),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 如何配置Nginx作為WebSocket代理

    如何配置Nginx作為WebSocket代理

    這篇文章主要介紹了如何配置Nginx作為WebSocket代理問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 使用nginx模擬進行金絲雀發(fā)布的方式

    使用nginx模擬進行金絲雀發(fā)布的方式

    今天小編就為大家分享一篇關(guān)于使用nginx進行金絲雀發(fā)布,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Nginx limit 限制訪問模塊的方法

    Nginx limit 限制訪問模塊的方法

    本篇文章主要介紹了Nginx limit 限制訪問模塊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Nginx配置-日志格式配置方式

    Nginx配置-日志格式配置方式

    這篇文章主要介紹了Nginx配置-日志格式配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Nginx實現(xiàn)瀏覽器可實時查看訪問日志的步驟詳解

    Nginx實現(xiàn)瀏覽器可實時查看訪問日志的步驟詳解

    我們經(jīng)常需要在頁面上實時查看nginx的日志輸出,并且能在頁面上顯示,那么下面小編就給大家說下怎么在瀏覽器上實時動態(tài)的查看nginx的訪問日志,有需要的朋友們可以參考借鑒。
    2016-09-09
  • nginx的語法(基本語法和組成部分)

    nginx的語法(基本語法和組成部分)

    Nginx是一個高效、穩(wěn)定的開源Web服務(wù)器和反向代理服務(wù)器,也可以用作郵件代理服務(wù)器、負(fù)載均衡器和HTTP緩存,很多人都選擇nginx作為web服務(wù)器使用,下面是Nginx配置文件的一些基本語法和組成部分,使用Nginx的朋友可以了解一下
    2023-05-05
  • 配置nginx保證frps服務(wù)器與web共用80端口的方法

    配置nginx保證frps服務(wù)器與web共用80端口的方法

    這篇文章主要介紹了frps服務(wù)端與nginx可共用80端口的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • zabbix自定義監(jiān)控nginx狀態(tài)實現(xiàn)過程

    zabbix自定義監(jiān)控nginx狀態(tài)實現(xiàn)過程

    這篇文章主要為大家介紹了zabbix如何自定義監(jiān)控nginx狀態(tài)的實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • nginx訪問動態(tài)接口報錯404Not Found問題解決

    nginx訪問動態(tài)接口報錯404Not Found問題解決

    本文主要介紹了nginx訪問動態(tài)接口報錯404Not Found問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評論