主机A:192.168.0.10
主机B:192.168.0.20
VIP: 192.168.0.30
域名A:a.com域名B:b.com
1、两台服务器安装nginx(此处省略很多字);
2、修改两台服务器的nginx配置nginx.conf(nginx.conf的配置可以直接同步)
在http{}里面增加这一段
upstream a.com { server 192.168.0.10:81; server 192.168.0.20:81; # ip_hash; ###如果用到session则需要开启ip_hash } server { listen 80; server_name a.com; location / { proxy_pass http://a.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } upstream b.com { server 192.168.0.10:82; server 192.168.0.20:82; # ip_hash; ###如果用到session则需要开启ip_hash } server { listen 80; server_name b.com; location / { proxy_pass http://b.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 81; server_name localhost; index index.html index.htm index.php; root /tmp/a; client_max_body_size 1024m; } server { listen 82; server_name localhost; index index.html index.htm index.php; root /tmp/b; client_max_body_size 1024m; }
1、安装keepalive(A、B两台操作一样)
可以直接到http://www.keepalived.org/download.html下载最新稳定版
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar -xf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure && make && make install
mkdir /etc/keepalived
vim /etc/keepalived/keepalived.conf
global_defs { notification_email { admin@163.com } notification_email_from admin@163.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #从设置为BACKUP interface eth0 #网卡名 virtual_router_id 51 mcast_src_ip 192.168.0.10 #本机IP priority 100 #从机小于主机 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.0.30/16 } }
/etc/init.d/nginx start
/etc/init.d/keepalived start