centos6.5安装nginx+keepalived实现高可用负载均衡

时间:2022-12-19 10:41:58
lb01  192.168.56.50 nginx主负载均衡器
lb02  192.168.56.51 nginx辅负载均衡器
web01 192.168.56.52 apache web01服务器
web02 192.168.56.53 apache web02服务器


防火墙和selinux都关闭,主机之间时间同步


chkconfig iptables off 


yum -y install gcc gcc-c++ make
yum -y install wget    
yum -y install pcre-devel     
yum -y install openssl-devel
yum -y install popt-devel 


     


wget http://nginx.org/download/nginx-1.6.2.tar.gz   
wget http://keepalived.org/software/keepalived-1.2.18.tar.gz


安装配置nginx的反向代理
useradd -s /sbin/nologin -M nginx
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
make && make install


创建软连接
ln -s /application/nginx-1.6.2/ /application/nginx


检查语法
/application/nginx/sbin/nginx -t


启动nginx
/application/nginx/sbin/nginx 


/application/nginx/sbin/nginx -s reload


检查nginx端口
netstat -lntup|grep nginx


lsof -i :80


curl 192.168.56.50


[root@lb01 conf]# cat /application/nginx/conf/nginx.conf
error_log  logs/error.log error;
worker_processes  4;
events {
    worker_connections  1024;
    use epoll;
}


http {
    server_tokens off;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 32k;
    gzip_comp_level 3;
    gzip_types  text/css text/xml application/javascript;
    gzip_vary on;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
     include extra/upstream01.conf;
}


[root@lb01 extra]# cat /application/nginx/conf/extra/proxy.conf
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;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
 
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access   user:rw  group:rw  all:r;
#proxy_temp_path      /dev/shm/nginx_proxy;
#proxy_temp_path       /data2/nginx_cache;




[root@lb01 extra]# cat /application/nginx/conf/extra/upstream01.conf 
upstream blog_real_servers {
         server 192.168.56.52:80  weight=5;
         server 192.168.56.53:80  weight=5;
}


server {
       listen       80;
       server_name  blog.etiantian.org;
       location / {
        proxy_pass http://blog_real_servers;
       }


     }








安装配置keepalived
mkdir /application
tar -xvf keepalived-1.2.18.tar.gz 
cd keepalived-1.2.18
./configure --prefix=/application/keepalived 
make && make install       
cp /application/keepalived/sbin/keepalived /usr/sbin/  
cp /application/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  
cp /application/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 


ip add
/etc/init.d/keepalived start