| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
dmc3
9年前发布

keepalived高可用反向代理的nginx

实验系统:

(1)CentOS 6.6_x86_64;

(2)共有三台主机,本实验以ip地址来命名主机,即131主机、132主机、133主机。

实验前提:防火墙和selinux都关闭,主机之间时间同步

实验软件:nginx-x86_64-1.6.2 keepalived-x86_64-1.2.13

实验拓扑:

keepalived高可用反向代理的nginx

一、安装配置keepalived

1.直接使用yum安装,133主机只安装nginx:

yum -y install keepalived nginx

2.在131主机上配置keepalived:

vim /etc/keepalived/keepalived.conf  --------------------------------------------  ! Configuration File for keepalived  global_defs {       //此段配置忽略     notification_email {     acassen@firewall.loc     failover@firewall.loc     sysadmin@firewall.loc     }     notification_email_from Alexandre.Cassen@firewall.loc     smtp_server 192.168.200.1     smtp_connect_timeout 30     router_id LVS_DEVEL  }    vrrp_instance VI_1 {    state MASTER      //定义为MASTER主机    interface eth0    virtual_router_id 55   //虚拟id    priority 100    //优先级  advert_int 1      //检测间隔    authentication {      auth_type PASS     //认证方式      auth_pass abcd     //认证密码    }    virtual_ipaddress {      192.168.245.150    //虚拟ip,即VIP   }    notify_master "/etc/init.d/nginx start"  //成为MASTER之后执行的动作    notify_backup "/etc/init.d/nginx stop"   //成为BACKUP之后执行的动作    notify_fault "/etc/init.d/nginx stop"    //FAULT之后执行的动作  }

3.将配置文件传到132主机,并配置:
vim /etc/keepalived/keepalived.conf  -------------------------------------------------- 只改动如下两行  state BACKUP         //定义为BACKUP  priority 99          //优先级

4.两台主机启动keepalived服务,现在131这台主机为MASTER,所以服务都起在这台主机上:

service keepalived start ip addr show

keepalived高可用反向代理的nginx

keepalived高可用反向代理的nginx

5.停止131主机的keepalived服务,可以看到服务已经转移到了132这台主机上:

service keepalived stop

keepalived高可用反向代理的nginx

6.再次启动131主机的keepalived服务,从日志可以看出,它又成为了MASTER:

keepalived高可用反向代理的nginx

二、配置nginx的反向代理

1.131和132主机配置nginx:

vim /etc/nginx/conf.d/default.conf  ----------------------------------------------  location / {                                              //增加此段          proxy_pass http://192.168.245.133;  }    #location / {                                             //将此段注释掉      #    root   /usr/share/nginx/html;      #    index  index.html index.htm;      #    example      #ModSecurityEnabled on;      #ModSecurityConfig /etc/nginx/modsecurity.conf;  #}  ----------------------------------------------  service nginx restart  service keepalived restart

2.133主机启动测试页:
rm -rf /usr/share/nginx/html/*                 //删除原有页面文件  vim /usr/share/nginx/html/index.html  ---------------------------------------------  <h1>This is a test!</h1>  ---------------------------------------------  service nginx start

访问http://192.168.245.150,133主机上的页面文件出现,反向代理成功。关闭131主机的keepalived服务,132主机变为MASTER,页面仍然可以访问。但是,当nginx进程被kill掉时,并不能实现高可用。

keepalived高可用反向代理的nginx


 本文由用户 dmc3 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1437832442615.html
Web服务器 Nginx