keepalived实现nginx的高可用

架构图:

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

keepalived.conf 配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     aaa@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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 {
     aaa@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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 "";
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值