nginx对后台服务健康检查及隐藏版本号

nginx对后台服务健康检查有几种方式,但是最可靠的是采用阿里巴巴的nginx_upstream_check_module模块。其原理是检测后方realserver的健康状态,如果后端服务器不可用,则会将其踢出upstream,所有的请求不转发到这台服务器。当期恢复正常时,将其加入upstream。

1. 下载Nginx及nginx_upstream_check_module模块

下载Nginx及模块到root目录。
wget https://nginx.org/download/nginx-1.14.2.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

2.编译Nginx

  1. 新建Nginx安装目录
    /usr/local/nginx_upstream_check
  2. 在Nginx的解压目录/root/nginx-1.14.2下执行以下命令进行编译。
    patch -p1 < …/nginx_upstream_check_module-master/check_1.14.0+.patch
    ./configure --prefix=/usr/local/nginx_upstream_check/ --add-module=/root/nginx_upstream_check_module-master/
    make
    make install
    至此已经编译完成。

3.配置Nginx

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #隐藏NG的版本
    server_tokens off;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    include vhost/*.conf;
    
    upstream test.zd.com {
      server 127.0.0.1:8080;
      server 127.0.0.1:3333;
      check interval=3000 rise=2 fall=5 timeout=1000 type=http;
      check_keepalive_requests 100;
      check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
      check_http_expect_alive http_2xx http_3xx;
    }
    
    server {
        listen       80;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }

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

    }

}

根据以上内容,可知需要在目录下新建文件。
mkdir -p /usr/local/nginx_upstream_check/conf/vhost
touch test.zd.com.conf

test.zd.com.conf文件内容如下。

server {

        listen 80;
        client_max_body_size 10M;

        server_name test.zd.com;
        charset utf-8;
        index index.html index.htm;
        location / {
                proxy_pass http://test.zd.com;
                proxy_connect_timeout 500s;
                proxy_read_timeout 500s;
                proxy_send_timeout 500s;
                proxy_redirect off ;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_buffer_size 256k;
                proxy_buffers 4 256k;
                proxy_busy_buffers_size 256k;
                proxy_temp_file_write_size 256k;
                proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
                proxy_max_temp_file_size 128m;
                #proxy_cache mycache;
                #proxy_cache_valid 200 302 60m;
                #proxy_cache_valid 404 1m;
       }

       location /nstatus {
         check_status;
         access_log off;
       }
}

4.启动Nginx

#先测试下配置是否正确
/usr/local/nginx/sbin/nginx -t
#启动
/usr/local/nginx/sbin/nginx

5.检查服务

curl 127.0.01:80/nstatus
我将返回内容拷贝到了test.html文件,用浏览器打开该html文件,内容如下。
在这里插入图片描述

发布了12 篇原创文章 · 获赞 2 · 访问量 673

猜你喜欢

转载自blog.csdn.net/gonghaiyu/article/details/103697812