Nginx 反向代理、后端檢測模塊



簡介:

Nginx 反向代理模塊:ngx_http_proxy_module、ngx_http_upstream_module 后端檢測模塊:nginx_http_upstream_check_module

前者是官方提供的,安裝 Nginx 的時候默認就內置了,可以直接使用,地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html

后者是淘寶大神提供的后端檢測模塊,需要手動編譯添加,地址:https://github.com/yaoweibin/nginx_upstream_check_module

當前穩定版本:http://nginx.org/download/nginx-1.12.2.tar.gz

一、實驗環境

1、Nginx

shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel

shell > wget http://nginx.org/download/nginx-1.12.2.tar.gz
shell > tar zxf nginx-1.12.2.tar.gz; cd nginx-1.12.2
shell > ./configure --prefix=/usr/local/nginx-1.12.2 && make && make install

2、后端服務器

shell > curl 192.168.10.24:8080
welcome to tomcat1
shell > curl 192.168.10.24:8081
welcome to tomcat2
shell > curl 192.168.10.24:8082
welcome to tomcat3

# 好了,三台后端服務器已經啟動,分別監聽 8080、8081、8082,分別返回 1、2、3

二、ngx_http_proxy_module、ngx_http_upstream_module

shell > vim conf/nginx.conf

user  nobody;
worker_processes  1;

pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    upstream ls {
        server 192.168.10.24:8080 weight=1 max_fails=3 fail_timeout=20s;
        server 192.168.10.24:8081 weight=2 max_fails=3 fail_timeout=20s;
        server 192.168.10.24:8082 weight=3 max_fails=3 fail_timeout=20s;
    }
    
    server {
        listen  80;
        
        location / {
            proxy_pass http://ls;
        }
    }
}

# 這是一個最簡配的 Nginx 配置文件,定義了一個負載均衡池,池中有三台服務器,權重分別是 1、2、3 ( 越大越高 )
# 最大失敗次數 3 次,超過 3 次失敗后,20 秒內不檢測。

# 當用戶訪問該 IP 的 80 端口時,被轉發到后端的服務器。下面是一些反向代理的配置。

# 故障轉移策略,當后端服務器返回如下錯誤時,自動負載到后端其余機器
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
# 設置后端服務器獲取用戶真實IP、代理者真實IP等
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 用於指定客戶端請求主體緩存區大小,可以理解成先保存到本地再傳給用戶
client_body_buffer_size 128k;
# 表示與后端服務器連接的超時時間,即發起握手等侯響應的超時時間
proxy_connect_timeout 90;
# 表示后端服務器的數據回傳時間,即在規定時間之后端服務器必須傳完所有的數據,否則 Nginx 將斷開這個連接
proxy_send_timeout 90;
# 設置 Nginx 從代理的后端服務器獲取信息的時間,表示連接建立成功后,Nginx 等待后端服務器的響應時間,其實是 Nginx 已經進入后端的排隊中等候處理的時間
proxy_read_timeout 90;
# 設置緩沖區大小,默認該緩沖區大小等於指令 proxy_buffers 設置的大小
proxy_buffer_size 4k;
# 設置緩沖區的數量和大小。Nginx 從代理的后端服務器獲取的響應信息,會放置到緩沖區
proxy_buffers 4 32k;
# 用於設置系統很忙時可以使用的 proxy_buffers 大小,官方推薦大小為 proxu_buffers 的兩倍
proxy_busy_buffers_size 64k;
# 指定 proxy 緩存臨時文件的大小
proxy_temp_file_write_size 64k;
shell > /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful

shell > /usr/local/nginx-1.12.2/sbin/nginx

shell > i=0; while [ $i -lt 10 ];do curl localhost; let i++;done
welcome to tomcat2
welcome to tomcat3
welcome to tomcat3
welcome to tomcat2
welcome to tomcat1
welcome to tomcat3
welcome to tomcat2
welcome to tomcat3
welcome to tomcat3
welcome to tomcat2

# 總共請求10次,tomcat3 響應了5次,因為它的權重最高(weight=3)。

# 這樣有一個問題,由於沒有后端檢測功能,當后端某一服務器無法提供服務時,該鏈接先被轉發到這台機器,然后發現該機故障,而后才轉發到其它機器。

# 導致資源浪費。

二、nginx_http_upstream_check_module

shell > git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

shell > yum -y install patch

shell > cd /usr/local/src/nginx-1.12.2; patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.12.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

# 切換到 Nginx 源碼目錄,打補丁 ( 注意與自己的 Nginx 版本匹配 )

shell > ./configure --prefix=/usr/local/nginx-1.12.2 --add-module=/usr/local/src/nginx_upstream_check_module
shell > make && make install

# 重新編譯、安裝 Nginx,注意加上原來的編譯參數

shell > vim /usr/local/nginx-1.12.2/conf/nginx.conf

    upstream ls {
        server 192.168.10.24:8080;
        server 192.168.10.24:8081;
        server 192.168.10.24:8082;

        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }

    server {
        listen  80;

        location / {
            proxy_pass http://ls;
        }

        location /status {
            check_status;
            access_log off;
            # allow x.x.x.x;
            # deny all;
        }
    }

# 去掉了權重值,注意:是可以同時存在的。
# 添加了一行,檢測間隔3000毫秒,連續成功2次標記為UP,連續失敗5次標記為DOWN,超時時間1000毫秒,檢測類型HTTP。

shell > /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful

shell > /usr/local/nginx-1.12.2/sbin/nginx -s stop
shell > /usr/local/nginx-1.12.2/sbin/nginx

# 直接 -s reload 貌似不行~

shell > curl localhost/status?format=json
{"servers": {
  "total": 3,
  "generation": 1,
  "server": [
    {"index": 0, "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": 20, "fall": 0, "type": "http", "port": 0},
    {"index": 1, "upstream": "ls", "name": "192.168.10.24:8081", "status": "up", "rise": 18, "fall": 0, "type": "http", "port": 0},
    {"index": 2, "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": 19, "fall": 0, "type": "http", "port": 0}
  ]
}}

# 總共有三台機器,都屬於負載均衡 ls 組,狀態 up,連續成功次數等等。

shell > curl localhost/status?format=json
{"servers": {
  "total": 3,
  "generation": 1,
  "server": [
    {"index": 0, "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": 73, "fall": 0, "type": "http", "port": 0},
    {"index": 1, "upstream": "ls", "name": "192.168.10.24:8081", "status": "down", "rise": 0, "fall": 6, "type": "http", "port": 0},
    {"index": 2, "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": 68, "fall": 0, "type": "http", "port": 0}
  ]
}}

# 關一台后端的話,就變成了這樣!重啟檢測成功后,會被重新加入到負載均衡中!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM