高性能Web服务器Nginx的配置与部署研究(14)平滑升级你的Nginx

1、概述(可以直接跳过看第2部分)

Nginx方便地帮助我们实现了平滑升级。其原理简单概括,就是:

(1)在不停掉老进程的情况下,启动新进程。

(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。

(3)新进程接受新请求。

(4)老进程处理完所有请求,关闭所有连接后,停止。

这样就很方便地实现了平滑升级。一般有两种情况下需要升级Nginx,一种是确实要升级Nginx的版本,另一种是要为Nginx添加新的模块。

2. 升级过程

具体的操作也很简单,如下:

(0)查看当前版本

在存放Nginx的可执行文件的目录下输入:

  1. ./nginx -V

(1)下载新的Nginx版本并编译。

  1. wget nginx-1.0.11.tar.gz
  2. tar zxvf nginx-1.0.11.tar.gz
  3. cd nginx-1.0.11
  4. ./configure --add-module=/customized_module_0 --add-module=/customized_module_1
  5. make

注意不要进行make install

(2)备份老版本的可执行文件

  1. cd /usr/local/nginx/sbin
  2. sudo cp nginx nginx.old

(3)修改配置文件

如果有必要的话,进行配置文件的修改。

(4)拷贝新的可执行文件

  1. sudo cp /home/michael/tmp/nginx-1.0.11/objs/nginx /usr/local/nginx/sbin/

(5)升级

  1. cd /home/michael/tmp/nginx-1.0.11
  2. make upgrade

(6)清理多余文件

  1. rm -rf /home/michael/tmp/nginx-1.0.11

(7)查看Nginx版本

  1. cd /usr/local/nginx/sbin
  2. ./nginx -V

3、观察进程变化

在我的机器上可以观察到,我配置的是10个worker进程,启动后观察到:

  1. root      6241 10419  0 10:51 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  2. nobody    6242  6241  2 10:51 ?        00:00:00 nginx: worker process
  3. nobody    6243  6241  0 10:51 ?        00:00:00 nginx: worker process
  4. nobody    6244  6241  0 10:51 ?        00:00:00 nginx: worker process
  5. nobody    6245  6241  0 10:51 ?        00:00:00 nginx: worker process
  6. nobody    6246  6241  0 10:51 ?        00:00:00 nginx: worker process
  7. nobody    6247  6241  0 10:51 ?        00:00:00 nginx: worker process
  8. nobody    6248  6241  0 10:51 ?        00:00:00 nginx: worker process
  9. nobody    6249  6241  0 10:51 ?        00:00:00 nginx: worker process
  10. nobody    6250  6241  0 10:51 ?        00:00:00 nginx: worker process
  11. nobody    6251  6241  1 10:51 ?        00:00:00 nginx: worker process
  12. nobody    6252  6241  0 10:51 ?        00:00:00 nginx: cache manager process
  13. nobody    6253  6241  0 10:51 ?        00:00:00 nginx: cache loader process
  14. luming    6310 25051  0 10:51 pts/1    00:00:00 grep --color=auto nginx
  15. nobody    7995 10419  0 Jan12 ?        00:20:37 nginx: worker process is shutting down
  16. nobody    7996 10419  0 Jan12 ?        00:20:11 nginx: worker process is shutting down
  17. nobody    7998 10419  0 Jan12 ?        00:20:04 nginx: worker process is shutting down
  18. nobody    8003 10419  0 Jan12 ?        00:20:12 nginx: worker process is shutting down
  19. root     10419     1  0 Jan08 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

可见新的进程有1个master和10个worker,另外还有1个老的master(可以从时间上看出)和4个worker(其他6个老的worker已经处理完所有连接而shutdown了)。还有一个loader进程。过几秒种可以看到worker只有两个了:

  1. root      6241 10419  0 10:51 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  2. nobody    6242  6241  1 10:51 ?        00:00:00 nginx: worker process
  3. nobody    6243  6241  0 10:51 ?        00:00:00 nginx: worker process
  4. nobody    6244  6241  0 10:51 ?        00:00:00 nginx: worker process
  5. nobody    6245  6241  0 10:51 ?        00:00:00 nginx: worker process
  6. nobody    6246  6241  0 10:51 ?        00:00:00 nginx: worker process
  7. nobody    6247  6241  0 10:51 ?        00:00:00 nginx: worker process
  8. nobody    6248  6241  0 10:51 ?        00:00:00 nginx: worker process
  9. nobody    6249  6241  0 10:51 ?        00:00:00 nginx: worker process
  10. nobody    6250  6241  0 10:51 ?        00:00:00 nginx: worker process
  11. nobody    6251  6241  0 10:51 ?        00:00:00 nginx: worker process
  12. nobody    6252  6241  0 10:51 ?        00:00:00 nginx: cache manager process
  13. nobody    6253  6241  0 10:51 ?        00:00:00 nginx: cache loader process
  14. luming    6430 25051  0 10:51 pts/1    00:00:00 grep --color=auto nginx
  15. nobody    7996 10419  0 Jan12 ?        00:20:11 nginx: worker process is shutting down
  16. nobody    8003 10419  0 Jan12 ?        00:20:12 nginx: worker process is shutting down
  17. root     10419     1  0 Jan08 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

再过一小会儿观察:

  1. root      6241     1  0 10:51 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  2. nobody    6242  6241  0 10:51 ?        00:00:01 nginx: worker process
  3. nobody    6243  6241  0 10:51 ?        00:00:01 nginx: worker process
  4. nobody    6244  6241  0 10:51 ?        00:00:01 nginx: worker process
  5. nobody    6245  6241  0 10:51 ?        00:00:00 nginx: worker process
  6. nobody    6246  6241  0 10:51 ?        00:00:00 nginx: worker process
  7. nobody    6247  6241  0 10:51 ?        00:00:00 nginx: worker process
  8. nobody    6248  6241  0 10:51 ?        00:00:00 nginx: worker process
  9. nobody    6249  6241  0 10:51 ?        00:00:00 nginx: worker process
  10. nobody    6250  6241  0 10:51 ?        00:00:01 nginx: worker process
  11. nobody    6251  6241  0 10:51 ?        00:00:02 nginx: worker process
  12. nobody    6252  6241  0 10:51 ?        00:00:00 nginx: cache manager process
  13. luming    8680 25051  0 10:56 pts/1    00:00:00 grep --color=auto nginx

Congratulations! You can upgrade your Nginx server gracefully.

时间: 2024-08-10 21:30:34

高性能Web服务器Nginx的配置与部署研究(14)平滑升级你的Nginx的相关文章

高性能Web服务器Nginx的配置与部署研究(13)应用模块之Memcached模块+Proxy_Cache双层缓存模式

通过<高性能Web服务器Nginx的配置与部署研究——(11)应用模块之Memcached模块的两大应用场景>一文,我们知道Nginx从Memcached读取数据的方式,如果命中,那么效率是相当高的.那么: 1. 如果不命中呢? 我们可以到相应的数据服务器上读取数据,然后将它缓存到Nginx服务器上,然后再将该数据返回给客户端.这样,对于该资源,只有穿透 Memcached的第一次请求是需要到数据服务器读取的,之后在缓存过期时间之内的所有请求,都是读取Nginx本地的.不过Nginx的 pro

高性能Web服务器Nginx的配置与部署研究(11)应用模块之Memcached模块的两大应用场景

一.应用场景1 最近在一个项目中,用到了Nginx的Memcached模块,所以就在这个系列教程中提前把Memcached模块拿出来写了.另外发现最近我的 博客文章频频被很多用采集器的网站拿走,帮我发扬光大,都不听我说声谢谢.在此还是希望我的博文被转载的时候能够被注明出处,满足下我小小的虚荣心. 现在有这样一种应用场景: 客户端Client通过Nginx反向代理,访问服务器Server.每次访问的内容就是将文件File传到Server上,然后可以访问到File的URL被广播到所有Client上,

高性能Web服务器Nginx的配置与部署研究(2)Nginx入门级配置与部署及“Hello World”

1. Nginx 程序包 目前最新的开发版本时1.1.12: Linux/Unix:nginx-1.1.12.tar.gz Windows:nginx-1.1.12.zip 我们可以下载稳定版尝试: Linux/Unix:nginx-1.0.11.tar.gz Windows:nginx-1.0.11.zip 2. 下载.解压.安装 Nginx 我们这里以Linux/Unix:nginx-1.0.11.tar.gz为例.下载并解压缩: wget http://nginx.org/download

高性能Web服务器Nginx的配置与部署研究(1)Nginx简介及入门示例

概述 从这篇博文起,将带领读者们一起领略Nginx的强大. Nginx 是做什么用的?我相信很多朋友都已经使用过,如果你没有,那么你一定知道以下这些名称之一:Apache,Lighttpd,Tomcat,Jetty. 它们占据了目前Web服务器的几乎全部江山,其中 Apache 是知名度最高的,同时也是最为重量级的.Lighttpd.Tomcat 和 Jetty 相对轻量级,其中 Jetty.Tomcat 多用于作为Java服务器容器. Nginx 是一个基于 BSD-like 协议.开源.高性

高性能Web服务器Nginx的配置与部署研究(3)Nginx请求处理机制

1. 处理什么样的请求 处理访问到 Nginx 所在 IP 地址的请求,并且这些请求的 HTTP 头信息中的 Host 为所要处理的域名(如下以80端口为例),如下几个 server 就对应响应的请求: server { listen 80; server_name nginx.org www.nginx.org; ... } server { listen 80; server_name nginx.net www.nginx.net; ... } server { listen 80; se

高性能Web服务器Nginx的配置与部署研究(4)Nginx常用命令

1. 启动 Nginx [email protected]:sudo ./sbin/nginx 2. 停止 Nginx [email protected]:sudo ./sbin/nginx -s stop [email protected]:sudo ./sbin/nginx -s quit -s都是采用向 Nginx 发送信号的方式. 3. Nginx 重载配置 [email protected]:sudo ./sbin/nginx -s reload 上述是采用向 Nginx 发送信号的方

高性能Web服务器Nginx的配置与部署研究系列(1)-- 入门 hello work

简介: Nginx 是一个基于 BSD-like 协议.开源.高性能.轻量级的HTTP服务器.反向代理服务器以及电子邮件(SMTP.POP3.IMAP)服务器.Nginx 是由一个俄罗斯的名叫“Igor Sysoev”的软件工程师开发的,最初用于 Rambler.ru 网站(该网站在俄罗斯国内访问量排名第二) 例子:hello work 1:配置清单: worker_processes  4; events { worker_connections  1024; } http { server

高性能Web服务器Nginx的配置与部署研究(9)核心模块之HTTP模块基本常用指令

一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.指令 1. alias 含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适用文件系统的路径. 语法:alias <file-path | directory-path> 缺省:N/A 作用域:http.server.location 示例: location /i/ { alias /home/michael/web/i/; } 则请求 /i/logo.png 则返回

高性能Web服务器Nginx的配置与部署研究(15)Upstream负载均衡模块

Nginx 的 HttpUpstreamModule 提供对后端(backend)服务器的简单负载均衡.一个最简单的 upstream 写法如下: server backend1.example.com; server backend2.example.com; server.backend3.example.com; location / { proxy_pass http://backend; } 1.后端服务器 通过 upstream 可以设定后端服务器,指定的方式可以是 IP 地址与端口