Nginx 高可用编译安装

1.1安装pcre

解压缩pcre-8.34.tar.gz包。
进入解压缩目录,执行./configure。
make & make install

1.2安装openssl

解压缩openssl-1.0.2q.tar.gz包
进入解压缩目录,执行./config
 make & make install

1.3安装zlib

解压缩zlib-1.2.11.tar.gz包。
进入解压缩目录,执行./configure。
make & make install

1.4编译安装nginx

1)解压缩nginx-xx.tar.gz包,并编译

 参数设置如下:

./configure
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

2)安装

make & make install

1.5修改nginx 文件配置

user nobody nobody; #所有用户都可以启动Nginx进程
worker_processes  8; #允许生成的进程数
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  debug; #指定日志路径 级别
pid        logs/nginx.pid; #指定nginx 进程运行文件存放地址
events {
         use epoll;  #使用epoll 模型作为事件驱动模型
         worker_connections  1024; #支持的最大连接数设置为 1024
         accept_mutex on; #设置网络序列化,当一个新连接到到时,多个worker串行执行,只有一个worker 被唤醒,避免资源浪费
         multi_accept on; #设置一个进程可以同时接受多个网络连接
}
http {
         include       mime.types; #文件扩展名与文件类型映射表
         default_type  application/octet-stream; #如果web程序未设置,映射表中也没有文件扩展名,使用默认的未知程序类型,流文件
         log_format myformat '$remote_addr-$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义日志格式
         access_log  logs/access.log myformat; #定义服务日志路径及格式
         client_header_buffer_size    1k; #(请求头+请求行)的大小不能超过1K
         large_client_header_buffers  4 1m; 请求行不能超过1M,(请求头+请求行)不超过 4*1M
         client_max_body_size 10m;  #设置nginx 上传文件大小限制
         client_body_buffer_size 128k;
         proxy_connect_timeout 360;
         proxy_send_timeout 360;
         proxy_read_timeout 360;
         #proxy_buffer_size 4k;
         #proxy_buffers 4 32k;
         #proxy_busy_buffers_size 64k;
         #proxy_temp_file_write_size 64k;
         #proxy_temp_path /opt/nginxproxy/temp_dir;
         #proxy_cache_path /opt/nginxproxy/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30g;
         #proxy_hide_header Set-Cookie;
         sendfile        on; #使用sendfile 的方式传输文件
         keepalive_timeout  65; #连接超时时间
         tcp_nodelay     on;
         
         upstream teis_server {
                   ip_hash;
                   server 10.2.4.55:9090 max_fails=3 weight=1 fail_timeout=60s;
                   server 10.2.4.56:9090 max_fails=3 weight=1 fail_timeout=60s;
        keepalive 256;
         }
         server {
                   listen       80;
                   server_name  teis_server;
                   
                   location  / {
                            proxy_pass http:// teis_server;
                           proxy_redirect default;
                            proxy_set_header   Host $host;
                            proxy_set_header X-Real-IP $remote_addr;
                            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                   }
         }
}

1.6启动nginx

cd /usr/local/nginx/sbin/
./nginx

查询nginx进程:

15098是nginx主进程的进程id,15099是nginx工作进程的进程id

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))

1.7停止nginx

1)方式1,快速停止:

cd /usr/local/nginx/sbin
./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

2)方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待 nginx 进程处理任务完毕进行停止。
1.8 重启 nginx
1)方式 1 ,先停止再启动(建议使用):
nginx 进行重启相当于先停止 nginx 再启动 nginx ,即先执行停止命令再执行启动命令。
如下:
./nginx -s quit
./nginx

2)方式2,重新加载配置文件:

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

1.9 安装keepalived  

1.9.1编译安装keepalived

tar -zxvf keepalived-1.2.7.tar.gz
yum install -y openssl openssl-devel
yum install popt-devel
cd keepalived-1.2.7
./configure --prefix=/usr/local/keeplived

如果配置执行报错:

open-devel 安装失败,需要重新安装

make
make install
cp /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keeplived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keeplived/sbin/keepalived /usr/sbin/
加入启动服务
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/etc/init.d/keepalived start" >> /etc/rc.local

 

1.9.2配置keepalived

按照上面的安装方法, keepalived 的配置文件在 /etc/keepalived/keepalived.conf 。主、从服务器的配置相关联但有所不同。如下:
Master:
! Configuration File for keepalived
global_defs {
  router_id NGINX_DEVEL  #标识节点的字符串,通常为hostname
}
#定义虚拟路由 VI_1 为自定义标识
vrrp_instance VI_1 {
    state MASTER  #主节点为Master,备节点为BACKUP
    interface eth0  #绑定虚拟IP的网络接口,与本机IP地址所在的网络接口相同
    virtual_router_id 51 #虚拟路由ID号
    priority 101       #优先级配置(0-254的值)
    advert_int 1      #组播信息发送间隔,两个节点必须配置一致,默认1s
    authentication {
        auth_type PASS
        auth_pass 1111  ##真实环境下对密码进行匹配
    }
    virtual_ipaddress {
       192.168.1.100
    }
}

Backup:

! Configuration File for keepalived
global_defs {
  router_id NGINX_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.1.100
    }
}
验证:
先后在主、从服务器上启动 keepalived: /etc/init.d/keepalived start
在主服务器上查看是否已经绑定了虚拟 IP ip addr
停止主服务器上的 keepalived: /etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟 IP
启动主服务器上的 keepalived ,看看主服务器能否重新接管虚拟 IP

1.9.3 keepalived监控NginX的状态

经过前面的配置,如果主服务器的 keepalived 停止服务,从服务器会自动接管 VIP 对外服务;一旦主服务器的 keepalived 恢复,会重新接管 VIP 但这并不是我们需要的,我们需要的是当 NginX 停止服务的时候能够自动切换。
keepalived 支持配置监控脚本,我们可以通过脚本监控 NginX 的状态,如果状态不正常则进行一系列的操作,最终仍不能恢复 NginX 则杀掉 keepalived ,使得从服务器能够接管服务。
如何监控 NginX 的状态
最简单的做法是监控 NginX 进程,更靠谱的做法是检查 NginX 端口,最靠谱的做法是检查多个 url 能否获取到页面。
如何尝试恢复服务
如果发现 NginX 不正常,重启之。等待 3 秒再次校验,仍然失败则不再尝试。
根据上述策略很容易写出监控脚本。这里使用 nmap 检查 nginx 端口来判断 nginx 的状态,记得要首先安装 nmap
监控脚本如下 :
#!/bin/bash
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=8080
nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne 0 ];then
    $NGINX -s stop
    $NGINX
    sleep 3
    nmap localhost -p $PORT | grep "$PORT/tcp open"
    [ $? -ne 0 ] && /etc/init.d/keepalived stop
fi

不要忘了设置脚本的执行权限,否则不起作用。

假设上述脚本放在/opt/chk_nginx.sh,则keepalived.conf中增加如下配置:

主keepalived

vrrp_script chk_http_port {
    script "/opt/chk_nginx.sh"
    interval 1
    weight -2
}
track_script {
    chk_http_port
}

例子:

! Configuration File for keepalived

global_defs {
   router_id NGINX_UPSTEAM
}
vrrp_script chk_http_port {
    script "/opt/chk_nginx.sh"
    interval 1
    weight -2
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
    track_script {
    chk_http_port
    }
}

更进一步,为了避免启动keepalived之前没有启动nginx , 可以在/etc/init.d/keepalived的start中首先启动nginx:

start() {
    /usr/local/nginx/sbin/nginx
    sleep 3
    echo -n $"Starting $prog: "
    daemon keepalived ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行客-编码爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值