nginx平滑升级

介绍:因为nginx缺少某个模块,或者想升级一下版本,但是nginx上有运载着一些服务,不能挂掉,那么nginx的平滑升级就很关键了!

nginx信号检测
主进程支持的信号:
TERM,INT:立刻退出
QUIT:等待工作进程结束再退出
KILL:强制终止进程
HUP:重新加载配置文件,使用新的的配置启动工作进程,并逐步关闭旧进程
USR1:重新打开日志文件
USR2:启动新的主进程,实现热升级
WINCH:逐步关闭工作进程
工作进程支持的信号
TERM,INT:立刻退出

nginx平滑升级的原理:
在不停掉旧进程的情况下,启动新进程。
旧进程负责处理仍然没有处理完的请求,但不再接受新请求。
新进程接受新请求。
旧进程处理完所有请求,关闭所有连接后,停止。

1、先安装低版本的nginx

[root@server104 ~/nginx-1.12.0]$ wget http://nginx.org/download/nginx-1.12.0.tar.gz   #利用wget去网上下载需要的包

[root@server104 ~/nginx-1.12.0]$ tar xvf nginx-1.12.0.tar.gz   #解压

[root@server104 ~/nginx-1.12.0]$ cd nginx-1.12.0   #进入目录

[root@server104 ~/nginx-1.12.0]$ useradd nginx -s /sbin/nologin   #创建一个专门用于启动nginx的用户,但不能够登陆主机

[root@server104 ~/nginx-1.12.0]$  ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx  --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre    #选择需要的选项来预安装

#我们可以查看一下预安装有哪些选项

[root@server104 ~/nginx-1.12.0]$ ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

[root@server104 ~/nginx-1.12.0]$ make && make install   #安装

[root@server104 ~/nginx-1.12.0]$ ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx   #创建一个快捷启动方式

[root@server104 ~/nginx-1.12.0]$ /usr/local/nginx/sbin/nginx   #启动nginx,使用绝对路径更加安全可靠

[root@server104 ~/nginx-1.14.0]$ ps -aux | grep nginx
root      10065  0.0  0.0  46060  1152 ?        Ss   10:50   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10066  0.0  0.1  46516  1908 ?        S    10:50   0:00 nginx: worker process
root      10074  0.0  0.0 112812   968 pts/1    R+   10:51   0:00 grep --color=auto nginx
[root@server104 ~/nginx-1.12.0]$ netstat -tupanl | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7088/nginx: master

nginx平滑升级_第1张图片

访问成功

[root@server104 ~]$ echo 777777 > /usr/local/nginx/html/index.html

[root@server104 ~]$ curl 192.168.20.104
777777

[root@server104 ~]$ nginx -v  #查看nginx版本信息
nginx version: nginx/1.12.0
[root@server104 ~]$ nginx -V  #查看nginx版本完全信息
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre

2、下载更高版本的nginx

[root@server104 ~]$ wget http://nginx.org/download/nginx-1.14.0.tar.gz

[root@server104 ~]$ cd nginx-1.14.0
[root@server104 ~/nginx-1.14.0]$ ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

[root@server104 ~/nginx-1.14.0]$  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --with-pcre --with-stream  \#--prefix的位置必须和旧版本的nginx位置一样

[root@server104 ~/nginx-1.14.0]$ make   #记住这里只能make,不能make install,如果make install,会导致旧的配置被覆盖掉

为了保险起见,我们可以备份原来的二进制启动文件

[root@server104 ~/nginx-1.14.0]$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

[root@server104 ~/nginx-1.14.0]$ ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README

[root@server104 ~/nginx-1.14.0]$ cp objs/nginx /usr/local/nginx/sbin/    \#把编译好的新的启动文件复制进来

查看一下nginx进程

[root@server104 ~/nginx-1.14.0]$ ps -aux | grep nginx
root      10065  0.0  0.0  46060  1152 ?        Ss   10:50   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10066  0.0  0.1  46516  1908 ?        S    10:50   0:00 nginx: worker process
root      10074  0.0  0.0 112812   968 pts/1    R+   10:51   0:00 grep --color=auto nginx

[root@server104 ~/nginx-1.14.0]$ cat /usr/local/nginx/logs/nginx.pid
10065

[root@server104 ~/nginx-1.14.0]$ kill -USR2 10065  \#USR2:启动新的主进程,实现热升级

kill -USR2 10065  #发送USR2信号给nginx master 进程(nginx服务接收到USR2信号后,首先会将旧的nginx.pid文件添加后缀.oldbin,变为nginx.pid.oldbin,然后执行新版本的二进制文件启动服务,如果新的服务启动成功,系统中将有新旧两个nginx服务共同提供web服务)

[root@server104 ~/nginx-1.14.0]$ ps -aux | grep nginx
root      10065  0.0  0.0  46060  1312 ?        Ss   10:50   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10066  0.0  0.1  46516  1908 ?        S    10:50   0:00 nginx: worker process
root      10076  0.0  0.1  46060  3280 ?        S    10:51   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10077  0.0  0.1  46516  1900 ?        S    10:51   0:00 nginx: worker process
root      10079  0.0  0.0 112812   972 pts/1    R+   10:51   0:00 grep --color=auto nginx

[root@server104 ~/nginx-1.14.0]$ cat /usr/local/nginx/logs/nginx.pid  #查看nginx高版本的进程号
10076
[root@server104 ~/nginx-1.14.0]$ cat /usr/local/nginx/logs/nginx.pid.oldbin   #查看nginx原来版本的进程号
10065
[root@server104 ~/nginx-1.14.0]$ kill -WINCH 10065   #平缓停止worker process
[root@server104 ~/nginx-1.14.0]$ kill -QUIT 10065   #平缓停止Nginx服务
[root@server104 ~/nginx-1.14.0]$ ps -aux | grep nginx     #我们可以发现nginx低版本的进程都结束了
root      10076  0.0  0.1  46060  3280 ?        S    10:51   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10077  0.0  0.1  46516  1900 ?        S    10:51   0:00 nginx: worker process
root      10091  0.0  0.0 112812   972 pts/1    R+   10:58   0:00 grep --color=auto nginx

[root@server104 ~/nginx-1.14.0]$ ll /usr/local/nginx/logs/    #nginx.pid.oldbin文件也没有了
total 12
-rw-r--r-- 1 root root 686 Jan 20 09:06 access.log
-rw-r--r-- 1 root root 458 Jan 20 10:51 error.log
-rw-r--r-- 1 root root   6 Jan 20 10:51 nginx.pid
[root@server104 ~/nginx-1.14.0]$ nginx -v
nginx version: nginx/1.14.0
[root@server104 ~/nginx-1.14.0]$ nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --with-pcre --with-stream

那么现在,nginx平滑升级就成功了!

你可能感兴趣的:(nginx,运维,服务器)