NGINX负载均衡+监控

2 篇文章 0 订阅
1 篇文章 0 订阅

一、配置NGINX运行环境

        更新yum源

                yum update

        安装gcc

                yum install -y gcc-c++

        安装PCRE pcre-devel

                yum install -y pcre pcre-devel

        安装zlib

                yum install -y zlib zlib-devel

        安装Open SSL

                yum install -y openssl openssl-devel

二、下载安装nginx

        1、下载nginx至/usr/local/ 目录

                wget -c https://nginx.org/download/nginx-1.20.1.tar.gz

        2、解压nginx

                tar -xzvf nginx-1.20.1.tar.gz

        3、下载nginx第三方模块nginx_upstream_check_module至/usr/local/目录

                wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master                 unzip master

        4、进入nginx-1.20.1文件,进行打nginx_upstream_check_module模块的补丁

cd nginx-1.20.1/
patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

        5、添加nginx_upstream_check_module模块的参数

./configure --prefix=../nginx --user=www-web --group=www-web --with-http_ssl_module --with-http_v2_module --with-threads --with-file-aio --with-http_stub_status_module --add-module=../nginx_upstream_check_module-master/

        6、编译安装nginx

                 这里和redis的编译安装比较类似,首先在当前目录(/usr/local/nginx-1.20.1)进行编译。输入make即可

                make
                然后回车,如果编译出错,请检查是否前面的4个安装都没有问题。
        编译成功之后,就可以安装了,输入以下指令:

                make install
                ok,安装成功。
                这时候返回上一级目录,就会发现多了nginx目录。

三、配置nginx和nginx监控

        进入/usr/local/nginx/conf/nginx.conf

        vim /usr/local/nginx/conf/nginx.conf

        1、配置nginx

upstream cluster {        
server 192.168.6.99:8080;    #配置应用入Tomcat或者apache        
server 192.168.6.99:8082;    #server localhost:端口号;        
check interval=5000 rise=1 fall=3 timeout=1000; #nginx监控配置    
}#interval: 检测间隔5秒#fall: 连续检测失败次数3次时,认定relaserver is down#rise: 连续检测成功1次时,认定relaserver is up#timeout: 超时1秒
#default_down: 初始状态为down,只有检测通过后才为up#type: 检测类型方式 tcp#1. tcp :tcp 套接字,不建议使用,后端业务未100%启动完成,前端已经放开访问的情况#2. ssl_hello: 发送hello报文并接收relaserver 返回的hello报文#3. http: 自定义发送一个请求,判断上游relaserver 接收并处理#4. mysql: 连接到mysql服务器,判断上游relaserver是否还存在#5. ajp: 发送AJP Cping数据包,接收并解析AJP Cpong响应以诊断上游relaserver是否还存活(AJPtomcat内置的一种协议)#6. fastcgi: php程序是否存活

        2、调用nginx监控

server {        
listen       80;       #nginx端口号80        
server_name  localhost;        
location / {            #从内部调用执行监控模块            
proxy_pass http://cluster;        
}        
location /nstatus {        #从外部调用监控的模块 http://localhost:80/nstatus                
check_status;                
access_log off;        
}
}

四、启动nginx

        1、开启添加80端口号

        防火墙添加80可用端口

                Firewall-cmd --permanent --zone=public --add-port=80/tcp

        重启防火墙可用端口

                Firewall-cmd --reload

        查看防火墙可用端口是否有80

                Firewall-cmd --zone=public --query-port=80/tcp

        2、启动nginx 并 设置开启自启动

        启动nginx

                ./usr/local/nginx/sbin/nginx        

        查看nginx进程

                ps aux | grep nginx

        设置开机自启动只需要在rc.local增加启动代码即可

                vim /ect/rc.local

        在rc.local文件底部加上下面的代码

                /usr/local/nginx/sbin/nginx

        关闭nginx

                ./nginx -s quit 或者 ./nginx -s stop

        重启nginx

                ./nginx -s reload

  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值