首页 > 代码库 > nginx反向代理web并使用keepalive做VIP

nginx反向代理web并使用keepalive做VIP

nginx反向代理:192.168.1.1、192.168.1.2

web服务器      :192.168.1.3、192.168.1.4

这里的nginx是编译安装的,安装在/usr/loacl/下

正常启动nginx服务后进入配置文件(1.1和1.2都要配置)

#vim /usr/local/nginx/conf/nginx.conf

在http模块里添加:

       upstream myCluster {

                server 192.168.1.3:80;

                server 192.168.1.4:80;

       }

在server模块里面修改并添加:

修改:  #location / {

            #root html;

            #index index.html index.htm;

            #}

添加:  location / {

            proxy_pass http://myCluster;

            proxy_redirect off;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;

            }

#killall -s QUIT nginx

#nginx

创建简单的nginx启动脚本

#!/bin/bash
# chkconfig: - 99 20
# description: this is nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
;;
esac
exit 0

安装keepalive(1.1和1.2上)

#tar -xzvf keepalived-1.2.13.tar.gz -C /usr/src/

#cd /usr/src/keepalived-1.2.13/

#yum install kernel-devel openssl-devel -y

#./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/

#make && make install

#chkconfig --add keepalived

#chkconfig keepalived on

#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

#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_R1               ###1.2上router_id LVS_DEVEL_R2   用于区别服务器

}
vrrp_instance VI_1 {
    state MASTER                               ###1.2上state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100                                   ###1.2上priority 50     比100低就可以
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.10
    }
notify_master "/etc/init.d/nginx start"
notify_backup "/etc/init.d/nginx stop"

notify_fault "/etc/init.d/nginx stop"
}

#/etc/init.d/keepalived start

#ip add

技术分享

在浏览器访问192.168.1.10验证结果

技术分享

技术分享


本文出自 “12325584” 博客,请务必保留此出处http://12335584.blog.51cto.com/12325584/1904889

nginx反向代理web并使用keepalive做VIP