keepalived+nginx負載均衡反向代理

keepalived配置php

vrrp_script chk_http_port {
    script "/root/check_nginx_pid.sh"
    interval 2                          #(檢測腳本執行的間隔)
    weight 2
}
vrrp_instance VI_1 {
    #備用服務器上爲 BACKUP
    state MASTER
    #綁定vip的網卡爲ens33,你的網卡可能不同,這裏須要改一下
    interface ens33
    virtual_router_id 52
    #備用服務器上爲90
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_http_port            #(調用檢測腳本)
    }
    virtual_ipaddress {
        192.168.198.24
    }
}

nginx反向代理配置:html

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    use epoll;
    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    /var/log/nginx/access.log main;
    #sendfile on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
     gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
     client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

upstream proxy_test {
   server 192.168.198.144:80 weight=4 max_fails=2 fail_timeout=30s;     #若是你要測試,把這裏換成你本身要代理後端的ip
   server 192.168.198.145:80 weight=9 max_fails=2 fail_timeout=30s;
   #ip_hash;                                              #當負載兩臺以上用ip來hash解決session的問題,一臺就別hash了。
 }
server {
        listen       80;
        server_name  www.mysvr1.com;
location / {
        proxy_pass       http://proxy_test;               #這裏proxy_test是上面的負載的名稱,映射到代理服務器,能夠是ip加端口,   或url
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.(html|php|jsp|jspx|dp)?$
    {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://proxy_test;    #轉向tomcat處理
    }
   }
}

檢查nginx的情況而決定keepalivednode

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/sbin/nginx                #重啓nginx
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重啓失敗,則停掉keepalived服務,進行VIP轉移
              killall keepalived                    
      fi
fi
相關文章
相關標籤/搜索