Nginx反向代理的主被動健康檢查方式
一、環(huán)境介紹
1、系統(tǒng):centos7
2、應用部署:weblogic 12c
2、nginx版本:nginx-1.7.2
3、應用1(weblogic1):192.168.29.149:7018/realware
4、應用2(weblogic2):192.168.29.150:7012/realware
5、nginx服務器:192.168.29.152:8080
二、下載nginx-1.7.2
mkdir -p /opt/src cd opt/src wget http://nginx.org/download/nginx-1.7.2.tar.gz tar -xvf nginx-1.7.2.tar.gz
三、下載主動健康檢查插件
cd opt/src yum -y install wget wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master yum install unzip -y unzip master.zip
四、安裝主動健康檢查插件
yum install patch -y cd /opt/src/nginx-1.7.2 patch -p1 < /opt/src/nginx_upstream_check_module-master/check_1.7.2+.patch
五、編譯安裝nginx
cd /opt/src/nginx-1.7.2 yum -y install gcc pcre-devel zlib-devel openssl openssl-devel ./configure --prefix=/usr/local/nginx --add-module=/opt/src/nginx_upstream_check_module-master make make install
六、反向代理被動健康檢查與反向代理主動健康檢查
1、反向代理被動健康檢查
配置文件修改:vim /usr/local/nginx/conf/nginx.conf
#user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #upstream 后面要用nginx所在服務器的IP,不然可能會導致網絡的css、js等外部連接無法調用。 upstream 192.168.29.152 { #以5s為周期,5秒內只要有一次訪問失敗,則這5s內不會再向該ip發(fā)起訪問。可以根據情況調整。 server 192.168.29.150:7012 max_fails=1 fail_timeout=5s; server 192.168.29.149:7018 max_fails=1 fail_timeout=5s; } server { listen 8080; #server_name 后面要用web應用的名稱 server_name realware; location / { proxy_pass http://192.168.29.152; #這里的IP要與upstream保持一致。 proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host:$server_port; } } }
2、反向代理主動健康檢查
配置文件修改:vim /usr/local/nginx/conf/nginx.conf
#user nginx; worker_processes 1; error_log /usr/local/nginx/logs/error.log info; #er #ror_log logs/error.log notice; #error_log logs/error.log info; pid /usr/local/nginx/logs/nginx.pid; # worker_rlimit_nofile 65535; events { # use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; log_formmat main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /usr/local/nginx/logs/access.log main sendfile on; #tcp_nopush on; keepalive_timeout 60; client_max_body_buffer_size 100m; client_max_body_size 40m; upstream realware_service { server 192.168.29.149:7018; server 192.168.29.150:7012; #以10s為一個周期,每隔10snginx會自動向上游服務器發(fā)送一次請求,如果超過5s超時且達到3次,則該服務器標記為不可用; #如果請求次數有一次以上沒有超時,這標記為可用 check interval=10000 rise=1 fall=3 timeout=5000 type=tcp default_down=true; check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } upstream realware_stamp { server 192.168.29.149:7018 weight=5; server 192.168.29.150:7012 weight=5; #以10s為一個周期,每隔10snginx會自動向上游服務器發(fā)送一次請求,如果超過5s超時且達到3次,則該服務器標記為不可用; #如果請求次數有一次以上沒有超時,這標記為可用 check interval=10000 rise=1 fall=3 timeout=5000 type=tcp default_down=true; 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 8080; server_name locahost; add header Access-Control-Allow-Origin *; add header Access-Control-Allow-Meehods 'GET,POST'; add header Accesg-Control-Allow-Headers 'DNT,x-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' access_log /usr/local/nginx/logs/access.log main location /realware { proxy_connect_timeout 60;#與上游服務器的連接超時時間 proxy_read_timeout 60;#與上游服務器的讀取超時時間 proxy_send_timeout 60;#與上游服務器的發(fā)送超時時間 proxy_pass http://realware_service; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host:8080; peoxy_set_header x-Real-IP $remote_addr; peoxy_set_header REMOTE-HOST $remote_addr; } server { listen 8081; server_name locahost; add header Access-Control-Allow-Origin *; add header Access-Control-Allow-Meehods 'GET,POST'; add header Accesg-Control-Allow-Headers 'DNT,x-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' access_log /usr/local/nginx/logs/access.log main location /estamp { proxy_connect_timeout 60;#與上游服務器的連接超時時間 proxy_read_timeout 60;#與上游服務器的讀取超時時間 proxy_send_timeout 60;#與上游服務器的發(fā)送超時時間 proxy_pass http://estamp_service; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host:$server_port; peoxy_set_header x-Real-IP $remote_addr; peoxy_set_header REMOTE-HOST $remote_addr; } #健康監(jiān)控頁面 location /status { check_status html; access_log off; } } }
五、啟動nginx
cd /usr/local/nginx/sbin ./nginx
六、檢驗結果
在瀏覽器輸入http://192.168.29.152:8080/realware 查看是否配置成功;
在瀏覽器輸入http://192.168.29.152:8080/status
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Nginx網站根目錄更改及導致403 forbidden的問題解決
最近因為工作需要,要將Nginx網站根目錄更改下,通過網上的一些教程更改后,但發(fā)現測試的時候一直提示403 forbidden錯誤,后臺通過一個朋友的提示也解決了,所以現在將詳細的步驟分享給大家,有需要的朋友們可以參考學習。2016-10-10Nginx配置txt、pdf、doc、xls等文件直接下載的方法
這篇文章主要介紹了Nginx配置txt、pdf、doc、xls等文件直接下載的方法,配置方法很簡單,本文直接給出配置示例,需要的朋友可以參考下2015-04-04Nginx+PHP(FastCGI)搭建高并發(fā)WEB服務器(自動安裝腳本)第二版
Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建勝過Apache十倍的Web服務器(第5版) 編寫2011-04-04