1、平滑升级Nginx
Kill命令传输信号给Nginx的主进程,下面这几个信息命令都是发送给nginx的master主进程的pid
TERM, INT(快速退出,当前的请求不执行完成就退出) -s stop
QUIT (优雅退出,执行完当前的请求后退出) -s quit
HUP (重新加载配置文件,用新的配置文件启动新worker进程,并优雅的关闭旧的worker进程) -s reload
USR1 (重新打开日志文件) -s reopen
USR2 (平滑的升级nginx二进制文件 拉起一个新的主进程master 旧主进程不停止)
WINCH (优雅的关闭worker子进程)
Kill 选项参数 pid nginx的master主进程的pid
kill -INT pid 快速关闭
kill -QUIT pid 优雅关闭
重新安装Nginx
1停止掉服务,删除编译的安装的软件包和源码包
2重新解压编译安装即可,有需要就备份配置文件和网站目录里的资源文件
平滑升级Nginx
升级软件版本之后,需要启动新的版本,启动不了,端口已经被旧版本占用。
如果直接把旧版本的服务停止掉,会影响线上业务的使用。
最佳解决办法:
1旧的不先停掉
2新的又可以起来
3旧的和新的同时提供服务,旧的请求完成之后,就停掉旧进程
kill -USR2 旧的主进程号 平滑启动一个新主进程(平滑升级)
kill -WINCH 旧的主进程号 优雅关闭旧子进程
kill -QUIT 旧的主进程号 优雅关闭旧主进程
也可以直接 kill -USR2 旧的主进程号 再kill -QUIT 旧的主进程号
tar xvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
[root@linux nginx-1.16.0]# /usr/local/nginx/sbin/nginx -V 查看版本和配置选项并退出
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
make && make install
升级新版本,需要把软件的安装路径,指定到旧版本上。
以上操作完成之后,会把原来的旧版本备份为nginx.old(/usr/local/nginx/sbin/nginx.old)
[root@linux sbin]# ls
nginx nginx.old
[root@linux sbin]# ./nginx 新版本起不来,旧版本占用了80端口
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
[root@linux sbin]# ./nginx -v 查看新版本
nginx version: nginx/1.16.0
[root@linux sbin]# ./nginx.old -v 查看旧版本
nginx version: nginx/1.14.2
[root@linux sbin]# ps -aux |grep nginx
root 37209 0.0 0.0 45956 1148 ? Ss 22:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 37210 0.0 0.0 46412 1924 ? S 22:38 0:00 nginx: worker process
root 39974 0.0 0.0 112708 972 pts/0 S+ 22:59 0:00 grep --color=auto nginx
[root@linux sbin]# cat /usr/local/nginx/logs/nginx.pid
37209
新旧版本同时运行,kill -USR2 主进程号
[root@linux sbin]# kill -USR2 37209
[root@linux sbin]# ps -aux |grep nginx
root 37209 0.0 0.0 45956 1332 ? Ss 22:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 37210 0.0 0.0 46412 1924 ? S 22:38 0:00 nginx: worker process
root 40001 0.0 0.0 45980 3388 ? S 23:01 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 40002 0.0 0.0 46444 1924 ? S 23:01 0:00 nginx: worker process
root 40004 0.0 0.0 112708 976 pts/0 S+ 23:01 0:00 grep --color=auto nginx
旧的和新的同时提供服务,旧的请求完成之后,就停掉旧进程
kill -USR2 旧的主进程号 平滑启动一个新主进程(平滑升级)
kill -WINCH 旧的主进程号 优雅关闭旧子进程
kill -QUIT 旧的主进程号 优雅关闭旧主进程
也可以直接 kill -USR2 旧的主进程号 再kill -QUIT 旧的主进程号
在nginx中,默认提供了平滑升级的操作,只需要执行以下命令
在nginx源码包先执行./configure
make install && make upgrade
原理是执行这个文件/root/soft/nginx-1.16.0/Makefile里面upgrade内容包含kill命令
Nginx配置文件/usr/local/nginx/nginx.conf
#nginx子进程启动用户
#user nobody;
#子进程数量 一般调整为cpu核数或者倍数
worker_processes 1;
#错误日志定义
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程pid 存储文件
#pid logs/nginx.pid;
#事件
events {
#每个子进程的连接数 nginx当前并发量 worker_processes * worker_connections
worker_connections 1024;
}
#http协议段
http {
#引入 文件扩展名和与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#访问日志access.log的格式
#log_format main '$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 main;
#linux内核 提供文件读写的机制
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#长连接超时时间 单位为s
keepalive_timeout 65;
#gzip压缩
#gzip on;
#server虚拟主机的配置
server {
#监听端口
listen 80;
#域名 可以有多个 用空格分隔
server_name localhost;
#默认编码
#charset koi8-r;
#access_log logs/host.access.log main;
#location 用来匹配url
location / {
#默认访问的网站路径
root html;
#默认访问页面 从前往后的顺序查找
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}