NGINX负载均衡+监控
一、配置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
總結
以上是生活随笔為你收集整理的NGINX负载均衡+监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分析边缘计算和雾计算的区别以及应用场景
- 下一篇: photoswipe.js插件