ngnix网站服务详解

一 Nginx的简介

1 Nginx:

①Nginx 是开源、高性能、高可靠的 Web 和反向代理服务器,而且支持热部署,几乎可以做到 7 * 24 小时不间断运行,即使运行几个月也不需要重新启动,还能在不间断服务的情况下对软件版本进行热更新
②Nginx的主要使用场景: 虚拟主机,反向代理,缓存,http  

③可以跨平台,配置简单,内存消耗少,10个nginx才占用150M内存。

④使用的是epoll模型,这种模型是I/O多路复用技术(I/O多路复用是一种技术,它允许一个进程或线程监控多个网络连接,当其中某个或某几个连接有数据时,当前程序可以拿到网卡收到的数据进行下一步的处理;),异步非阻塞的模型(异步非阻塞模型可以提高程序的效率,在等待I/O操作完成的同时,可以继续执行其他代码。)

⑥nginx支持高并发连接,处理2-3万并发连接数,官方监测能支持5万并发。对HTTP并发连接的处理能力高,单台物理服务器可支持30000~50000个并发请求。(实际操作,很多公司为了服务器的稳定,都会设置在20000个左右)

⑦成本低,且开源,稳定性高,宕机概率非常小;

⑧内置的健康检查功能,可以允许在服务器宕机的时候,做健康检查,再发送的请求就不会发给宕机的服务器,会重新提交到其他节点上。

2 Apache:

Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器 

 2.1Apache与Nginx的区别 

1 nginx是轻量级,nginx比apache 占用更少的内存及资源;

2 静态处理,Nginx 静态处理性能比 Apache 高 ;

3 Nginx可以实现无缓存的反向代理加速,提高网站运行速度;

4 Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于硬件;

5 Nginx支持热部署,启动速度迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级;

6 nginx是异步进程,基于事件的web服务器,多个连接对应一个进程 ;apache是同步多进程,基于流程的web服务器,单个线程处理单个请求;

7 Nginx高度模块化,编写模块相对简单,且组件比Apache少

8 nginx抗并发,nginx处理请求是异步非阻塞的,而Apache则是阻塞性的,在高并发下的nginx能够保持低资源,低消耗高性能。

9 nginx类似于速度,apache类似于功率.

10 nginx在负载均衡方面比较好,apache当流量进程达到极限时,将拒绝新的连接

2.2Nginx对比Apache的优势: 

轻量级,nginx比apache 占用更少的内存及资源

静态处理,Nginx 静态处理性能比 Apache 高 

Nginx可以实现无缓存的反向代理加速,提高网站运行速度

Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于硬件

Nginx支持热部署,启动速度迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级

nginx是异步进程,多个连接可以对应一个进程 ;apache是同步多进程,一个连接对应一个进程

Nginx高度模块化,编写模块相对简单,且组件比Apache少

高并发下nginx 能保持低资源低消耗高性能

Nginx 配置简洁, Apache配置复杂

2.3apache相对于nginx的优点 :

Rewrite比nginx的rewrite强大 (rewrite的主要功能就是实现统一资源定位符URL的跳转)
模块多,基本想到的都可以找到‘少bug, nginx的bug相对较多,超稳定
Nginx处理动态请求是弱项,动态请求要Apache去做,不过可以进行动静分离,将动态请求交给后端程序去进行处理。

3 Nginx的进程  

Apache和Nginx的默认端口都是80,当你发先Nginx启动失败,导致的原因可能就是80端口被占用,而导致服务启动失败,可以修改服务的端口,从而实现两个服务的同时进行

Nginx有两个进程:

master process:主进程(守护进程),用来管理工作进程,读取nginx的配置文件信息

worker process:工作进程,用来处理用户的连接请求

4 nginx的应用场景

1 可以做web服务器,nginx是一个http服务,可以独立地提供http服务,可以做静态服务器;

2 虚拟主机。可以实现一台服务器,虚拟多个站点,例如基于ip、不同端口、或者不同域名的站点

3 反向代理服务器,负载均衡。当网站的访问量达到一定的程度时,单台服务器就不能满足用户的请求,此时需要多台服务器集群,此时可以使用nginx做反向代理,并且多台服务器可以平均分担负载,不会让某台服务器负载高或者闲置。(也可以使用ip hash技术 进行负载均衡分配)

4 nginx中还可以配置安全管理,比如支持nginx搭建API接口网关,对每一个接口服务进行拦截;

5 nginx还能做缓存服务器;

