安装此模块实现高可用

当upstream中的某个服务挂了,就不会再请求此服务,保障服务的正常访问

1、创建目录

mkdir /opt/env
2、下载和解压

cd /opt/env && wget https://openresty.org/download/openresty-1.21.4.1.tar.gz && tar -xf openresty-1.21.4.1.tar.gz && cd /opt/env/openresty-1.21.4.1

3、下载健康检测模块

https://github.com/yaoweibin/nginx_upstream_check_module

下载zip包后解压到如下目录

/opt/env/openresty-1.21.4.1/bundle/

首先安装gcc编译环境等

yum -y install gcc pcre-devel make zlib-devel openssl-devel libxml2-devel \
    libxslt-devel gd-devel GeoIP-devel libatomic_ops-devel luajit \
    luajit-devel perl-devel perl-ExtUtils-Embed git wget vim

编译


./configure \
    --with-threads \
    --with-file-aio \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_auth_request_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_degradation_module \
    --with-http_slice_module \
    --with-http_stub_status_module \
    --with-stream=dynamic \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_geoip_module=dynamic \
	--add-module=./bundle/nginx_upstream_check_module-master \
    --with-stream_ssl_preread_module

然后执行进行安装

make && make install

配置nginx的配置文件

/usr/local/openresty/nginx/conf/nginx.conf

关键配置如下


#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
	upstream cluster1 {
       # simple round-robin
        server 127.0.0.1:80;
        server 127.0.0.1:84;
        # interval:向后端发送的健康检查包的间隔
        # rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up
        # fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down
        # timeout: 后端健康请求的超时时间
        # type=http:健康检查包的类型,发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
        # check_http_send:配置http健康检查包发送的请求内容。
        # check_http_expect_alive :指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的
        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       88;
        server_name  localhost;

         location /status {
                check_status;
                access_log off;
        }
        location / {
               proxy_next_upstream http_502 http_504 error timeout invalid_header;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_pass http://cluster1;
        }
    }
}

 

 

 

最后修改于 2022-11-15 15:26:10
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