【Nginx】Nginx 安装(平滑升级和回滚)

一、 Nginx 概述

Nginx 介绍
Nginx engine X 2002 年开发,分为社区版和商业版 (nginx plus )
2019 3 11 F5 Networks 6.7 亿美元的价格收购
Nginx 是免费的、开源的、高性能的 HTTP 和反向代理服务器、邮件代理服务器、以及 TCP/UDP 代理服务器解决C10K 问题( 10K Connections
Nginx 官网: http://nginx.org
nginx 的其它的二次发行版:
Tengine 由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加
了很多高级功能和特性。 Tengine 的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了
很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的 Web 平台。从 2011 12 月开始,Tengine成为一个开源项目官网 : http://tengine.taobao.org/
OpenResty :基于 Nginx Lua 语言的高性能 Web 平台, 章亦春团队开发,官网: http://openr
esty.org/cn/
Nginx 功能介绍
  • 静态的web资源服务器html,图片,jscsstxt等静态资源
  • http/https协议的反向代理
  • 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
  • tcp/udp协议的请求转发(反向代理)
  • imap4/pop3协议的反向代理

基础特性

  • 模块化设计,较好的扩展性
  • 高可靠性
  • 支持热部署:不停机更新配置文件,升级版本,更换日志文件
  • 低内存消耗:10000keep-alive连接模式下的非活动连接,仅需2.5M内存
  • event-driven,aio,mmapsendfile
 Web 服务相关的功能
  • 虚拟主机(server
  • 支持 keep-alive 和管道连接(利用一个连接做多次请求)
  • 访问日志(支持基于日志缓冲提高其性能)url rewirte
  • 路径别名
  • 基于IP及用户的访问控制
  • 支持速率限制及并发数限制
  • 重新配置和在线升级而无须中断客户的工作进程

Nginx 安装

 Nginx 版本和安装方式
Nginx 版本
Mainline version 主要开发版本 , 一般为奇数版本号 , 比如 1.19
Stable version 当前最新稳定版 , 一般为偶数版本 , :1.20
Legacy versions 旧的稳定版 , 一般为偶数版本 , :1.18
Nginx 安装可以使用 yum 或源码安装,但是推荐使用源码编译安装
yum 的版本比较旧
编译安装可以更方便自定义相关路径
使用源码编译可以自定义相关功能,更方便业务的上的使用
Nginx 编译安装
编译器介绍
源码安装需要提前准备标准的编译器, GCC 的全称是( GNU Compiler collection ),其有 GNU 开发,并以GPL即 LGPL 许可,是自由的类 UNIX 即苹果电脑 Mac OS X 操作系统的标准编译器,因为 GCC 原本只能处理 C 语言,所以原名为GNU C 语言编译器,后来得到快速发展,可以处理 C++,Fortran pascal objective C, java以及 Ada 等其他语言,此外还需要 Automake 工具,以完成自动创建 Makefile 的工作, Nginx 的一些模块需要依赖第三方库,比如: pcre (支持 rewrite ), zlib (支持 gzip 模块)和 openssl (支持 ssl 模块)等。
  编译安装 Nginx

官方源码包下载地址

https://nginx.org/en/download.html
[root@Nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# tar zxf nginx-1.24.0.tar.gz
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# cd nginx-1.24.0/
[root@Nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
[root@Nginx nginx-1.24.0]# make && make install
下载1.24版本的

下载c语言的编译器:

下载负载的软件包:
最后效果:makefile配置文件(make规则)

 将make文件与c语言合成,最后生成nginx文件

下载文件到/usr/local/ngnix/sbin
查看内存:
删除nginx
关闭debug功能
下载最小内存的nginx:
编辑
访问:
升级稳定版本1.26:
有时候我们需要对 Nginx 版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时Nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级
  • 将旧Nginx二进制文件换成新Nginx程序文件(注意先备份)
  • master进程发送USR2信号
  • master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
  • master进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx
  • 进程共同提供Web服务,当前新的请求仍然由旧Nginxworker进程进行处理,将新生成的master
  • 程的PID存放至新生成的pid文件nginx.pid
  • 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止
  • 向旧master进程发送QUIT信号,关闭老master,并删除Nginx.pid.oldbin文件
  • 如果发现升级有问题,可以回滚∶向老master发送HUP,向新master发送QUIT

更改nginx名字变量:

平滑升级和回滚
准备环境:
重新make install进行下载
[root@node1 nginx-1.24.0]# cd /usr/local/nginx/
[root@node1 nginx]# ls
conf  html  logs  sbin
[root@node1 nginx]# cd sbin/
[root@node1 sbin]# ls
nginx
[root@node1 sbin]# nginx 
[root@node1 sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 17 Aug 2024 12:25:34 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 12:24:43 GMT
Connection: keep-alive
ETag: "66c0968b-267"
Accept-Ranges: bytes

[root@node1 sbin]# cp nginx nginx.old
[root@node1 sbin]# ls
nginx  nginx.old
[root@node1 sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/
[root@node1 sbin]# date
Sat Aug 17 08:29:10 PM CST 2024
[root@node1 sbin]# date
Sat Aug 17 08:29:13 PM CST 2024
[root@node1 sbin]# ll
total 7216
-rwxr-xr-x 1 root root 6150176 Aug 17 20:29 nginx
-rwxr-xr-x 1 root root 1235800 Aug 17 20:26 nginx.old
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860   928 ?        Ss   20:25   0:00 nginx: master process nginx
nginx      19472  0.0  0.1  13756  5472 ?        S    20:25   0:00 nginx: worker process
root       19531  0.0  0.0 221664  2356 pts/1    S+   20:30   0:00 grep --color=autonginx
[root@node1 sbin]# pidof nginx
19472 19471
[root@node1 sbin]# kill -USR2 19471
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
nginx      19472  0.0  0.1  13756  5472 ?        S    20:25   0:00 nginx: worker process
root       19537  0.0  0.1   9760  5964 ?        S    20:31   0:00 nginx: master process nginx
nginx      19538  0.0  0.1  13788  4924 ?        S    20:31   0:00 nginx: worker process
root       19540  0.0  0.0 221664  2372 pts/1    S+   20:31   0:00 grep --color=autonginx
[root@node1 sbin]# kill -WINCH 19471
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
root       19537  0.0  0.1   9760  5964 ?        S    20:31   0:00 nginx: master process nginx
nginx      19538  0.0  0.1  13788  4924 ?        S    20:31   0:00 nginx: worker process
root       19542  0.0  0.0 221664  2372 pts/1    S+   20:33   0:00 grep --color=autonginx
[root@node1 sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 17 Aug 2024 12:34:27 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 12:24:43 GMT
Connection: keep-alive
ETag: "66c0968b-267"
Accept-Ranges: bytes

[root@node1 sbin]# kill -HUP 19471
[root@node1 sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 17 Aug 2024 12:35:15 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 12:24:43 GMT
Connection: keep-alive
ETag: "66c0968b-267"
Accept-Ranges: bytes

[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
root       19537  0.0  0.1   9760  5964 ?        S    20:31   0:00 nginx: master process nginx
nginx      19538  0.0  0.1  13788  4924 ?        S    20:31   0:00 nginx: worker process
nginx      19544  0.0  0.1  13756  4872 ?        S    20:35   0:00 nginx: worker process
root       19547  0.0  0.0 221664  2372 pts/1    S+   20:35   0:00 grep --color=autonginx
[root@node1 sbin]# kill -WINCH 19544
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
root       19537  0.0  0.1   9760  5964 ?        S    20:31   0:00 nginx: master process nginx
nginx      19538  0.0  0.1  13788  4924 ?        S    20:31   0:00 nginx: worker process
nginx      19549  0.0  0.1  13756  4872 ?        S    20:36   0:00 nginx: worker process
root       19551  0.0  0.0 221664  2372 pts/1    S+   20:36   0:00 grep --color=autonginx
[root@node1 sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 17 Aug 2024 12:36:29 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 12:24:43 GMT
Connection: keep-alive
ETag: "66c0968b-267"
Accept-Ranges: bytes

[root@node1 sbin]# 
[root@node1 sbin]# 
[root@node1 sbin]# kill -WINCH 19537
[root@node1 sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 17 Aug 2024 12:37:19 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 12:24:43 GMT
Connection: keep-alive
ETag: "66c0968b-267"
Accept-Ranges: bytes

[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
root       19537  0.0  0.1   9760  6524 ?        S    20:31   0:00 nginx: master process nginx
nginx      19549  0.0  0.1  13756  5472 ?        S    20:36   0:00 nginx: worker process
root       19556  0.0  0.0 221664  2376 pts/1    S+   20:37   0:00 grep --color=autonginx
[root@node1 sbin]# LS
bash: LS: command not found...
Similar command is: 'ls'
[root@node1 sbin]# ls
nginx  nginx.old
[root@node1 sbin]# cp nginx nginx.new
[root@node1 sbin]# \cp -f nginx.old nginx
[root@node1 sbin]# ls
nginx  nginx.new  nginx.old
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
root       19537  0.0  0.1   9760  6524 ?        S    20:31   0:00 nginx: master process nginx
nginx      19549  0.0  0.1  13756  5472 ?        S    20:36   0:00 nginx: worker process
root       19579  0.0  0.0 221664  2360 pts/1    S+   20:39   0:00 grep --color=autonginx
[root@node1 sbin]# kill -9 19537
[root@node1 sbin]# ps aux | grep nginx
root       19471  0.0  0.0   9860  2552 ?        Ss   20:25   0:00 nginx: master process nginx
nginx      19549  0.0  0.1  13756  5472 ?        S    20:36   0:00 nginx: worker process
root       19581  0.0  0.0 221664  2372 pts/1    S+   20:40   0:00 grep --color=autonginx
[root@node1 sbin]# 
复制1.24的版本作为备份
查看进程
查看两个进程的状态
回收旧版本:

 回滚老版本:

 最后回归新版本:

[root@Nginx nginx]# tar zxf nginx-1.26.1.tar.gz
[root@Nginx nginx]# cd nginx-1.26.1/
#开始编译新版本
[root@Nginx nginx-1.26.1]# ./configure --with-http_ssl_module --withhttp_v2_module --with-http_realip_module --with-http_stub_status_module --withhttp_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --
with-stream_realip_module
#只要make无需要make install
[root@Nginx nginx-1.26.1]# make
#查看两个版本
[root@Nginx nginx-1.26.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 1239416 Jul 18 15:08 objs/nginx
-rwxr-xr-x 1 root root 5671488 Jul 18 11:41 /usr/local/nginx/sbin/nginx
#把之前的旧版的nginx命令备份
[root@Nginx ~]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp nginx nginx.24
#把新版本的nginx命令复制过去
[root@Nginx sbin]# \cp -f /root/nginx/nginx-1.26.1/objs/nginx
/usr/local/nginx/sbin
#检测一下有没有问题
[root@Nginx sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Nginx sbin]# kill -USR2 48732 #nginx worker ID
#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的
nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进
程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 48733 0.0 0.2 14200 4868 ? S 14:17 0:00 nginx: worker
process
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 4868 ? S 15:41 0:00 nginx: worker
process
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 ##依旧是旧版本生生效
Date: Thu, 18 Jul 2024 07:45:58 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes
#回收旧版本
[root@Nginx sbin]# kill -WINCH 48732
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 4868 ? S 15:41 0:00 nginx: worker
process
#检测版本信息
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.26.1 #新版本生效
Date: Thu, 18 Jul 2024 07:59:45 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes
#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker
[root@Nginx sbin]# cp nginx nginx.26
[root@Nginx sbin]# ls
nginx nginx.24 nginx.26
[root@Nginx sbin]# mv nginx.24 nginx
mv: overwrite 'nginx'? y
[root@Nginx sbin]# kill -HUP 48732
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 5124 ? S 15:41 0:00 nginx: worker
process
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker
process
[root@Nginx sbin]# kill -WINCH 52075
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker
process
root 52137 0.0 0.1 221664 2176 pts/0 S+ 16:31 0:00 grep --
color=auto nginx
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 ##版本回滚完成
Date: Thu, 18 Jul 2024 08:31:51 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xialliy

你的鼓励是我最大的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值