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

Nginx負載均衡與健康檢查使用詳解

 更新時間:2025年09月29日 09:01:31   作者:?? Komo·  
文章介紹了Nginx反向代理的負載均衡配置及健康檢查模塊nginx_upstream_check_module的使用,通過安裝Tengine并配置檢查參數(shù),實現(xiàn)后端服務器狀態(tài)監(jiān)控與自動切換,提升服務可用性

一、nginx原生模塊介紹

我們在使用nginx做反向代理都會使用到以下兩個模塊:

1、ngx_http_proxy_module

定義允許將請求傳遞到另一臺服務器。此模塊下常用指令如下:

proxy_pass
proxy_cache
proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout
proxy_next_upstream

2、ngx_http_upstream_module

用于定義可由proxy_pass,fastcgi_pass等指令引用的服務器組。

此模塊下常用指令如下:

upstream
server
ip_hash

默認負載均衡配置

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

此時nginx默認的負載均衡策略是輪詢外,還有其他默認參數(shù),如下:

http {
    upstream myapp1 {
        server srv1.example.com weight=1 max_fails=1 fail_timeout=10;
        server srv2.example.com weight=1 max_fails=1 fail_timeout=10;
        server srv3.example.com weight=1 max_fails=1 fail_timeout=10;
    }
?
    server {
        listen 80;
        proxy_send_timeout=60;
        proxy_connect_timeout=60;
        proxy_read_timeout=60;
        proxy_next_upstream=error timeout;
?
        location / {
            proxy_pass http://myapp1;
        }
    }
}

二、nginx_upstream_check_module模塊

借助淘寶技術團隊開發(fā)的nginx??靚ginx_upstream_check_module來檢測后方realserver的健康狀態(tài),如果后端服務器不可用,則會將其踢出upstream,所有的請求不轉發(fā)到這臺服務器。當期恢復正常時,將其加入upstream。

在淘寶自己的tengine上是自帶了該模塊的,大家可以訪問淘寶Tengine官網(wǎng)來獲取該版本的nginx,也可以到Gitbub

如果沒有使用淘寶的tengine的話,可以通過補丁的方式來添加該模塊到我們自己的nginx中。

下載地址1:https://github.com/yaoweibin/nginx_upstream_check_module

#打補丁
#注意不同版本對應的補丁
cd nginx-1.6.0
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.6 --sbin-path=/usr/local/nginx1.6 --conf-path=/usr/local/nginx1.6/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-pcre=/usr/local/src/nginx/pcre-8.36 --with-zlib=/usr/local/src/nginx/zlib-1.2.8 --add-module=/usr/local/src/nginx/ngx_cache_purge-2.1 --add-module=/usr/local/src/nginx/headers-more-nginx-module-master --add-module=/usr/local/src/nginx/nginx_upstream_check_module-master

make
#不要執(zhí)行make install命令

cd /usr/local/nginx1.6
#備份命令
cp nginx nginx.bak
nginx -s stop
cp -r /usr/local/src/nginx/nginx-1.6.0/objs/nginx .

打完補丁后,可進行如下配置:

http {

        upstream cluster {

            # simple round-robin

            server 192.168.0.1:80;
            server 192.168.0.2:80;

            check interval=5000 rise=1 fall=3 timeout=4000;

            #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;

            #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
            #check_http_send "HEAD / HTTP/1.0\r\n\r\n";
            #check_http_expect_alive http_2xx http_3xx;
        }

        server {
            listen 80;

            location / {
                proxy_pass http://cluster;
            }

            location /status {
                check_status;

                access_log   off;
                allow SOME.IP.ADD.RESS;
                deny all;
           }
        }

    }

其中:

Syntax:  check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
Default: 如果沒有配置參數(shù),默認值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream
 
該指令可以打開后端服務器的健康檢查功能。指令后面的參數(shù)意義是:
interval:向后端發(fā)送的健康檢查包的間隔,單位為毫秒。
fall(fall_count): 如果連續(xù)失敗次數(shù)達到fall_count,服務器就被認為是down。
rise(rise_count): 如果連續(xù)成功次數(shù)達到rise_count,服務器就被認為是up。
timeout: 后端健康請求的超時時間,單位毫秒。
default_down: 設定初始時服務器的狀態(tài),如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認為是不可用,要等健康檢查包達到一定成功次數(shù)以后才會被認為是健康的。
type:健康檢查包的類型,現(xiàn)在支持以下多種類型:
     tcp:簡單的tcp連接,如果連接成功,就說明后端正常。
     ssl_hello:發(fā)送一個初始的SSL hello包并接受服務器的SSL hello包。
     http:發(fā)送HTTP請求,通過后端的回復包的狀態(tài)來判斷后端是否存活。
     mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷后端是否存活。
     ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過接收Cpong包來判斷后端是否存活。
     port: 指定后端服務器的檢查端口。你可以指定不同于真實服務的后端服務器的端口,比如后端提供的是443端口的應用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認是0,表示跟后端server提供真實服務的端口一樣。該選項出現(xiàn)于Tengine-1.4.0。
     
