nginx反向代理+keepalived

系统版本:D 2.6.32-431.el6.x86_64

虚拟机  :四台

环境准备:关闭selinux ; 关闭iptables;

         其中两台 nginx +keepalive已安装且正常;

         另外两台的节点web工作正常;

#为了更好的测试, web 1:www.22  web 2:www.23 ;

nginx安装:

 yum install pcre* -y

 yum install openssl openssl-devel -y

 useradd -s /sbin/nologin -M nginx

 wget -q http://nginx.org/download/nginx-1.6.2.tar.gz

 ./configure  --user=nginx --group=nginx --prefix=/application/nginx1.6.2 --with-http_stub_status_module  --with-http_ssl_module

    echo $?

    make && make install

    echo $?

ln -s /application/nginx1.6.2/ /application/nginx

    /application/nginx/sbin/nginx


keepalived 安装:

     wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

     ln -s /usr/src/kernels/2.6.32-504.1.3.el6.x86_64/ /usr/src/linux

     yum install openssl* -y

     yum install popt* -y

     cd keepalived-1.1.19

     ./configure

      make     

      make install

 

    /bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
    /bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
    mkdir /etc/keepalived -p
    /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
    /bin/cp /usr/local/sbin/keepalived /usr/sbin/
 
    etc/init.d/keepalived  start

nginx 反向代理配置范例

      cd   /application/nginx/conf/extra/

      vi upstream01.conf 
        upstream blog_real_servers {
            #ip_hash;
             server 10.0.0.22:80  weight=15;
             server 10.0.0.23:80  weight=15
        }
        server {
           listen       80;
           server_name  blog.etiantian.org;
           location / {
            proxy_pass http://blog_real_servers;

            #代理多个虚拟主机需添加: proxy_set_header host $host ;

            
           }
        }

在主配置文件nginx.conf 里http标签包含配置文件extra/upstream01.conf;模块

     include extra/upstream01.conf;

注意:用nginx 做反向代理的时候尽量不要做web。

     一个nginx也可以代理多个网站,使用多个vip。进行不同的解析。

#两台服务器都需要配置

keepalived配置:

    vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived

        global_defs {
           notification_email {
           [email protected]
           }
           notification_email_from [email protected]
           smtp_server 10.0.0.33
           smtp_connect_timeout 30
           router_id LVS_7
        }

        vrrp_instance VI_1 {
            state MASTER
            interface eth0
            virtual_router_id 55
            priority 150
            advert_int 1
            authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.33/24
        }

        }


在检查上述配置无误的情况下,启动两台的nginx和keepalive

    /application/nginx/sbin/nginx  -s reload

    /etc/init.d/keepalived restart

本机配置hosts文件:解析vip 域名

测试:

wKioL1ScP7KwoJoOAACNJo_vB3E808.jpg


wKiom1ScP5ihJpDTAACJVrlfJZU326.jpg



#如果多次访问为同一台主机,检查nginx的weight=15 权重是否一直。


将其中一台web服务停止,检测域名是否可以正常访问~

如果两台web同时停止服务,访问时返回:502 Bad Gateway

将其中一台主机的keepalive停止,测试web服务是否正常。

完毕!




本文出自 “思想大于技术” 博客,谢绝转载!

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