nginx-keepalived双主互备反向代理

安装NGINX

yum install -y gcc gcc-c++ openssl-devel zlib-devel
tar xf pcre-8.34.tar.gz 
cd pcre-8.34
./configure && make && make install 
cd ..
cd nginx-1.4.7
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module
make && make install


Nginx配置文件:

user    www www;
worker_processes 1;
error_log    /usr/local/nginx/logs/nginx_error.log    crit;
pid                /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 20000;
 
events
{
         use epoll;
         worker_connections 20000;   
}
http
{
include mime.types;
      default_type application/octet-stream;
      charset utf-8;
      server_names_hash_bucket_size 128;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;
      client_max_body_size 300m;
 
      sendfile on;
      tcp_nopush on;
      keepalive_timeout 60;
      tcp_nodelay on;
      client_body_buffer_size 512k;
 
 proxy_connect_timeout 30;
       proxy_send_timeout 30;
       proxy_read_timeout 60;
       proxy_buffer_size 256k;
       proxy_buffers 4 256k;
      proxy_busy_buffers_size 256k;
      proxy_temp_file_write_size 256k;
  proxy_max_temp_file_size 128m;
      gzip on;
      gzip_min_length 1k;
      gzip_buffers 4 16k;
      gzip_http_version 1.1;
      gzip_comp_level 2;
      gzip_types text/plainapplication/x-javascript text/css application/xml;
      gzip_vary on;
 
upstream cache.dragon.com {
          server 192.168.150.128:80 weight=8 max_fails=2 fail_timeout=30s; 
          Server 192.168.150.133:80 weight=8 max_fails=2 fail_timeout=30s;
             }
server
     {
        listen             80;
               server_name    www.dragon.com
              charset GB2312;
             index index.html index.htm;
            root    /usr/local/nginc/html/;
               location ~ ^/NginxStatus/ {
                        stub_status on;
                        access_log off;
                        }
         location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 50m;
                client_body_buffer_size 256k;
                proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
                proxy_max_temp_file_size 128m;
                proxy_pass    http://cache.dragon.com;
                }
}
 
}


 

Keepalived配置(另一keepalived文件不同之处,已用"#"标出):

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 220.181.12.11
   smtp_connect_timeout 30
   router_id haproxy_DEVEL
}
 
 
vrrp_script check_nginx {
   script "killall -0 nginx"
   interval  2
 
}
 
 
 
vrrp_instance VI_1 {
    state MASTER    #改为SLAVE
    interface eth5    
    virtual_router_id 51
    priority 80    #改为100
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1234
    }
 
 
   track_script {
    check_haproxy
}    
 
   virtual_ipaddress {
        22.22.22.121/24 dev eth5
    
    }
}
 
vrrp_instance VI_2 {
    state MASTER    #改为SLAVE
    interface eth5
    virtual_router_id 52
    priority 100    #改为80
    advert_int 1
 
    authentication {
        auth_type PASS
        auth_pass 1234
    }
 
 
   track_script {
    check_nginx
        }
 
   virtual_ipaddress {
        22.22.22.122/24 dev eth5
 
    }
}

virtual_server  22.22.22.121/24 80 {   
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    persistence_timeout 50   
    protocol TCP   
  
    real_server 192.168.150.128 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
	}
   real_server 192.168.150.127 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
  
    }   
}
virtual_server  22.22.22.122/24 80 {   
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    persistence_timeout 50   
    protocol TCP   
  
    real_server 192.168.150.128 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
	}
   real_server 192.168.150.127 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
  
    }   
}


[root@centos-server ~]# service keepalived start

[root@centos-server ~]# service nginx start

查看两台服务器的网卡是否被挂载虚拟IP:

wKioL1SjYf_Q3EpPAAK-r34Yerg567.jpg

wKiom1SjYUny4giGAALGU8rhiJ4131.jpg

停掉其中一台服务器的nginx:

wKiom1SjYYGAFPc7AALEsLaq4uI841.jpg


测试动态网页是否被代理:

wKiom1SjYcfTDo6JAAC42eqPNEo647.jpg

wKioL1SjYn3xoSZVAADCYU5AlSs439.jpg


本文出自 “龙爱雪琪” 博客,转载请与作者联系!

你可能感兴趣的:(nginx,反向代理)