基于nginx+keepalived架构实现的完整过程

Nginx 安装


1.安装PCRE软件包

tar zxvf pcre-8.10.tar.gz

cd pcre-8.10

./configure

make && make install


2.安装Nginx软件包

tar zxvf nginx-1.2.6.tar.gz

cd nginx-1.2.6

useradd -s /sbin/nologin -M www

./configure --prefix=/usr/local/nginx \

--user=www --group=www \

--with-http_stub_status_module \

--with-file-aio \

--with-http_ssl_module

make && make install


3.Nginx 配置文件和启动

/usr/local/nginx/sbin/nginx -t #(加载配置文件)

/usr/local/nginx/sbin/nginx #(启动)

/usr/local/nginx/sbin/nginx -s reload#(重新加载)



Nginx 负载均衡器(1+2)


0.准备过程

useradd www

mkdir -p /data/logs/

chown -R www:www /data/logs/

ulimit -SHn 102400


1.安装 pcre 

tar zxvf pcre-8.20.tar.gz

cd pcre-8.20

./configure

make && make install


2.安装nginx

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module


3.修改nginx配置文件

user    www     www;

worker_processes 2;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events

{

        use epoll;

        worker_connections      51200;

}

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;

        sendfile on;

        tcp_nopush         on;

        keepalive_timeout 60;

        tcp_nodelay on;

        gzip on;

        gzip_min_length 1k;

        gzip_buffers         4 16k;

        gzip_http_version 1.0;

        gzip_comp_level 2;

        gzip_types           text/plain application/x-javascript text/css application/xml;

        gzip_vary on;

        upstream www.free.com {

                ip_hash;

                server 192.168.18.213:80;

                server 192.168.18.214:80;

        }

     #access log

       log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '

                            '$status $body_bytes_sent "$http_referer" '

                            '"$http_user_agent" "$http_x_forwarded_for"';

        access_log /data/logs/access.log  access;

        server {

                listen 80;

                server_name www.free.com;

                location /{

                        proxy_pass      http://www.free.com;

                        proxy_set_header Host   $host;

                        proxy_set_header X-Real-IP      $remote_addr;

                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                }

                #开启监控功能

                location /nginx{   

                        access_log off;

                        stub_status on;

                }

        }

}


4.检查配置文件

/usr/local/nginx/sbin/nginx -t


5.启动nginx 

/usr/local/nginx/sbin/nginx


6.重启加载配置文件

/usr/local/nginx/sbin/nginx -s reload





Nginx+Keepalived 做负载均衡器


1.安装 keepalived

tar zxvf keepalived-XXXX.tar.gz

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.XXXXXXXX/

make && make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

service keepalived start


2.Master与Backup 的配置文件

Master:

! Configuration File for keepalived

global_defs {

   notification_email {

      15251076067@163.com

   }

   notification_email_from king_819@163.com

   smtp_server smtp.163.com

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

# VIP1

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    mcast_src_ip 192.168.18.211

    priority 100

    advert_int 5

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.18.200

    }

}


BackUP:

! Configuration File for keepalived

global_defs {

   notification_email {

      15251076067@163.com

   }

   notification_email_from king_819@163.com

   smtp_server smtp.163.com

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

# VIP1

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    mcast_src_ip 192.168.18.212

    priority 90

    advert_int 5

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.18.200

    }

}





监控Nginx负载均衡器脚本


1.编写脚本

vim nginx_pid.sh

#!/bin/bash

while  :

do

nginxpid=`ps -C nginx --no-header | wc -l`

if [ $nginxpid -eq 0 ];then

/usr/local/nginx/sbin/nginx

sleep 5

  if [ $nginxpid -eq 0 ];then

  /etc/init.d/keepalived stop

  fi

fi

sleep 5

done


2.执行脚本

sh /root/nginx_pid.sh &

nohup /bin/bash /root/nginx_pid.sh &



本文转自 dengaosky 51CTO博客,原文链接:http://blog.51cto.com/dengaosky/1957263,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值