2022年10月14日 星期五

Nginx | 健康檢查模組安裝與設定(RTSP負載均衡範例)

官方Free版本的Nginx是不支援後端健康檢查,還好有強者寫出一個免費版本,以下就是安裝方法。


步驟一:編譯Nginx


直接照下面步驟編譯,如果有其他想要調整的可以參考github裡面的安裝步驟,編譯前記得先安裝GCC。
# git clone https://github.com/nginx/nginx.git
# git clone https://github.com/zhouchangxun/ngx_healthcheck_module.git
# cd nginx/;
# git checkout branches/stable-1.12
# git apply ../ngx_healthcheck_module/nginx_healthcheck_for_nginx_1.12+.patch
# ./auto/configure --with-stream --with-http_stub_status_module --add-module=../ngx_healthcheck_module/ --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx



錯誤訊息 checking for PCRE library ... not found

checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

安裝PCRE套件解決
# apt-get install libpcre3-dev

錯誤訊息 ./auto/configure: error: the HTTP gzip module requires the zlib library.


安裝ZLIB套件解決
# apt-get install zlib1g-dev




步驟二:執行make


# make


錯誤訊息 cd.current_salt[0] = ~salt[0];

src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
   36 |     cd.current_salt[0] = ~salt[0];
      |       ^
make[1]: *** [objs/Makefile:841: objs/src/os/unix/ngx_user.o] Error 1
make[1]: Leaving directory '/tmp/nginx'
make: *** [Makefile:8: build] Error 2

編輯 src/os/unix/ngx_user.c
註釋  cd.current_salt[0] = ~salt[0]; 這行,如下

#ifdef __GLIBC__
    /* work around the glibc bug */
    /* cd.current_salt[0] = ~salt[0]; */
#endif


錯誤訊息-Werror=cast-function-type

src/http/ngx_http_script.c: In function ‘ngx_http_script_add_copy_code’:
src/http/ngx_http_script.c:698:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
  698 |     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
      |                  ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_var_code’:
src/http/ngx_http_script.c:787:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
  787 |     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;
      |                  ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_capture_code’:
src/http/ngx_http_script.c:1181:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
 1181 |     code->code = (ngx_http_script_code_pt)
      |                  ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_full_name_code’:
src/http/ngx_http_script.c:1296:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
 1296 |     code->code = (ngx_http_script_code_pt) ngx_http_script_full_name_len_code;
      |                  ^
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:946: objs/src/http/ngx_http_script.o] Error 1
make[1]: Leaving directory '/tmp/nginx'
make: *** [Makefile:8: build] Error 2


編輯 objs/Makefile
找到下面這行
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g

去掉 -Werrori
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g


步驟三:執行make install


# make install


步驟四:增加nginx使用者


由於Nginx服務啟動會用到nginx帳號,所以要先新增帳號
# useradd nginx

步驟五:編輯nginx.conf


編輯 /etc/nginx/nginx.conf
最主要就是 stream 這個段落,我是用在RTSP,所以範例就是如下格式

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        # status interface
        location /status {
            healthcheck_status json;
        }
        # http front
        location / {
            proxy_pass http://http-cluster;
        }
        location /nginx-status {
            stub_status on;
            access_log  off;
        }
    }

    server {
        listen 8080;
        location / {
          root html;
        }
    }

    upstream http-cluster {
        # simple round-robin
        server 127.0.0.1:8080;

        check interval=3000 rise=2 fall=5 timeout=5000 type=tcp;
    }
}

stream {
    upstream rtsp {
        server 192.168.1.1:554;
        server 192.168.1.2:554;
        server 192.168.1.3:554;
        check port=554 interval=3000 rise=2 fall=1 timeout=3000 type=tcp;
    }

    server {
       listen 554;
       proxy_pass rtsp;
       error_log       /var/log/nginx/rtsp.log info;
    }
}


步驟六:啟動Nginx


# nginx -t #檢查config有沒有錯誤# nginx #啟動# nginx -s reload #重新整理

步驟七:檢查後端健康狀態


這主要設定在步驟五的upstream http-cluster 段落,如下圖

輸入Nginx的網址後面加入/status?format=html 就可以瀏覽狀態。







沒有留言: