Nginx(7)

nginx的编译安装

  1. 线上业务已有nginx,要新上一个业务,按照之前的安装方式进行。
    先nginx -V获取所有的编译参数,然后使用nginx编译参数进行安装
  2. 线上业务已有nginx,要新上一个业务,还有第三方模块需要进行安装
    先nginx -V获取所有的编译参数,然后使用nginx以及第三方模块的编译参数进行安装
    安装nginx示例:
下载安装、解压
wget http://nginx.org/download/nginx-1.14.2.tar.gz
	[root@nfs ~]# tar xf nginx-1.14.2.tar.gz
	[root@nfs ~]# cd nginx-1.14.2/
安装nginx编译安装所需要的库文件与开发包
yum install gcc redhat-rpm-config \
	libxslt-devel gd-devel perl-ExtUtils-Embed \
	geoip-devel gperftools-devel pcre-devel openssl-devel -y

configure
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

make && make install
直接使用make与make install命令编译安装即可。


安装nginx并添加第三方模块安装方法:

0) 安装Nginx所依赖的库文件或开发包
yum install gcc redhat-rpm-config \
	libxslt-devel gd-devel perl-ExtUtils-Embed \
	geoip-devel gperftools-devel pcre-devel openssl-devel -y


1)下载软件、解压
	[root@nfs ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
	[root@nfs ~]# tar xf nginx-1.14.2.tar.gz
	[root@nfs ~]# cd nginx-1.14.2/

2)编译
	./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

	make
	make install 


2.请给Nginx1.14版本添加第三方模块? nginx_upstream_check_module

	[root@nfs ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
	[root@nfs ~]# unzip master.zip
	[root@nfs ~]# cd nginx-1.14.2/
	[root@nfs nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch


	./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

	make 
	make install


	验证下该模块是否可用?


[root@nfs nginx-1.14.2]# cat  /etc/nginx/conf.d/upstream_check.conf
upstream blog.oldxu.com {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
    #interval检测间隔时间,单位为毫秒
    #rsie表示请求2次正常,标记此后端的状态为up
    #fall表示请求3次失败,标记此后端的状态为down
    #type  类型为tcp
    #timeout为超时时间,单位为毫秒
}

upstream webserver {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}

upstream php {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}


server {
    listen 8888;

    location / {
        proxy_pass http://blog.oldxu.com;
    }

    location /upstream_status {
        check_status;     #开启upstream状态页面
    }
}


平滑升级原理

在这里插入图片描述

nginx平滑升级步骤

# 升级
1. 下载新版本的nginx
2. 了解原旧版的nginx编译参数
3. 讲就的nginx二进制文件进行备份,然后替换成新的nginx二进制文件
4. 向旧的nginx的master进程发送usr2信号
5. 旧的master进程会修改pid文件,添加后缀.oldbin
6. master进程会用新的nginx文件启动新的master进程
7. 向旧的master进程发送winch信号,旧的worker子进程退出
8. 向旧的master进程发送quit信号,旧的master进程退出了

# 回滚
1. 替换旧的nginx二进制文件
2. 向旧的master发送USR2信号
3. 向旧的master发送winch信号
4. 向旧的master新城发送quit信号

nginx平滑升级实战

4.实现Nginx平滑升级实践

 
1)安装Nginx所需依赖包
    [root@web01 ~]# yum install gcc redhat-rpm-config \
    libxslt-devel gd-devel perl-ExtUtils-Embed \
    geoip-devel gperftools-devel pcre-devel openssl-devel -y
 
 
2) 下载并编译Nginx
    [root@nfs ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
    [root@nfs ~]# tar xf nginx-1.16.1.tar.gz
    [root@nfs ~]# cd nginx-1.16.1/
    [root@nfs nginx-1.16.1]# rm -f /etc/nginx/conf.d/upstream_check.conf    #由于该三方模块不兼容1.16版本,所以拿掉避免升级出错
 
 
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
 
    make    #仅make即可,不需要make install。因为make后会生成新的二进制文件   ( 新的汽车发送机引擎 )
 
 
3) 将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件
    [root@nfs nginx-1.16.1]# mv /usr/sbin/nginx /usr/sbin/nginx.old
    [root@nfs nginx-1.16.1]# cp objs/nginx /usr/sbin/nginx
 
4) 向旧的Nginx的Master进程发送USR2信号。( 平滑升级二进制可执行文件 )
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     20863  20848  0 10:22 ?        00:00:00 nginx: worker process
nginx     20864  20848  0 10:22 ?        00:00:00 nginx: worker process
 
#发送信号
[root@nfs nginx-1.16.1]# kill -USR2 20848
 
#pid文件会自动添加.oldbin后缀,该文件中记录的是旧master的pid进程号 
[root@nfs nginx-1.16.1]# cat  /var/run/nginx.pid.oldbin
20848
 
 
#新老master共存了
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf    (OLD)
nginx     20863  20848  0 10:22 ?        00:00:00 nginx: worker process
nginx     20864  20848  0 10:22 ?        00:00:00 nginx: worker process
root      24971  20848  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf    (NEW)
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process
 
 
#验证站点是否正常
 
5)向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。
[root@nfs nginx-1.16.1]# kill -WINCH 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root      24971  20848  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process
 
 
6) 向旧的master进程发送QUIT信号,旧的master进程就退出了。
[root@nfs nginx-1.16.1]# kill -QUIT 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process
 
 
 
回滚:
    1.替换nginx二进制文件
    2.向旧的master发送USR2信号
    3.向旧的master发送SIGWINCH
    4.向旧的master发送QUIT
 
 

1.准备一个大的文件,让客户端持续下载着,然后在来进行回滚操作,看看是否会出现中断
[root@nfs nginx-1.16.1]# dd if=/dev/zero of=/usr/share/nginx/html/bigdata bs=200M count=1

  1. 替换nginx二进制文件
    [root@nfs ~]# mv /usr/sbin/nginx /usr/sbin/nginx-1.16
    [root@nfs ~]# mv /usr/sbin/nginx.old /usr/sbin/nginx

  2. 向旧的master发送USR2信号
    [root@nfs ~]# ps -ef |grep nginx
    root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx 25124 24971 0 11:48 ? 00:00:00 nginx: worker process
    nginx 25125 24971 0 11:48 ? 00:00:00 nginx: worker process

[root@nfs ~]# kill -USR2 24971
[root@nfs ~]# ps -ef |grep nginx
root 24971 1 0 11:37 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf (OLD 1.16)
nginx 25124 24971 0 11:48 ? 00:00:00 nginx: worker process
nginx 25125 24971 0 11:48 ? 00:00:00 nginx: worker process
root 25163 24971 1 11:50 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf (NEW 1.14)
nginx 25164 25163 0 11:50 ? 00:00:00 nginx: worker process
nginx 25165 25163 0 11:50 ? 00:00:00 nginx: worker process


 
 
3) 向旧的master发送WINCH信号,优雅关闭旧的Worker工作进程。
[root@nfs ~]# kill -WINCH 24971
[root@nfs ~]# ps  -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25124  24971  0 11:48 ?        00:00:00 nginx: worker process is shutting down
root      25163  24971  0 11:50 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25164  25163  0 11:50 ?        00:00:00 nginx: worker process
nginx     25165  25163  0 11:50 ?        00:00:00 nginx: worker process
 
 
4) 向旧的master发送QUIT信号,退出该master进程。
[root@nfs ~]# kill -QUIT 24971
[root@nfs ~]# ps -ef |grep nginx
root      25163      1  0 11:50 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25164  25163  0 11:50 ?        00:00:00 nginx: worker process
nginx     25165  25163  0 11:50 ?        00:00:00 nginx: worker process
 

对应升级或回滚对应的信号
QUIT 优雅关闭、quit
HUP 优雅重启、reload
USR1 重新打开日志文件、reopen
USR2 平滑升级可执行的二进制程序。
WINCH 平滑关闭Worker进程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值