5 影响用户体验的因素

服务器端:

服务器I/O压力

服务器并发数量

服务器架构设计

服务器硬件配置

服务器响应文件大小及数量buffer cache

服务器网络速率

服务器应用程序工作模式

6 Nginx 是如何实现高并发的?

 异步,非阻塞,使用了epoll 和大量的底层代码优化
如果一个server采用一个进程负责一个request的方式,那么进程数就是并发数。正常情况下,会有很多进程一直在等待中
而nginx采用一个master进程,多个woker进程的模式
master进程主要负责收集、分发请求。每当一个请求过来时,master就拉起一个worker进程负责处理这个请求。同时master进程也负责监控woker的状态,保证高可靠性
woker进程一般设置为跟cpu核心数一致。nginx的woker进程在同一时间可以处理的请求数只受内存限制,可以处理多个请求
Nginx 的异步非阻塞工作方式正把当中的等待时间利用起来了。在需要等待的时候,这些进程就空闲出来待命了,因此表现为少数几个进程就解决了大量的并发问题

同步和异步

同步:一个服务的完成需要依赖其他服务时,只有等待被依赖的服务完成后,才算完成,这是一种可靠的服务序列。要么成功都成功,失败都失败,服务的状态可以保持一致

异步:一个服务的完成需要依赖其他服务时,只通知其他依赖服务开始执行,而不需要等待被依赖的服务完成,此时该服务就算完成了。被依赖的服务是否最终完成无法确定,因此它是一个不可靠的服务序列

阻塞与非阻塞

阻塞:阻塞调用是指调用结果返回之前,当前线程会被挂起,一直处于等待消息通知,不能够执行其他业务,函数只有在得到结果之后才会返回。

非阻塞:非阻塞和阻塞的概念相对应,指在不能立刻得到结果之前,该函数不会阻塞当前线程,而会立刻返回

8 Nginx为什么不使用多线程?

Apache: 创建多个进程或线程,而每个进程或线程都会为其分配 cpu 和内存(线程要比进程小的多,所以worker支持比perfork高的并发),并发过大会耗光服务器资源

Nginx: 采用单线程来异步非阻塞处理请求(管理员可以配置Nginx主进程的工作进程的数量)(epoll),不会为每个请求分配cpu和内存资源,节省了大量资源,同时也减少了大量的CPU的上下文切换。所以才使得Nginx支持更高的并发

9 nginx 六大模块

  • 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件驱动机制 、进程管理等核心功能

  • 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等等

  • 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如:Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等

  • 邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持

  • Stream服务模块: 实现反向代理功能,包括TCP协议代理 反向

  • 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等

nginx高度模块化,但其模块早期不支持DSO机制;1.9.11 版本支持动态装载和卸载

10 I/O过程

用户态:应用程序,我们可以控制

内核态:操作系统层面,我们不容易去控制的 操作系统

① 当用户发起 http 请求需要请求一个index.html 网页文件
② 客户端请求与服务器端建立连接,建立连接后,会发送请求报文
③ 服务端的网卡收到请求报文,会将该报文复制到内核空间,内核空间分析报文后交给对应的程序。
  nginx 分析该报文,将报文和自己的配置文件,一一比对,按照配置文件完成请求,分析后发现客户需要 index.html 文件

④ 由于程序的权限问题,没有资格直接调用磁盘上的文件,程序会再将这个请求再次转发给内核,内核得到后请求去磁盘上找文件,找到文件后复制给程序 nginx
⑤ 程序会构建响应报文,构建好后再交给内核空间
⑥ 内核空间得到响应报文后,再交给网卡发给客户

11 零拷贝技术

在传统的数据传输过程中,数据通常需要经过多次复制。比如,当数据从磁盘读取到内存时,首先将数据读入内核缓冲区,然后再从内核缓冲区复制到用户空间的应用程序缓冲区。零拷贝技术通过避免或减少数据在内存和设备之间的多次复制来提高效率。具体做法包括直接内存访问(DMA)、文件映射(mmap)和发送文件(sendfile)等。

12 nginx实现方式

Nginx支持在多种不同的操作系统实现不同的事件驱动模型,但是其在不同的操作系统甚至是不同的系统版本上面的实现方式不同。

① select:select库是在linux和windows平台都基本支持的 事件驱动模型库,并且在接口的定义也基本相同,只是部
分参数的含义略有差异,最大并发限制1024,是最早期的事件驱动模型。

② poll:在Linux 的基本驱动模型,windows不支持此驱动模型,是select的升级版,取消了最大的并发限制,在编
译nginx的时候可以使用--with-poll_module和--without-poll_module这两个指定是否编译select
库。

③ epoll:epoll是库是Nginx服务器支持的最高性能的事件驱动库之一,是公认的非常优秀的事件驱动模型,它和select和poll有很大的区别,epoll是poll的升级版,但是与poll有很大的区别.
epoll的处理方式是创建一个待处理的事件列表,然后把这个列表发给内核,返回的时候在去轮训检查这个表,以判断事件是否发生,epoll支持一个进程打开的最大事件描述符的上限是系统可以打开的文件的最大数,同时epoll库的I/O效率不随描述符数目增加而线性下降,因为它只会对内核上报的“活跃”的描述符进行操作。

二 nginx各方面概述

1 功能描述

静态的 web 资源服务器 html,图片,js,css,txt 等静态资源

http/https 协议的反向代理 ,7层 url

结合 FastCGI /uWSGI/SCGI 等协议反向代理动态资源请求

tcp/udp 协议的请求转发(反向代理) 4层

2 基础特性

模块化设计,较好的扩展性

高可靠性

支持热部署:不停机更新配置文件,升级版本,更换日志文件

低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存

event-driven, aio, mmap,sendfile

3 web 服务相关的功能

虚拟主机(server)

支持 keep-alive 和管道连接(利用一个连接做多次请求)

访问日志(支持基于日志缓冲提高其性能)

url rewirte

路径别名

基于IP及用户的访问控制

支持速率限制及并发数限制

重新配置和在线升级而无须中断客户的工作进程

二 yum安装nginx

①yum install -y epel-release       #安装epel源   centos7 需要安装epel源  

②yum install nginx -y

[root@localhost ~]# yum install -y epel-release

[root@localhost ~]# yum install -y nginx

三 编译安装Nginx服务

安装Nginx下载安装依赖包

systemctl stop firewalld
systemctl disable firewalld   开机不自启
setenforce 0
 yum -y install gcc pcre-devel openssl-devel zlib-devel openssl  openssl-devel

主:nginx的配置及运行需要pcre、zlib、openssl等软件包的支持,因此需要安装这些软件的开发

包,以便提供相应的库和头文件。

2 创建运行用户、组

(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限) 

useradd -M -s /sbin/nologin nginx

3 下载安装包

先从官网下载安装包  www.nginx.org.com

 

4 解压软件包

5 编译安装

./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

6编译安装

make -j 2 && make install
make -j  2   ##表示cpu有2核,使用2个cpu同时一起编译,make是将源代码翻译成二进制make install    ##表示将二进制文件放到指定的目录中

 7 启动 、 停止nginx

方法二
ln -s /apps/nginx/sbin/nginx /usr/bin/

/apps/nginx/sbin/nginx       #绝对路径启动
 
nginx                        #相对路径启动
 
killall  nginx               #停止服务

方法一 

  

[root@nginx nginx-1.18.0]# nginx
[root@nginx nginx-1.18.0]# ps aus | grep nginx
error: conflicting format options

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
[root@nginx nginx-1.18.0]# ps aux | grep nginx
root       9799  0.0  0.0  46208  1164 ?        Ss   21:39   0:00 nginx: master process nginx
nginx      9800  0.0  0.0  48736  1996 ?        S    21:39   0:00 nginx: worker process
root       9804  0.0  0.0 112824   988 pts/2    S+   21:39   0:00 grep --color=auto nginx
[root@nginx nginx-1.18.0]# nginx
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)
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)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@nginx nginx-1.18.0]# ^C

8 修改权限

  9 创建Nginx 自启动文件   使用systemd 进行管理

注意:创建自启动必须先把nginx 关闭,否则报错

[root@nginx nginx-1.18.0]# killall  nginx
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid        #注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf     #注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
 
[Install]
WantedBy=multi-user.target

步骤如下

rm -rf /var/run/yum.pid  杀死尝试

yum -y install gcc pcre-devel openssl-devel zlib-devel openssl  openssl-devel
安装依赖包

useradd -M -s /sbin/nologin nginx
#新建nginx用户便于管理

cd /opt/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载安装包

tar xf nginx-1.18.0.tar.gz 
cd nginx-1.18.0/
#解压软件包

./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
添加各种模块

make -j2 && make install
编译安装