Syntax: check_keepalive_requests request_num
Default: 1
Context: upstream
該指令可以配置一個連接發(fā)送的請求數(shù),其默認值為1,表示Tengine完成1次請求后即關閉連接。
 
Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream
該指令可以配置http健康檢查包發(fā)送的請求內容。為了減少傳輸數(shù)據(jù)量,推薦采用"HEAD"方法。
 
當采用長連接進行健康檢查時,需在該指令中添加keep-alive請求頭,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同時,在采用"GET"方法的情況下,請求uri的size不宜過大,確??梢栽?個interval內傳輸完成,否則會被健康檢查模塊視為后端服務器或網(wǎng)絡異常。

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream
該指令指定HTTP回復的成功狀態(tài),默認認為2XX和3XX的狀態(tài)是健康的。

例子如下:

server{
        listen 80;
        
        upstream test{
            server 192.168.3.12:8080 weight=5 max_fails=3 fail_timeout=10s;
               server 192.168.3.13:8080 weight=5 max_fails=3 fail_timeout=10s;
                
            check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
              check_http_send "HEAD /index.html HTTP/1.0\r\n\r\n";
               check_http_expect_alive http_2xx http_3xx;
       }

        location / {

                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_pass http://test;
                proxy_next_upstream error timeout  http_500 http_502 http_503;
        }
    #后端階段健康狀態(tài)監(jiān)控
        location /status {
                check_status;
                access_log off;
        }
}

以上我們同時使用了nginx原生的及淘寶的健康檢查模塊,但是淘寶的間隔時是毫秒級,而且可以自定義監(jiān)控url,定制監(jiān)控頁,響應速度快,比原生的敏感度要高。

三、使用阿里巴巴的tengine實現(xiàn)后端節(jié)點狀態(tài)檢查

1、安裝tengine

unzip tengine-3.1.0.zip
cd tengine-3.1.0/
./configure --prefix=/usr/local/tengine --add-module=/root/tengine-3.1.0/modules/ngx_http_upstream_check_module && make && make install

2、修改配置文件

user  nginx;
worker_processes  1;
error_log  logs/error.log  info;
error_log  "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G"  main;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    upstream webs {
        
            server 192.168.166.10:80;
            server 192.168.166.13:80;
            check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
              check_http_send "HEAD /index.html HTTP/1.0\r\n\r\n";
               check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://webs;
        }
        location /status {
            check_status html;
            access_log   off;
            allow all;
            deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 圖文詳解Nginx版本平滑升級方案

    圖文詳解Nginx版本平滑升級方案

    Nginx平滑升級就是指在不停止業(yè)務的前提下,實現(xiàn)對Nginx軟件版本的升級,下面這篇文章主要給大家介紹了關于Nginx版本平滑升級方案的相關資料,需要的朋友可以參考下
    2021-09-09
  • nginx http響應限速的具體實現(xiàn)

    nginx http響應限速的具體實現(xiàn)

    本文主要介紹了nginx http響應限速的具體實現(xiàn),可以使用limite_rate和limit_rate_after來限制HTTP響應的速度,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • nginx 與后臺端口沖突的解決

    nginx 與后臺端口沖突的解決

    這篇文章主要介紹了nginx 與后臺端口沖突的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • nginx配置wss協(xié)議的實現(xiàn)

    nginx配置wss協(xié)議的實現(xiàn)

    本文主要介紹了nginx配置wss協(xié)議的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • nginx 自定義 404、50x 錯誤頁面的實現(xiàn)

    nginx 自定義 404、50x 錯誤頁面的實現(xiàn)

    本文主要介紹了nginx 自定義 404、50x 錯誤頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12
  • Nginx和Apache幾種防盜鏈配置方法實例

    Nginx和Apache幾種防盜鏈配置方法實例

    這篇文章主要介紹了Nginx和Apache幾種防盜鏈配置方法實例,本文使用判斷來路的方法實現(xiàn)防盜鏈,分別給出Nginx和Apache配置實例,需要的朋友可以參考下
    2015-02-02
  • Nginx安裝配置的實現(xiàn)示例

    Nginx安裝配置的實現(xiàn)示例

    本文主要介紹了Nginx安裝配置的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12
  • 權限問題導致Nginx 403 Forbidden錯誤的解決方法

    權限問題導致Nginx 403 Forbidden錯誤的解決方法

    這篇文章主要介紹了權限問題導致Nginx 403 Forbidden錯誤的解決方法,本文中導致 403 Forbidden錯誤的原因是配置文件中沒有指明一個用戶,需要的朋友可以參考下
    2014-08-08
  • Nginx主機域名配置實現(xiàn)

    Nginx主機域名配置實現(xiàn)

    本文主要介紹了Nginx主機域名配置實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • nginx中(13: Permission denied)權限問題的解決辦法

    nginx中(13: Permission denied)權限問題的解決辦法

    "nginx 13: Permission denied" 錯誤通常表示nginx進程沒有足夠的權限來訪問特定的文件或目錄,本文就來介紹一下解決方法,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09

最新評論