Nginx负载均衡健康检查性能提升

 更新时间:2023年10月27日 09:55:27   作者:ChengKe_dawn  
这篇文章主要为大家介绍了Nginx负载均衡健康检查性能提升,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块 但可以使用第三方模块 nginx_upstream_check_module 来检测后端服务器的健康状态。我安装Nginx的方式,是通过 yum 安装的,通过 yum 安装的没有 nginx_upstream_check_module 这个模块,所以我们需要先给 Nginx 把这个模块安装好。

如果你已经yum安装过Nginx,那么建议最好是先 yum remove nginx 卸载一下,暂时没有找到找能不卸载然后给Nginx配置这个模块的办法。

安装配置

  • 安装依赖包
yum -y install gcc glibc gcc-c++ openssl-devel pcre-devel patch
  • 下载nginx源码包和nginx_upstream_check模块第三方模块

Nginx官方地址 

cd /usr/local
wget https://nginx.org/download/nginx-1.21.4.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/heads/master.zip

该第三方模块在 github 上的地址为:可以去查看,也给出了配置的示例,很详细。地址

  • 解压Nginx源码包和第三方模块
tar -zxvf nginx-1.21.4.tar.gz
unzip nginx_upstream_check_module-master.zip

添加Nginx模块

cd nginx-1.21.4/
patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch

 p1代表在Nginx目录,p0是不在Nginx目录

  • 编译Nginx,需要添加upstream_check第三方模块

通过 nginx -V 查看


将以上内容复制下来

重新编译的时候加上 --add-module=/usr/local/nginx_patch/nginx_upstream_check_module-master。 模块地址就是你下载 nginx_upstream_check_module 到本地的地址。(但是有时候nginx -v会失败,但是不会又太大影响,直接复制下面的命令也是可以的)

./configure --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --add-module=/usr/local/nginx_patch/nginx_upstream_check_module-master --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  • 编译安装
make && make install

  • 启动NGINX(防止因为报错为启动成功)
suho /usr/sbin/nginx

 此时可能会报错没有发现某个文件夹,但是不影响,你去对应的目录创建一个即可

nginx -t 检查nginx配置是否正确
nginx -s reload 重新加载配置文件

博主的nginx.conf配置(其实建议最好的每个站点的配置文件独立开,但是我偷懒了并没有)

注意,这配置文件通用性比较高,但是也是需要依赖自己的需求修改部分配置,博主是在两台服务器之间进行搭建的两台NGINX,双方进行心跳检测,ip为内网和弹性两个,10.75.25.207对应的内网是10.75.28.14

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    map $time_iso8601 $logdate {
    '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
    default    'date-not-found';
    }
