mil 发表于 2013-8-12 09:46:00

Nginx+Keepalive实现高可用+负载均衡并配置多web站点(两台服务器)

主机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_namea.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_nameb.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

wangwengwu 发表于 2013-8-12 11:07:23

在一辆拥挤的公车上,一位女郎忽然叫了起来:别挤啦!别挤啦!把人家的奶都挤出来啦!(她拿着酸奶呢)。

jgugugiug 发表于 2013-8-12 19:50:06

学习了,不错,讲的太有道理了

xuanhao 发表于 2013-8-12 21:47:44

解释就是掩饰,掩饰就是编故事!

michellc 发表于 2013-8-13 15:12:31

微机原理闹危机,随机过程随机过,实变函数学十遍,汇编语言不会编!

chinaab 发表于 2013-8-14 01:49:27

这是什么东东啊

hege 发表于 2013-8-14 08:56:18

听君一席话,省我十本书!
页: [1]
查看完整版本: Nginx+Keepalive实现高可用+负载均衡并配置多web站点(两台服务器)