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

Nginx負(fù)載均衡與健康檢查使用詳解

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

一、nginx原生模塊介紹

我們?cè)谑褂胣ginx做反向代理都會(huì)使用到以下兩個(gè)模塊:

1、ngx_http_proxy_module

定義允許將請(qǐng)求傳遞到另一臺(tái)服務(wù)器。此模塊下常用指令如下:

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等指令引用的服務(wù)器組。

此模塊下常用指令如下:

upstream
server
ip_hash

默認(rèn)負(fù)載均衡配置

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

    server {
        listen 80;

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

此時(shí)nginx默認(rèn)的負(fù)載均衡策略是輪詢(xún)外,還有其他默認(rèn)參數(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模塊

借助淘寶技術(shù)團(tuán)隊(duì)開(kāi)發(fā)的nginx??靚ginx_upstream_check_module來(lái)檢測(cè)后方realserver的健康狀態(tài),如果后端服務(wù)器不可用,則會(huì)將其踢出upstream,所有的請(qǐng)求不轉(zhuǎn)發(fā)到這臺(tái)服務(wù)器。當(dāng)期恢復(fù)正常時(shí),將其加入upstream。

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

如果沒(méi)有使用淘寶的tengine的話,可以通過(guò)補(bǔ)丁的方式來(lái)添加該模塊到我們自己的nginx中。

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

#打補(bǔ)丁
#注意不同版本對(duì)應(yīng)的補(bǔ)丁
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 .

打完補(bǔ)丁后,可進(jìn)行如下配置:

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

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream
該指令指定HTTP回復(fù)的成功狀態(tài),默認(rèn)認(rèn)為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;
        }
}

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

三、使用阿里巴巴的tengine實(shí)現(xiàn)后端節(jié)點(diǎn)狀態(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;
        }
    }
}

總結(jié)

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

相關(guān)文章

  • nginx使用rewrite報(bào)錯(cuò)的解決

    nginx使用rewrite報(bào)錯(cuò)的解決

    本文主要介紹了nginx使用rewrite報(bào)錯(cuò)的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Nginx開(kāi)啟stub_status模塊配置方法

    Nginx開(kāi)啟stub_status模塊配置方法

    這篇文章主要介紹了Nginx開(kāi)啟stub_status模塊配置方法,Nginx中的stub_status模塊主要用于查看Nginx的一些狀態(tài)信息,本文講解它的開(kāi)啟配置方法,需要的朋友可以參考下
    2015-02-02
  • nginx獲取客戶(hù)端真實(shí)ip的常用方法

    nginx獲取客戶(hù)端真實(shí)ip的常用方法

    這篇文章給大家介紹了在nginx中獲取客戶(hù)端真實(shí)IP的兩種常用方法,文中有詳細(xì)的代碼供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-09-09
  • nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小

    nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小

    在用 phpMyAdmin 進(jìn)行 sql 數(shù)據(jù)庫(kù)導(dǎo)入的時(shí)候,經(jīng)常需要上傳比較大的 sql 數(shù)據(jù)文件,而這時(shí)會(huì)常碰見(jiàn) nginx報(bào)錯(cuò):413 Request Entity Too Large。解決此問(wèn)題,根據(jù)上傳數(shù)據(jù)文件的大小進(jìn)行修改處理
    2014-06-06
  • Nginx配置解決NetCore的跨域問(wèn)題

    Nginx配置解決NetCore的跨域問(wèn)題

    跨域資源共享(CORS)標(biāo)準(zhǔn)新增了一組?HTTP?首部字段,允許服務(wù)器聲明哪些源站有權(quán)限訪問(wèn)哪些資源,這篇文章主要介紹了Nginx配置解決NetCore的跨域問(wèn)題,需要的朋友可以參考下
    2022-07-07
  • nginx做白名單和限流的完整過(guò)程

    nginx做白名單和限流的完整過(guò)程

    ??我們都知道nginx里面是可以用lua腳本做一些稍微復(fù)雜些的邏輯處理的,要使用lua腳本需要編譯lua解釋器,時(shí)間有限我直接用了openresty,它集成了lua和nginx,這篇文章主要介紹了nginx做白名單和限流,需要的朋友可以參考下
    2024-02-02
  • Nginx HTTP:413 Request Entity Too Large解決方法

    Nginx HTTP:413 Request Entity Too Large解決方法

    這篇文章主要介紹了Nginx HTTP:413 Request Entity Too Large解決方法,這個(gè)問(wèn)題需要修改PHP配置以及Nginx配置才可以解決,需要的朋友可以參考下
    2015-07-07
  • Nginx之正向代理與反向代理進(jìn)階方式(支持https)

    Nginx之正向代理與反向代理進(jìn)階方式(支持https)

    文章介紹了如何在Nginx中實(shí)現(xiàn)正向代理和反向代理對(duì)HTTP和HTTPS協(xié)議的支持,通過(guò)使用第三方模塊`ngx_http_proxy_connect_module`和Nginx內(nèi)置的`ngx_http_ssl_module`,可以實(shí)現(xiàn)Nginx對(duì)HTTPS的正向和反向代理
    2025-03-03
  • Nginx負(fù)載均衡/SSL配置的實(shí)現(xiàn)

    Nginx負(fù)載均衡/SSL配置的實(shí)現(xiàn)

    這篇文章主要介紹了Nginx負(fù)載均衡/SSL配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Nginx代理Redis哨兵主從配置的實(shí)現(xiàn)

    Nginx代理Redis哨兵主從配置的實(shí)現(xiàn)

    本文主要介紹了Nginx代理Redis哨兵主從配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論

安吉县| 高邮市| 云阳县| 凌海市| 蚌埠市| 桐柏县| 于都县| 昌乐县| 西充县| 新龙县| 凤凰县| 长兴县| 和龙市| 浑源县| 乡宁县| 思南县| 万盛区| 巴东县| 闻喜县| 罗平县| 濮阳县| 澳门| 天峨县| 九江市| 清河县| 若尔盖县| 永康市| 沐川县| 芷江| 云阳县| 尉犁县| 湖北省| 陆丰市| 昌吉市| 舒兰市| 宜昌市| 同德县| 玉树县| 米泉市| 偏关县| 门头沟区|