192.168.56.104javascript
/etc/keepalived/keepalived.confcss
global_defs { router_id nginx-proxy-ha } vrrp_script check_nginx { script "/home/lhb/sh/check_nginx.sh" ###監控腳本 interval 2 ###監控時間 weight 2 ###目前搞不清楚 } vrrp_instance VI_1{ state MASTER ### 設置爲 主 interface eth0 ### 監控網卡 virtual_router_id 51 ### 這個兩臺服務器必須同樣 priority 101 ### 權重值 MASTRE 必定要高於 BAUCKUP authentication { auth_type PASS ### 加密 auth_pass eric ### 加密的密碼,兩臺服務器必定要同樣,否則會出錯 } track_script { check_nginx ### 執行監控的服務 } virtual_ipaddress { 192.168.56.200 ### VIP 地址 } }
192.168.56.107html
/etc/keepalived/keepalived.confjava
global_defs { router_id nginx-proxy-ha } vrrp_script check_nginx { script "/home/lhb/sh/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP ### 設置爲 輔機 interface eth0 virtual_router_id 51 ### 與 MASTRE 設置 值同樣 priority 100 ### 比 MASTRE權重值 低 authentication { auth_type PASS auth_pass eric ### 密碼 與 MASTRE 同樣 } track_script { check_nginx } virtual_ipaddress { 192.168.56.200 } }
/home/lhb/sh/check_nginx.shnginx
#!/bin/bash A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx進程 把值賦給變量A if [ $A -eq 0 ];then ## 若是沒有進程值得爲 零 /usr/local/openresty/nginx/sbin/nginx sleep 3 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived ## 則結束 keepalived 進程 fi fi
192.168.56.10四、192.168.56.107上nginx配置:bash
/usr/local/openresty/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; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; gzip on; gzip_min_length 1k; gzip_buffers 4 1024k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml; #gzip_vary on; proxy_hide_header Vary; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; upstream backendserver { #ip_hash; server 192.168.56.105:80; server 192.168.56.106:80; } server { listen 80; server_name localhost; location / { index index.html index.htm; proxy_pass http://backendserver; proxy_set_header Host $host; proxy_next_upstream error timeout invalid_header http_500 http_502 http_504; proxy_set_header X-Forwarded-For $remote_addr; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }