jjfjjj 发表于 2015-9-4 12:13:36

nginx+keepalived用rpm安装

  首先添加几个源epel,atomic,rpmforge,rpmfusion,方法可以参照前面文章
  #yum -y install gcc openssl-devel pcre-devel zlib-devel nginx keepalived
  #vi /etc/nginx/nginx.conf   
  #####在http{ 中添加下面的内容
  include /etc/nginx/conf.d/*.conf;
    client_max_body_size 300m;
    client_body_buffer_size 128k;
    client_body_temp_path /tmp;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_temp_path /tmp;
    upstream    192.168.100.200 {    //192.168.100.159为我的代理地址,下面两IP是应用服务器地址
       server 192.168.100.121:80 weight=4 max_fails=2 fail_timeout=30s;   
       server 192.168.100.122:80 weight=4 max_fails=2 fail_timeout=30s;   
            }
  #vi /etc/nginx/conf.d/default.conf
  ##########在location / { 中添加下面内容
  proxy_pass http://192.168.100.200;       //代理的地址,此为VIP
      proxy_set_header Host $host;
      proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
  nginx代理已经配置完成,下面配置keepalived
  #vi /etc/keepailved
  ! Configuration File for keepalived
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from root@localhost
    smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script nginx.sh {
script "/root/nginx.sh"   //监控nginx脚本
interval 1
weight 2
}
vrrp_instance VI_1 {
    state MASTER   //主机,备机改为SLAVE
    interface eth0       //网卡
    virtual_router_id 51
    priority 200          //优先级,比备机数值大
    advert_int 1   
    authentication {
      auth_type PASS
      auth_pass 1234
    }
    track_script {   //上面定义的脚本
  nginx.sh
}
    virtual_ipaddress {
       192.168.100.200/24   //虚拟IP(VIP)
    }
}
  备机的配置只要改
state MASTER ==>state SLAVE
  priority 200==>priority 190
  其他的不变,接着新建一脚本
  #vi /root/nginx.sh
  #!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then      // 判断nginx是否未启用
    /usr/local/nginx/sbin/nginx   // 运行nginx
    sleep 3
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
//3秒后检测,如果nginx启动出错就关闭keepalived
      killall keepalived
    fi
fi
  主备都添加即可,然后长ping 192.168.100.200,把主机断网,则可以看到有断的
  
  
页: [1]
查看完整版本: nginx+keepalived用rpm安装