Centos7.6搭建nginx 1.16.1並使用upstream_check_module模塊

環境說明:

主機名 操作系統版本 ip nginx版本 httpd版本 備註
nginx Centos 7.6.1810 172.27.34.41 1.16.1 / nginx服務器
web01 Centos 7.6.1810 172.27.34.161 / 2.4.6 web服務器
web02 Centos 7.6.1810 172.27.34.162 / 2.4.6 web服務器
web03 Centos 7.6.1810 172.27.34.163 / 2.4.6 web服務器

一、nginx_upstream_check_module簡介

1. 模塊出處

由淘寶團隊開發,淘寶自己的 tengine 上自帶該模塊。

2. 模塊意義

nginx自帶的針對後端節點健康檢查的功能比較簡單,無法主動識別後端節點狀態,後端即使有不健康節點, 負載均衡器依然會把該請求轉發給該不健康節點,只能等待超時時間後轉發到其他節點,這樣就會造成響應延遲性能降低的問題。

二、nginx安裝

1. nginx下載

nginx版本查看:https://nginx.org/en/download.html

圖片.png

下載最新的穩定版本nginx-1.16.1源碼包並解壓

[root@nginx ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.16.1.tar.gz

圖片.png

2. nginx_upstream_check_module模塊下載

[root@nginx ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nginx ~]# yum -y install unzip
[root@nginx ~]# unzip master.zip

圖片.png

下載nginx_upstream_check_module模塊並解壓,如果沒有unzip命令可以通過'yum -y install unzip'安裝。

3. 安裝準備

[root@nginx ~]# yum -y install gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel

分別安裝依賴包gcc、pcre、zlib、OpenSSL。

img

依賴包 作用
gcc 編譯依賴 gcc 環境
pcre PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫,nginx也需要此庫。
zlib zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
OpenSSL OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。<br/>nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

4. 打補丁

[root@nginx ~]# yum -y install patch
[root@nginx ~]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.16.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

圖片.png

進入nginx源碼目錄,運行patch命令打補丁

5. 編譯安裝

5.1 安裝nginx

[root@nginx nginx-1.16.1]# ./configure --add-module=/root/nginx_upstream_check_module-master

圖片.png

5.2 編譯

[root@nginx nginx-1.16.1]# make && make install

圖片.png

5.3 安裝檢查

[root@nginx nginx-1.16.1]# ll /usr/local/nginx/
總用量 0
drwxr-xr-x 2 root root 333 1月  22 15:15 conf
drwxr-xr-x 2 root root  40 1月  22 15:15 html
drwxr-xr-x 2 root root   6 1月  22 15:15 logs
drwxr-xr-x 2 root root  19 1月  22 15:15 sbin

安裝完成後/usr/local路徑下有nginx目錄,該目錄爲nginx的默認目錄。

5.4 nginx命令全局執行

[root@nginx nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
[root@nginx nginx-1.16.1]# nginx -v
nginx version: nginx/1.16.1
[root@nginx nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --add-module=/root/nginx_upstream_check_module-master
[root@nginx nginx-1.16.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

'nginx -v'可查看nginx的版本,'nginx -V'可查看加載的模塊,'nginx -t'可檢查配置文件是否正確配置。

圖片.png

6. nginx配置文件

[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# more nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream web-test {
        server 172.27.34.161:8001        weight=5;
        server 172.27.34.162:8001        weight=5;
        server 172.27.34.163:8001        weight=5;
        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       81;

        location / {
            proxy_pass http://web-test;
        }

       location /status {
            check_status;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

各參數意義:

  • interval:向後端發送的健康檢查包的間隔。
  • fall(fall_count): 如果連續失敗次數達到fall_count,服務器就被認爲是down。
  • rise(rise_count): 如果連續成功次數達到rise_count,服務器就被認爲是up。
  • timeout: 後端健康請求的超時時間。
  • default_down: 設定初始時服務器的狀態,如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認爲是不可用,要等健康檢查包達到一定成功次數以後纔會被認爲是健康的。
  • type:健康檢查包的類型,現在支持以下多種類型
    • tcp:簡單的tcp連接,如果連接成功,就說明後端正常。
    • ssl_hello:發送一個初始的SSL hello包並接受服務器的SSL hello包。
    • http:發送HTTP請求,通過後端的回覆包的狀態來判斷後端是否存活。
    • mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷後端是否存活。
    • ajp:向後端發送AJP協議的Cping包,通過接收Cpong包來判斷後端是否存活。
  • port: 指定後端服務器的檢查端口。你可以指定不同於真實服務的後端服務器的端口,比如後端提供的是443端口的應用,你可以去檢查80端口的狀態來判斷後端健康狀況。默認是0,表示跟後端server提供真實服務的端口一樣。該選項出現於Tengine-1.4.0。

後端檢查方式默認爲tcp,本文使用http方式。

三、 apache服務器安裝

在web01、web02和web03上分別安裝配置apache

1. 安裝apache

[root@web01 ~]# yum -y install httpd

圖片.png

2. 配置修改

2.1 修改默認端口

[root@web01 ~]# sed -i 's/Listen 80/Listen 8001/g'  /etc/httpd/conf/httpd.conf

3臺web服務器的默認端口都由80修改爲8001

2.2 修改首頁輸出

[root@web03 ~]# echo `hostname`>/var/www/html/index.html

3臺web服務器首頁輸出都修改爲主機名

3. 啓動apache並設置開機啓動

[root@web01 ~]# systemctl start httpd
[root@web01 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

3臺web服務器啓動並設置httpd服務開機啓動

四、 啓動nginx並設置開啓啓動

[root@nginx conf]# nginx
[root@nginx conf]# sed -i '$a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local
[root@nginx conf]# chmod u+x /etc/rc.d/rc.local

測試:

[root@client ~]# for i in {1..100};do sleep 1; curl http://172.27.34.28:82/;done

image-20200113144228734

五、測試

1. 訪問測試

[root@nginx ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done

圖片.png

訪問nginx服務器 http://172.27.34.41:81,發現請求被平均的分配到後端的3臺web服務器。

2. 後端狀態檢查

瀏覽器輸入 http://172.27.34.41:81/status 檢查後端web服務器狀態圖片.png

參數 意義
server number 後端服務器的數量
generation Nginx reload的次數
Index 服務器的索引
Upstream 在配置中upstream的名稱
Name 服務器IP
Status 服務器的狀態
Rise 服務器連續檢查成功的次數
Fall 連續檢查失敗的次數
Check type 檢查的方式
Check port 後端專門爲健康檢查設置的端口

3. 模擬後端服務不可用

[root@nginx ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done
[root@web01 ~]# systemctl stop httpd

nginx服務器上持續訪問nginx,同時停止web01的httpd服務.

圖片.png

圖片.png

請求沒有被分配到web01,同時後端檢測web01也異常。

4. 啓動web01

[root@nginx ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done
[root@web01 ~]# systemctl start httpd

重新啓動web01上的httpd服務,發現請求重新被分配到web01上

圖片.png

image-20200122155942111

後端檢查web01也恢復正常。

六、nginx卸載

1. 停進程

[root@nginx ~]# ps -ef|grep nginx
root     17746     1  0 16:54 ?        00:00:00 nginx: master process nginx
nobody   17747 17746  0 16:54 ?        00:00:00 nginx: worker process
root     17784 17750  0 16:55 pts/0    00:00:00 grep --color=auto nginx
[root@nginx ~]# pkill nginx
[root@nginx ~]# ps -ef|grep nginx
root     17791 17750  0 16:55 pts/0    00:00:00 grep --color=auto nginx

2. 刪除文件

[root@nginx ~]# find /* -name "nginx*"|xargs rm -rf

 

 

nginx各模塊說明:健康檢查模塊功能

nginx_upstream_check_module模塊源碼nginx_upstream_check_module

 

 

Centos7.6搭建nginx 1.16.1並使用upstream_check_module模塊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章