博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于nginx+keepalived架构实现的完整过程
阅读量:6449 次
发布时间:2019-06-23

本文共 3863 字,大约阅读时间需要 12 分钟。

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,如需转载请自行联系原作者

你可能感兴趣的文章
Java 中的异常和处理详解
查看>>
不是技术牛人,如何拿到国内IT巨头的Offer
查看>>
centos7安装mysql
查看>>
Xmind教程之五种主题
查看>>
cs224d 课程学习第一课
查看>>
Linux 挂载光盘:mount: you must specify the filesystem
查看>>
OSChina 周三乱弹 —— 写代码强迫症
查看>>
OSChina 儿童节乱弹 —— 六一不能让童工加班!
查看>>
linux command
查看>>
新魔方玩法
查看>>
golang标准包中文版
查看>>
使用ghost.py(webkit)来爬取网页
查看>>
聆听这寂寞与宿命 !!!
查看>>
【转】NAS备份所面临的挑战
查看>>
深入浅出JS的封装与继承
查看>>
从字节码角度看String、StringBuffer、StringBuilder的不同
查看>>
MySQLdb简单查询
查看>>
Docker系列学习之一——何为Docker?
查看>>
关于Facebook API 获取Page主页的永久access_token
查看>>
Shuffle 调优之 HashShuffleManager 和 SortShuffleManager
查看>>