大家都知道,前段nginx做反代,如果后端服务器宕掉的话,nginx是不能把这台realserver提出upstream的,所以还会有请求转发到后端的这台realserver上面去,虽然nginx可以在localtion中启用proxy_next_upstream来解决返回给用户的错误页面,方法在:http://www.linuxyan.com/web-s...
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz’
$ tar -xzvf nginx-1.16.1.tar.gz
$ cd nginx-1.16.1/
$ patch -p1 < /path/to/nginx_http_upstream_check_module/check.patch
注:因nginx版本更新,1.2以上版本的nginx,补丁为check_1.16.1+.patch
$ ./configure --add-module=/data/nginx_upstream_check_module-master --prefix=/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --add-module=/data/brotli/ngx_brotli --add-module=/data/echo-nginx-module-0.61

$ make
$ mv /data/nginx/sbin/nginx /tpm/
$ mv obj/nginx /data/nginx/sbin/

之后在nginx.conf配置文件里面的upstream加入健康检查,如下:

[root@81-nginx-test xxxx]# cat up.conf 
upstream ser{
ip_hash;
server  192.168.0.47:8090  max_fails=2 fail_timeout=10s;
server  192.168.0.48:8090  max_fails=2 fail_timeout=10s;
check interval=5000 rise=1 fall=5 timeout=4000;
keepalive 20000;
}

这里下面加的这句话我解释下,interval检测间隔时间,单位为毫秒,rsie请求2次正常的话,标记此realserver的状态为up,fall表示请求5次都失败的情况下,标记此realserver的状态为down,timeout为超时时间,单位为毫秒。 在server段里面可以加入查看realserver状态的页面

[root@81-nginx-test xxxx]# cat a.conf 
####uat-skpay-backmanage.xxxx.com
server {
    listen 80;
    server_name back.xxxx.com;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    #add_header X-Frame-Options SAMEORIGIN;
    index index.html;
    access_log /data/ser.log;

 location / {
        if ($request_method !~ ^(GET|POST|PUT|DELETE|HEAD|OPTIONS)$) {
            return  403;
        }
        #proxy_pass http://service-backmanage_uat;
        #proxy_pass http://192.168.0.48:8090/backmanage/;
        proxy_pass http://ser;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 16k;
        proxy_buffering on;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_max_temp_file_size 1024m;
    }
        location /nstatus {
                  check_status;
                  access_log off;
                  allow all;
        }
}




####uat-skpay-backmanage.xxxx.com
server {
    listen 80;
    server_name dev-back.xxxx.com;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    #add_header X-Frame-Options SAMEORIGIN;
    index index.html;
    access_log /data/ser.log;

        location / {
        if ($request_method !~ ^(GET|POST|PUT|DELETE|HEAD|OPTIONS)$) {
            return  403;
        }
        #proxy_pass http://service-backmanage_uat;
        #proxy_pass http://192.168.0.48:8090/backmanage/;
        proxy_pass http://ser;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 16k;
        proxy_buffering on;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_max_temp_file_size 1024m;
    }
        location /nstatus {
                  check_status;
                  access_log off;
                  allow all;
        }
}

image.png


锅包肉
89 声望17 粉丝

这个人很懒,没有什么说的。