chown -R nginx.nginx /apps/nginx
#修改权限,递归设置属主与属组
至此安装结束



ln -s /apps/nginx/sbin/nginx  /usr/sbin/   建立软连接,可直接启动nginx


①绝对路径启动
/apps/nginx/sbin/nginx   若启动不起来,看看端口是否被占用,占用关闭

pstree -p | grep nginx
②做完软链接直接启动nginx
[root@localhost ~]#nginx
[root@localhost ~]#pstree -p | grep nginx
           |-nginx(27991)---nginx(27992)





ll /usr/local/sbin/

使用yum安装nginx时

配置文件位置在:/etc/nginx/nginx.conf;

默认根目录在:/usr/share/nginx/html;

默认日志文件在:/var/log/nginx/路径下

10 配置文件介绍

  • conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和
  • fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
  • html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
  • logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
  • sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

四 Nginx的初始命令 

①启/停nginx方法
/apps/nginx/sbin/nginx 
#使用绝对路径启动nginx

ps aux |grep nginx
#查看是否有nginx进程
②做完软连接nginx直接启动

#nginx

#pstree -p |grep nginx

2 创建nginx自启动文件

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target




[root@localhost nginx]#systemctl daemon-reload     #重新加载配置
[root@localhost nginx]#systemctl start nginx     
[root@localhost nginx]#systemctl status nginx
[root@localhost nginx]#systemctl enable --now niginx      #开机立即自启动

3 停止nginx服务
cat /usr/local/nginx/logs/nginx.pid		#先查看nginx的PID号

kill -3 <PID号>

kill -s QUIT <PID号>

killall -3 nginx

killall -s QUIT nginx
4 多种方式可以查看nginx进程号
cat /usr/local/nginx/logs/nginx.pid   //查看nginx主进程的PID
 
pgrep nginx -l   //查看nginx的主进程号和工作进程号
 
lsof -i :80    //查看nginx的主进程号和工作进程号
 ​
ss -ntap | grep nginx  //查看nginx的主进程号和工作进程号
 ​
netstat -ntap | grep nginx  //查看nginx主进程的PID
5 启动关闭nginx查看进程 
[root@localhost nginx-1.18.0]# nginx
[root@localhost nginx-1.18.0]# ps aux | grep nginx
root      49343  0.0  0.0  20564   616 ?        Ss   18:39   0:00 nginx: master process nginx
nobody    49344  0.0  0.0  23084  1380 ?        S    18:39   0:00 nginx: worker process
root      49358  0.0  0.0 112824   984 pts/0    S+   18:39   0:00 grep --color=auto nginx
7 重载nginx服务
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx


①nginx -V 查看安装了那些模块

②nginx -v 查看版本号

③nginx -s 发送信号 后面可以跟stop、reload、quit、reopen

④nginx -t 检查著配置文件语法

nginx -g 修改执行用户ps aux |grep nginx中查看。需要注销配置文件中的user nginx中。nginx -g "daemon off"前台运行,默认后台运行容器中需要使用。

⑥nginx -c 指定配置文件,一般用于.service文件中

⑦nginx -h 查询nginx信号使用规则

8 分割日志:

日志分割,重新打开日志文件;日志每天要换一次

[root@localhost logs]#ls
access.log  error.log  nginx.pid

[root@localhost logs]#mv access.log    access.`date +%F`.log

[root@localhost logs]#ls
access.2022-09-27.log  error.log  nginx.pid

[root@localhost logs]#kill -USR1 8468

[root@localhost logs]#ls
access.2022-09-27.log  access.log  error.log  nginx.pid

[root@localhost logs]#

此时打开一个终端#curl 192.168.11.6访问一下logs会再次出现 

另一种方法

错误对比 

9 创建nginx自启动文件
注意:创建自启动前需确保nginx软件安装,不仅仅时下载。

[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target


 
[root@localhost ~]# systemctl daemon-reload    
#重新加载配置
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl enable --now nginx
#开机自启并立即启动
10 指定配置

不以配置文件中的为准

[root@localhost nginx]#pstree -p |grep nginx
           |-nginx(8678)---nginx(9434)
[root@localhost nginx]#vim /apps/nginx/conf/nginx.conf
[root@localhost nginx]#nginx -s reload
[root@localhost nginx]#pstree -p |grep nginx
           |-nginx(8678)-+-nginx(13336)
           |             `-nginx(13337)

五 热升级:nginx1.18-nginx1.20 

  1. 将旧Nginx文件换成新Nginx文件(注意备份)

  2. 向master进程发送USR2信号

  3. master进程修改pid文件名,加后缀.oldbin

  4. master进程用新Nginx文件启动新master进程,系统中将有新旧两个Nginx主进程共同提供Web服务

  5. 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止,并删除Nginx.pid.oldbin文件

  6. 向旧master进程发送QUIT信号,关闭老master

  7. 如果发现升级有问题,可以回滚向老master发送HUP,向新master发送QUIT

  1. 下载新版本安装包,重新编译安装

[root@localhost ~]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载安装包到/usr/local/src/目录
[root@localhost ~]#cd /usr/local/src/
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz 
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.20.2]# nginx -V
nginx version: nginx/1.18.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=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.20.2]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#重新编译
[root@localhost nginx-1.20.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@localhost nginx-1.20.2]# make
[root@localhost nginx-1.20.2]# cd objs/
#此文件夹中有新版本的nginx运行程序
[root@localhost objs]# ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@localhost objs]# ./nginx -v
nginx version: nginx/1.20.2
#查看版本

备份

拷贝新版本至原安装路径

[root@localhost objs]# mv /apps/nginx/sbin/nginx /apps/nginx/sbin/nginx.bak
#将低版本的nginx主程序改名
[root@localhost objs]# cp nginx /apps/nginx/sbin/
#将新版本拷入原安装路径
[root@localhost objs]# cd /apps/nginx/sbin/
[root@localhost sbin]# ls
nginx  nginx.bak
[root@localhost nginx-1.20.2]# ll /apps/nginx/sbin/
总用量 15308
-rwxr-xr-x. 1 root  root  7896080 2月  20 21:15 nginx
-rwxr-xr-x. 1 nginx nginx 7774624 2月  20 20:52 nginx.bak
[root@localhost nginx-1.20.2]# nginx -v
nginx version: nginx/1.20.2
#nginx文件为新版本
[root@localhost sbin]# /apps/nginx/sbin/nginx -t
#检查下语法问题

 生成新进程

[root@localhost nginx-1.20.2]# kill -USR2 `cat /apps/nginx/logs/nginx.pid`
#发送 2 信号
 
[root@localhost sbin]# pstree -p | grep nginx
           |-nginx(5073)-+-nginx(8717)-+-nginx(8718)
           |             |             `-nginx(8719)
           |             |-nginx(5623)
           |             `-nginx(5624)
[root@localhost sbin]#  ps aux | grep -v grep | grep nginx
root       5073  0.0  0.1  46344  2020 ?        Ss   22:33   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      5623  0.0  0.1  48856  2092 ?        S    22:37   0:00 nginx: worker process
nginx      5624  0.0  0.1  48856  2084 ?        S    22:37   0:00 nginx: worker process
root       8717  0.0  0.1  46220  3368 ?        S    22:43   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx      8718  0.0  0.1  48756  1992 ?        S    22:43   0:00 nginx: worker process
nginx      8719  0.0  0.1  48756  1992 ?        S    22:43   0:00 nginx: worker process
#生成新的master8717
[root@localhost ~]# cd /apps/nginx/logs
[root@localhost logs]# ls
access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@localhost logs]# cat nginx.pid
8717         #新的master进程
[root@localhost logs]# cat nginx.pid.oldbin
5073         #旧的master进程
192.168.11.4:(下载版本的虚拟机)
[root@localhost ~]# cd /apps/nginx/html
[root@localhost html]# ls
50x.html  index.html
#会有新老两个进程
[root@localhost html]# dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=10
#生成大文件
[root@localhost html]# ll -h
总用量 1.1G
-rw-r--r--. 1 root root 494 2月  21 18:22 50x.html
-rw-r--r--. 1 root root 612 2月  21 18:22 index.html
-rw-r--r--. 1 root root 10G 2月  21 19:44 m.img

 
192.168.11.3:
[root@localhost data]# wget --limit-rate=1M http://192.168.11.4/m.img
#下载文件
 
192.168.11.9:
[root@localhost html]# kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#优雅关闭旧进程的worker进程,此时旧进程不会再接收新的任务,后期可以直接退出
 
192.168.11.3:
[root@localhost ~]# curl 192.168.11.4 -I
HTTP/1.1 200 OK
Server: nginx/1.20.2         
#新版本接替接收任务,即升级成功

引入新的升级安装包:

优雅的退出:

回滚:

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值