架构图:

keepalived实现nginx的高可用

1,服务器A:192.168.52.101     部署:keepalived + nginx + 前端页面

keepalived.conf 配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_lvs-01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
    interval 2 ## 检测时间间隔
    weight -20 ## 如果条件成立,权重-20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111  ###主备通信授权密码    和主配成一样的
    }
    virtual_ipaddress {
        192.168.52.100     ###VIP   虚拟ip
    }

    ##将 track_script 块加入 instance 配置块
    track_script {
        chk_nginx  ## 执行 Nginx 监控的服务
    }
}

nginx配置:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream  fengzp.com {
            server    192.168.52.101:8085  weight=1;
            server    192.168.52.101:8086  weight=9;
    }

    server {
        listen       8090;
        server_name  192.168.52.101;

        location / {
            root   "/data/data/front-page/dist";    //前端页面路径
            index  index.html index.htm;
        }

        location /sso/ {
            proxy_pass http://fengzp.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
    }
}
2,服务器B:192.168.52.102     部署:keepalived + nginx + 前端页面

keepalived.conf 配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_lvs-01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
    interval 2 ## 检测时间间隔
    weight -20 ## 如果条件成立,权重-20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.52.100     ###VIP   虚拟ip
    }

    ##将 track_script 块加入 instance 配置块
    track_script {
        chk_nginx  ## 执行 Nginx 监控的服务
    }
}

nginx配置:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream  fengzp.com {
            server    192.168.52.102:8085  weight=1;
            server    192.168.52.102:8086  weight=9;
    }

    server {
        listen       8090;
        server_name  192.168.52.102;

        location / {
            root   "/data/data/front-page/dist";    //前端页面路径
            index  index.html index.htm;
        }

        location /sso/ {
            proxy_pass http://fengzp.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
    }
}

相关文章: