nginx是个用起来很爽的webserver。今天介绍一下nginx的平滑升级。

为了验证nginx是真正的平滑升级,在升级过程中使用webbench进行压力测试,查看升级过程中nginx是否工作正常。


平滑升级完全参照张宴的nginx书操作,之前需要查看现有nginx版本、编译安装的参数、进程数据等;

查看版本、编译参数:

[root@test shell]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.2.7
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
TLS SNI support disabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module


进程数据:

[root@test shell]# ps -ef |egrep -egrep|egrep nginx
root     14859     1  0 Oct22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr   16499 14859  0 Oct22 ?        00:00:00 nginx: worker process                                        
appusr   16500 14859  0 Oct22 ?        00:00:00 nginx: worker process                                        
appusr   16501 14859  0 Oct22 ?        00:00:00 nginx: worker process                                        
appusr   16502 14859  0 Oct22 ?        00:00:00 nginx: worker process


下面进行平滑升级操作:

1、下载想要升级nginx版本,然后编译覆盖安装(默认新的nginx将继续安装在旧的nginx目录下)。为了体现出升级的不同我在编译时还增加了关于user、group的定义。


wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar -zvxf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --user=appusr --group=appusr --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module  && make && make install


2、执行

[root@test nginx-1.5.6]# kill -USR2 14859  【老版本的Nginx主进程号】


3、旧版本 nginx 的主进程将重命名它的 pid 文件例如 .oldbin(如:/usr/local/nginx/logs/nginx.pid.oldbin)


4、此时,新旧两个版本的nginx进程都在运行中(默认,新版本的nginx在安装后就自动运行。)此时运行 kill -WINCH 【老版本的Nginx主进程号】 使老版本的nginx 的worker process 逐步结束;

[root@test shell]# kill -WINCH 14859
[root@test shell]# ps -ef |egrep nginx
root      4906 14859  0 11:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr    4907  4906  6 11:19 ?        00:00:04 nginx: worker process                                        
appusr    4908  4906  6 11:19 ?        00:00:04 nginx: worker process                                        
appusr    4909  4906  3 11:19 ?        00:00:02 nginx: worker process                                        
appusr    4910  4906  6 11:19 ?        00:00:04 nginx: worker process                                        
root      4978 29465  0 11:20 pts/0    00:00:00 egrep nginx
root     14859     1  0 Oct22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr   16499 14859  0 Oct22 ?        00:00:25 nginx: worker process is shutting down                       
appusr   16500 14859  0 Oct22 ?        00:00:27 nginx: worker process is shutting down                       
appusr   16502 14859  0 Oct22 ?        00:00:27 nginx: worker process is shutting down


可以看到nginx: worker process isshutting down,说明老版本的nginx worker process正在逐步关闭。


5,待所有旧版本的worker process全部退出,

仅由新的工作进程来处理输入的请求了。 使用kill -QUIT 14859 【老版本的Nginx主进程号】 从容关闭旧版本master(主进程号)。完成版本升级。

此时查看nginx版本信息、编译信息、及进程信息;

[root@test nginx-1.5.6]# /usr/local/nginx/sbin/nginx -V    
nginx version: nginx/1.5.6
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
TLS SNI support disabled
configure arguments: --user=appusr --group=appusr --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module   【可以看到新版本添加的--user --group 参数】
以下为新版本的进程信息
[root@test shell]# ps -ef |egrep nginx
root      4906     1  0 11:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr    4907  4906  8 11:19 ?        00:00:15 nginx: worker process                                        
appusr    4908  4906  8 11:19 ?        00:00:15 nginx: worker process                                        
appusr    4909  4906  8 11:19 ?        00:00:15 nginx: worker process                                        
appusr    4910  4906  9 11:19 ?        00:00:16 nginx: worker process                                        
root      5082 29465  0 11:22 pts/0    00:00:00 egrep nginx



再查看开头提到的webbench 测试我总共请求10分钟并发1000 共88万个请求仅5个请求失败。证明这次平滑升级没有影响线上服务。