send_timeout 30;
    client_body_buffer_size 40m;
    proxy_buffer_size 128k; 
    proxy_buffers 4 128k; 
    proxy_busy_buffers_size 128k;
    # access_log /var/log/nginx/access.log main;
    access_log  /var/log/nginx/access-$logdate.log main;
    server_tokens off;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /usr/local/nginx/conf/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    server {
        listen       80;
        #listen 443 ssl http2;
        server_name  10.75.28.14 10.75.25.207;
        index  index.html;
        root /data/wwwroot/paizhu/dist/;
        gzip on; 
        gzip_buffers 32 4K; 
        gzip_comp_level 6; 
        gzip_min_length 200;
        # access_log /var/log/nginx/host.access_.log main; 
        gzip_types application/javascript text/css text/xml; 
        gzip_disable "MSIE [1-6]\."; 
        #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
        gzip_vary on;
        #SSL-END
        location /status {
            check_status;
            access_log   off;
        }
        location /file {
            alias /data/wwwroot/upload;
            try_files $uri @mongrel;
        }
        location @mongrel {
            if ($http_lan_check = "1") {
                return 404;
            }
            proxy_pass http://10.75.28.37:80;
            proxy_set_header lan-check "1";
        }
        location /oss {
            rewrite ^/oss(.*)$ /$1 break;
            proxy_pass http://szpzjdgzglxt.oss111b-cn-sx-zjsxzwy01-d01-a.inner.zjsxzwy.cn/;
            #proxy_set_header Host $host; OSS的反向代理,需要去掉来源地址,政务云是这样
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_http_version 1.1;
            add_header X-Cache $upstream_cache_status;
        }
        location /manage {
            proxy_pass http://manage;
            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_set_header REMOTE-HOST $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection $connection_upgrade;
            proxy_http_version 1.1;
            # proxy_hide_header Upgrade;
            add_header X-Cache $upstream_cache_status;
            #Set Nginx Cache
            set $static_fileyb8cQBoU 0;
            if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
            {
                set $static_fileyb8cQBoU 1;
                expires 1m;
            }
            if ( $static_fileyb8cQBoU = 0 )
            {
                add_header Cache-Control no-cache;
            }
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    upstream manage {
        least_conn;
        server 10.75.28.14:500 weight=1 max_fails=2 fail_timeout=10s; # 服务实例1
        server 10.75.28.14:501 weight=1 max_fails=2 fail_timeout=10s; # 服务实例2
        server 10.75.28.37:500 weight=1 max_fails=2 fail_timeout=10s; # 服务实例3
        server 10.75.28.37:501 weight=1 max_fails=2 fail_timeout=10s; # 服务实例4
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "GET /manage/heartbeat HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }
}

效果图

正常状态

掉线状态

以上就是Nginx负载均衡健康检查性能提升的详细内容,更多关于Nginx负载均衡的资料请关注脚本之家其它相关文章!

相关文章

  • Nginx跨域设置Access-Control-Allow-Origin无效的解决办法

    Nginx跨域设置Access-Control-Allow-Origin无效的解决办法

    今天小编就为大家分享一篇关于Nginx跨域设置Access-Control-Allow-Origin无效的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • nginx https 443端口配置的方法

    nginx https 443端口配置的方法

    本文主要介绍了nginx https 443端口配置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Nginx中使用Lua脚本与图片的缩略图处理的实现

    Nginx中使用Lua脚本与图片的缩略图处理的实现

    本文主要介绍了Nginx中使用Lua脚本与图片的缩略图处理的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • nginx部署前端项目的超级详细步骤记录

    nginx部署前端项目的超级详细步骤记录

    众所周知Nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,这篇文章主要给大家介绍了关于nginx部署前端项目的超级详细步骤,需要的朋友可以参考下
    2023-02-02
  • nginx 502 Bad Gateway 错误解决办法

    nginx 502 Bad Gateway 错误解决办法

    一些运行在Nginx上的网站有时候会出现“502 Bad Gateway”错误,有些时候甚至频繁的出现。以下是小编搜集整理的一些Nginx 502错误的排查方法,供参考
    2013-10-10
  • Nginx限制IP访问的实现示例

    Nginx限制IP访问的实现示例

    限制某些IP地址访问网站是一个常见的需求,本文主要介绍了Nginx限制IP访问的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-06-06
  • nginx负载均衡配置方式

    nginx负载均衡配置方式

    这篇文章主要介绍了nginx负载均衡配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06
  • prometheus监控nginx的两种方式

    prometheus监控nginx的两种方式

    这篇文章主要介绍了两种不同的Nginx监控方法,第一种是nginx自带的tub_status模块进行监控,第二种是用vts监控工具进行监控,都是基于prometheus、grafana结合第三方模块或监控工具搭建,文中通过图文结合的方式介绍的非常详细,需要的朋友可以参考下
    2024-05-05
  • 通过nginx做mysql的负载均衡实现过程

    通过nginx做mysql的负载均衡实现过程

    Nginx stream模块用于DNS和主从MySQL的TCP/UDP代理与负载均衡,安装需编译启用或通过yum安装,配置stream块及upstream实现转发和轮询,测试后验证端口监听状态
    2025-07-07
  • Nginx请求转发配置指南

    Nginx请求转发配置指南

    Nginx 是一款高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器,本文档将介绍如何使用 Nginx 配置请求转发,并解释一些常用的配置参数,需要的朋友可以参考下
    2024-10-10

最新评论