Nginx部署详解

一 Nginx简介

Nginx 是一个 高性能 的 HTTP 和 反向代理 web服务器,同时也提供了IMAP/POP3/SMTP服务。
Nginx 是一款轻量级的 Web 服务器 / 反向代理服务器 及 电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。
Nginx 是高性能的 HTTP 和 反向代理的web服务器,处理高并发 能力是十分强大的,能经受高负载的考验,有报告表明能支持高达 50,000 个并发连接数

【模块介绍】

Nginx模块分为核心模块,基础模块和第三方模块。
     核心模块:HTTP模块、EVENT模块(事件)、MAIL模块。
     基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块。
     第三方模块:HTTP Upstream Request Hash模块、Notice模块、HTTP Access Key模块。

【工作原理】

Nginx采用多进程模型,主进程(Master)负责接收客户端的连接请求,然后将连接请求分发给工作进程(worker)来处理。每个工作进程可以处理多个客户端连接,采用异步非阻塞的方式来处理连接请求。

【性能优势】

       web服务器:处理静态文件、索引文件以及自动索引效率高。
       代理服务器:快速高效反向代理,提升网站性能。
       负载均衡器:内部支持Rails和PHP,也可支持HTTP代理服务器,对外进行服务。同时支持简单容错和利用算法进行负载均衡。
       性能方面:Nginx专门为性能设计,实现注重效率。采用Poll模型,可以支持更多的并发连接,并在大并发时占用很低内存。
       稳定性方面:采用分阶段资源分配技术,使CPU资源占用率低。
       高可用性方面:支持热备,启动迅速

【主要功能】

1 反向代理
1.1 反向代理: 是指代理服务器来接收Internet上的客户端请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给客户端。此时代理服务器对外就表现为一个反向代理服务器。
反向代理客户端不知道服务器的信息,隐藏了服务器的信息
1.2 正向代理:是一个位于客户端和原始服务器之间的服务器,为了从原始服务器获得内容。客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转发并获得的内容返回给客户端
正向代理服务器不知道客户端的信息,隐藏了客户端的信息

2 负载均衡
Nginx负载均衡是以反向代理的方式进行负载均衡的,简而言之,就是当有两台或者以上服务器,根据规则随机的将请求分发到指定的服务器上处理,负载均衡配置一般都需要配置反向代理,通过反向代理跳转到负载均衡。
将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,负载分发到不同的服务器,就是我们所说的负载均衡

【Nginx负载均衡分配策略】

1 轮询(Round Robin):这是最简单的一种负载均衡方式,循环将请求依次分发至每个被代理的服务器。Nginx 默认使用轮询方式。

    upstream zhang_demo {
            server 11.0.1.131:8080;
            server 11.0.1.132:8080;
    }

2 IP 哈希(IP Hash):这种方式根据请求客户端的 IP 地址来计算 hash,然后用 hash 值对服务器总数取模得到分配的服务器地址,如果客户端的 IP 地址不变,那么它的请求都将被分配到同一台服务器上。

    upstream zhang_demo {
            ip_hash;
            server 11.0.1.131:8080;
            server 11.0.1.132:8080;
    }

3 最少连接(Least Connections):这种方式基于后端服务器连接数的统计数据,将新的请求分配到连接数最少的服务器上,这种方式能够确保每个后端服务器的负载尽量平衡。

    upstream zhang_demo {
            least_conn;
            server 11.0.1.131:8080;
            server 11.0.1.132:8080;
    }

4 加权轮询(Weighted Round Robin):这种方式通过为每个被代理的服务器分配不同的权重值,从而使得高权重服务器有更多的请求机会。

    upstream zhang_demo {
            server 11.0.1.131:8080 weight=1;
            server 11.0.1.132:8080 weight=2;
    }


加权最少连接(Weighted Least Connections):这种方式将前两种负载均衡方式的优点相结合,不仅考虑到了每个服务器的负载情况,还能使高权重的服务器拥有更多的连接机会。

5 URL Hash:这种方式是根据访问 URL 的 hash 值来进行分配,通常用于后端服务器不能共享 session 的情况,使得每次请求都能够访问到之前的数据。

    upstream backend {  
           server 192.168.0.1:88;  
           server 192.168.0.2:80;  
           hash $request_uri;  
           hash_method crc32;  
}

【Nginx负载均衡调度状态】

在Nginx upstream模块中,可以设定每台后端服务器在负载均衡调度中的状态,常用的状态有:

1 down,表示当前的server暂时不参与负载均衡
2 weight 默认为1,weight越大,负载的权重就越大。
3 backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的访问压力最低
4 max_fails,允许请求失败的次数,默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误。
5 fail_timeout,请求失败超时时间,在经历了max_fails次失败后,暂停服务的时间。max_fails和fail_timeout可以一起使用。

upstream bakend{ 
      ip_hash; 
      server 192.168.0.1:90 down; 
      server 192.168.0.1:80 weight=2; 
      server 192.168.0.2:90; 
      server 192.168.0.2:80 backup; 
}

二 Nginx部署

1 部署环境

# 系统环境
BigCloud Enterprise Linux For Euler 21.10 LTS

# nginx版本
nginx-1.24.0.tar.gz

2 部署详解

1 安装相关依赖包

安装编译环境

[root@jie ~]# yum -y install gcc gcc-c++

安装pcre软件包(使nginx支持http rewrite模块)

[root@jie ~]# yum install -y pcre pcre-devel

安装 openssl-devel(使 nginx 支持 ssl)

[root@jie ~]# yum install -y openssl openssl-devel

安装zlib

[root@jie ~]# yum install -y zlib zlib-devel gd gd-devel

创建用户 nginx

[root@jie ~]# useradd -s /sbin/nologin nginx

2 编译nginx

下载安装包(离线环境可以提前下载通过工具商上传到服务器)

[root@jie weihu]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@jie weihu]# tar -zxvf nginx-1.24.0.tar.gz
[root@jie weihu]# cd nginx-1.24.0
[root@jie nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

编译配置

[root@jie nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module 

[root@jie nginx-1.24.0]# make && make install
[root@jie nginx-1.24.0]# ll /usr/local/nginx/
总用量 16K
drwxr-xr-x 2 root root 4.0K  5月 20 11:42 conf
drwxr-xr-x 2 root root 4.0K  5月 20 11:42 html
drwxr-xr-x 2 root root 4.0K  5月 20 11:42 logs
drwxr-xr-x 2 root root 4.0K  5月 20 11:42 sbin
[root@jie nginx-1.24.0]# 

3 nginx相关命令

[root@jie nginx-1.24.0]# cd /usr/local/nginx/sbin/
[root@jie sbin]# ./nginx              # 启动Nginx
[root@jie sbin]# ./nginx -t           # 验证配置文件是正确
[root@jie sbin]# ./nginx -s reload    # 重启Nginx
[root@jie sbin]# ./nginx -s stop      # 停止Nginx
[root@jie sbin]# ./nginx -v            # 查看是否安装成功
nginx version: nginx/1.24.0
[root@jie sbin]# netstat -ntlp | grep nginx # 查看是否启动
tcp   0    0 0.0.0.0:80     0.0.0.0:*     LISTEN    13364/nginx: master

配置 Nginx 命令和服务并开机启动(可选)

1、创建服务配置文件
[root@jie ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload= /usr/local/nginx/sbin/nginx -s reload
ExecStop= /usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
2、添加执行权限
[root@jie ~]# chmod +x /usr/lib/systemd/system/nginx.service
3、启动服务(先停止nginx服务)
[root@jie ~]# systemctl daemon-reload
[root@jie ~]# systemctl start nginx.service   # 启动
[root@jie ~]# systemctl stop nginx.service    # 停止
[root@jie ~]# systemctl reload nginx.service  # 修改配置后重新加载生效 
[root@jie ~]# systemctl restart nginx.service # 重启
[root@jie ~]# systemctl status nginx   # 查看服务是否启动
4、添加开机启动
[root@jie ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

三 Nginx 编译参数

1 查看 nginx 安装的模块

[root@jie ~]# cd /usr/local/nginx/sbin/
[root@jie sbin]# ./nginx -V
nginx version: nginx/1.24.0
built by gcc 7.3.0 (GCC) 
built with OpenSSL 1.1.1t  7 Feb 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module

2  模块参数具体功能 

--with-cc-opt='-g -O2 -fPIE -fstack-protector'   # 设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' 
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' 

--prefix=/usr/share/nginx                        # 指向安装目录
--conf-path=/etc/nginx/nginx.conf                # 指定配置文件
--http-log-path=/var/log/nginx/access.log        # 指定访问日志
--error-log-path=/var/log/nginx/error.log        # 指定错误日志
--lock-path=/var/lock/nginx.lock                 # 指定lock文件
--pid-path=/run/nginx.pid                        # 指定pid文件

--http-client-body-temp-path=/var/lib/nginx/body    # 设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     # 设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy         # 设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi           # 设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         # 设定http uwsgi临时文件路径

--with-debug                                        # 启用debug日志
--with-pcre-jit                                     # 编译PCRE包含“just-in-time compilation”
--with-ipv6                                         # 启用ipv6支持
--with-http_ssl_module                              # 启用ssl支持
--with-http_stub_status_module                      # 获取nginx自上次启动以来的状态
--with-http_realip_module                 # 允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module           # 实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module               # 作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module                    # 增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module                  # 使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module                 # 它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module            # 在线实时压缩输出数据流
--with-http_image_filter_module           # 传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
--with-http_spdy_module                   # SPDY可以缩短网页的加载时间
--with-http_sub_module                    # 允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module                   # 过滤转换XML请求
--with-mail                               # 启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module                    # 启用ngx_mail_ssl_module支持启用外部模块支持

四 Nginx 配置文件 

1 全局配置

worker_processes  1;          # 设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 
error_log  logs/error.log;    # 指定错误日志 
worker_rlimit_nofile 102400;  # 设置一个nginx进程能打开的最大文件数 
pid        /var/run/nginx.pid; 
events {                      # 事件配置
     worker_connections  10240; # 设置一个进程的最大并发连接数
     use epoll;                # 事件驱动类型
}

2 http

http {  
    #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  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
sendfile          on;      # 用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
tcp_nopush        on;      # 减少网络报文段数量,当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
tcp_nodelay       on;      # 提高I/O性能,确保数据尽快发送, 提高可数据传输效率                           
gzip              on;      # 是否开启 gzip 压缩 
keepalive_timeout  65;     # 设置长连接的超时时间,请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系统带来的性能损                                    耗,类似于线程池,数据库连接池
types_hash_max_size 2048;  # 影响散列表的冲突率。types_hash_max_size 越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。                                            types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升
include             /etc/nginx/mime.types;  # 关联mime类型,关联资源的媒体类型(不同的媒体类型的打开方式)
default_type        application/octet-stream;  # 根据文件的后缀来匹配相应的MIME类型,并写入Response header,导致浏览器播放文件而不是下载

# 虚拟服务器的相关设置 
server { 
listen      80;        # 设置监听的端口 
server_name  localhost;        # 设置绑定的主机名、域名或ip地址 
# charset koi8-r;        # 设置编码字符 
location / { 
root  /var/www/nginx;           # 设置服务器默认网站的根目录位置 
index  index.html index.htm;    # 设置默认打开的文档 
  } 
error_page  500 502 503 504  /50x.html; # 设置错误信息返回页面 
location = /50x.html { 
root  html;        # 这里的绝对位置是/var/www/nginx/html 
    } 
  } 
}

五 扩展

1 Nginx 命令控制

nginx -c /path/to/nginx.conf     # 以特定目录下的配置文件启动nginx:
nginx -s reload                  # 修改配置后重新加载生效
nginx -s reopen                  # 重新打开日志文件
nginx -s stop                    # 快速停止nginx
nginx -s quit                    # 完整有序的停止nginx
nginx -t                         # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf  # 测试特定的nginx配置文件是否正确

2 安装注意事项

1、安依赖包不能少,否则编译安装会出现问题

2.解压后的nginx目录不能和编译后的目录在同一目录下,否则make install 会报错

3.nginx命令找不到?

[root@jie ~]# vim /etc/profile   # 在末行加入
PATH=$PATH:/usr/local/nginx/sbin
export PATH                              
[root@jie ~]# source /etc/profile

4.nginx设置自启动(没有由添加system管理服务的可以用这种方法进行nginx自启动)

[root@jie ~]# vim /etc/rc.d/rc.local
#在末行加入
/usr/local/nginx/sbin/nginx

#然后保存退出,再给/etc/rc.d/rc.local 这个文件执行权限
[root@jie ~]# chmod +x /etc/rc.d/rc.local

3 在原有nginx的基础上重新添加某个模块

示例:
重新添加:--with-http_stub_status_module --with-http_ssl_module

1)进入nginx原文件路径  此处的原路径是指安装包所在的路径,并非安装位置

[root@jie ~]# cd /home/weihu/nginx-1.24.0
[root@jie nginx-1.24.0]#

2)重新编译,添加模块 --with-http_stub_status_module --with-http_ssl_module  此处的编译过程需要注意各取所需(例如有rtmp模块,则理应一并编译)

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

3)编译 make  #不要 make install  这里只进行编译,避免指令错误引起的问题

make

4)备份&拷贝  这一部分是最为关键的部分,此模块的添加为锦上添花,但是一旦nginx无法运行就是大问题

#备份旧程序
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

#拷贝
cp -r objs/nginx /usr/local/nginx/sbin/nginx

5)重启nginx

/usr/local/nginx/sbin/nginx -s reload

6)检查模块是否加载

/usr/local/nginx/sbin/nginx -V
#最后的V是一个大写的V。若是小写的v,则只显示版本

如下是成功:

nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

六 Nginx平滑升级和回滚

nginx安装路径 /usr/local/nginx/

新版本源码包存放路径 /app/nginx-1.26.0.tar.gz

A 平滑升级

有时候需要对Nginx版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时Nginx上面在跑着业务无法停止,不能影响用户使用,这时就可以选择平滑升级

1 下载新版nginx二进制包

# 内网环境可以先将包下载到本地,然后再传至内网服务器
[root@jie ~]# cd /app/
[root@jie ~]# wget https://nginx.org/download/nginx-1.26.0.tar.gz
[root@jie app]# ls
nginx-1.26.0.tar.gz

2 编译新版本,生成新版本的二进制文件

注意:编译参数要查看老版本的编译参数直接复制过来编译即可(nginx -V),如果需要添加编译参数也可以,直接加进去编译就可以

#查看老版本的编译参数
[root@jie ~]# cd /usr/local/nginx/sbin/
[root@jie nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 7.3.0 (GCC) 
built with OpenSSL 1.1.1t  7 Feb 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module

# 进入新的ng版本源码目录里面进行编译
[root@jie ~]# cd /app/nginx-1.26.0
[root@jie nginx-1.26.0]# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module

# 开始编译(注意只执行make 不执行make install)
[root@jie nginx-1.26.0]# make

# 编译完成后在~/objs目录下会有一个名为nginx的程序(编译之前是没有的),可以进入这个目录看下版本
[root@jie nginx-1.26.0]# cd /app/nginx-1.26.0/objs/
[root@jie objs]# ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@jie objs]# ./nginx -v
nginx version: nginx/1.26.0
[root@jie objs]# 

3 用新Nginx程序文件替换旧Nginx二进制文件(注意先备份旧版本的二进制文件)

# 备份老版本,一定要备份,如果新版本有问题,还可以回退
[root@jie ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.24.0
[root@jie ~]# ll /usr/local/nginx/sbin/nginx   
-rwxr-xr-x 1 root root 7.6M  5月 20 11:42 /usr/local/nginx/sbin/nginx
[root@jie ~]# cp -a /usr/local/nginx/sbin/nginx /opt/nginx.bak
[root@jie ~]# ls /opt/nginx.bak
/opt/nginx.bak


# 把新版本的nginx命令复制过去覆盖旧版本程序文件,注意:需要加 -f 选项强制覆盖,否则会提示Text
[root@jie objs]# cp -f /app/nginx-1.26.0/objs/nginx /usr/local/nginx/sbin/
cp:是否覆盖'/usr/local/nginx/sbin/nginx'? y
[root@jie objs]# 


# 查看版本
[root@jie objs]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.26.0
看到已经是新版了

这里要注意一下:

虽然查看版本已经是新版了,但是请求还是在老版本中,因为/usr/local/nginx/sbin/nginx 只是个磁盘文件,老版本已经被系统加载到内存中了,也就是说内存中有一个老版本,硬盘中有一个新版本(系统内存中只有一个老版本)
所以现在要把新版本也拉起来,然后新旧版本共存
启动新版本之前,最好做一下语法检查(避免和老版本出现指令兼容性问题)

# 请求还是在老版本中
[root@jie ~]# curl http://11.0.1.131 -I
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 20 May 2024 06:30:06 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 20 May 2024 03:20:57 GMT
Connection: keep-alive
ETag: "664ac6c1-267"
Accept-Ranges: bytes


# 进行语法检查
[root@jie ~]# /usr/local/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

4 拉起新版本

#发送信号USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx

# 发送信号前查看
[root@jie ~]# ll /usr/local/nginx/logs/
总用量 8.0K
-rw-r--r-- 1 root root   0  5月 20 11:45 access.log
-rw-r--r-- 1 root root 617  5月 20 12:56 error.log
-rw-r--r-- 1 root root   5  5月 20 13:58 nginx.pid

# 发送信号USR2
[root@jie ~]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

# 发送信号后查看
[root@jie nginx]# ll /usr/local/nginx/logs/
总用量 12K
-rw-r--r-- 1 root root   0  5月 20 11:45 access.log
-rw-r--r-- 1 root root 691  5月 20 14:32 error.log
-rw-r--r-- 1 root root   5  5月 20 14:32 nginx.pid
-rw-r--r-- 1 root root   5  5月 20 13:58 nginx.pid.oldbin

# 此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80

#此时Nginx开启一个新的master进程,且这个新master进程会生成新的worker进程,即升级后的Nginx进程,此时老的进程不会自动退出,新的请求仍由旧进程处理。

[root@jie nginx]# ps -ef |grep nginx
root        3952       1  0 13:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody      3953    3952  0 13:58 ?        00:00:00 nginx: worker process
root        7438    3952  0 14:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody      7439    7438  0 14:32 ?        00:00:00 nginx: worker process
root        7443    1363  0 14:33 pts/0    00:00:00 grep --color=auto nginx
[root@jie nginx]# 

5 关闭老版本

# 先关闭旧nginx的worker进程,而不关闭旧nginx主进程方便回滚
# 向原老的Nginx主进程发送WINCH信号,它会平滑关闭老的工作进程(主进程不退出),这时所有新请求都会由新版Nginx处理
# 如果旧版worker进程有用户的旧的请求,会一直等待处理完后才会关闭,即平滑关闭

[root@jie ~]# kill -WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin`


# 查看nginx进程,发现老版本的work进程已经退出,老版本的master进程还在(旧版本的用户请求已经处理完毕)
[root@jie ~]# ps -ef |grep nginx
root        3952       1  0 13:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root        7438    3952  0 14:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody      7439    7438  0 14:32 ?        00:00:00 nginx: worker process
root        7483    1363  0 14:41 pts/0    00:00:00 grep --color=auto nginx
[root@jie ~]# 

# 访问测试
[root@jie ~]# curl http://11.0.1.131 -I
HTTP/1.1 200 OK
Server: nginx/1.26.0
Date: Mon, 20 May 2024 06:50:42 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 20 May 2024 03:42:57 GMT
Connection: keep-alive
ETag: "664ac6c1-267"
Accept-Ranges: bytes

【重点】必须要测试一段时间

# 经过一段时间测试,新版本服务没问题,最后发送QUIT信号,退出老的master,完成全部升级过程

[root@jie ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`


# 查看nginx进程,发现老版本已经全部退出了(平滑退出,老用户请求处理完成后老版本的master才会退出)
[root@jie ~]# ps -ef |grep nginx
root        7438       1  0 14:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody      7439    7438  0 14:32 ?        00:00:00 nginx: worker process
root        7493    1363  0 14:48 pts/0    00:00:00 grep --color=auto nginx

###### 至此ngix升级已经完成 ########

B 回滚

# 如果升级的新版本发现问题需要回滚,可以发送HUP信号,重新拉起旧版本的worker

[root@jie ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid.oldbin`

# 最后关闭新版的master和worker,如果不执行上面的HUP信号,此步QUIT信号也可以重新拉起旧版本的worker进程(平滑)

[root@jie ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

# 恢复旧版的文件

[root@jie ~]# mv /opt/nginx.bak /usr/local/nginx/sbin/nginx
mv: overwrite '/usr/local/nginx/sbin/nginx'? y

C 拓展

相关参数可以进去到nginc源码里面用man帮助查看

[root@jie nginx-1.26.0]# cd /app/nginx-1.26.0
[root@jie nginx-1.26.0]# man man/nginx.8 
NGINX(8)                                                      BSD System Manager's Manual                                                     NGINX(8)

NAME
     nginx — HTTP and reverse proxy server, mail proxy server

SYNOPSIS
     nginx [-?hqTtVv] [-c file] [-e file] [-g directives] [-p prefix] [-s signal]

DESCRIPTION
     nginx (pronounced “engine x”) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server.  It is known for its
     high performance, stability, rich feature set, simple configuration, and low resource consumption.

     The options are as follows:

     -?, -h         Print help.

     -c file        Use an alternative configuration file.

     -e file        Use an alternative error log file.  Special value stderr indicates that the standard error output should be used.

     -g directives  Set global configuration directives.  See EXAMPLES for details.

     -p prefix      Set the prefix path.  The default value is %%PREFIX%%.

     -q             Suppress non-error messages during configuration testing.

     -s signal      Send a signal to the master process.  The argument signal can be one of: stop, quit, reopen, reload.  The following table shows
                    the corresponding system signals:

                    stop    SIGTERM
..............

  • 27
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker是一个开源的容器化平台,用于轻松地打包、部署和运行应用程序。而Nginx是一个高性能的开源反向代理服务器,也是一个流行的Web服务器。 在使用Docker部署Nginx时,你需要以下几个步骤: 1. 首先,你需要安装Docker。你可以根据你的操作系统去官方网站下载并安装Docker。 2. 在安装完成后,你需要编写一个Dockerfile文件来定义Nginx容器的构建过程。一个简单的例子如下: ```dockerfile FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] ``` 这个Dockerfile使用最新版本的Nginx作为基础镜像,然后将自定义的`nginx.conf`文件复制到容器中的`/etc/nginx/nginx.conf`路径下。接着,将容器的80端口暴露出来,并设置Nginx以非守护进程的方式运行。 3. 在同一目录下创建`nginx.conf`文件,并在其中进行Nginx的相关配置。例如,可以指定Nginx监听的端口、代理规则等。 4. 接下来,使用Docker命令来构建镜像。在终端中切换到Dockerfile所在的目录,并执行以下命令: ``` docker build -t my-nginx . ``` 这个命令会根据Dockerfile构建一个名为`my-nginx`的镜像。注意最后的`.`表示Dockerfile所在的路径。 5. 构建完成后,你可以使用以下命令来运行Nginx容器: ``` docker run -d -p 80:80 my-nginx ``` 这个命令会在后台运行一个名为`my-nginx`的容器,并将主机的80端口映射到容器的80端口。 现在,你已经成功地使用Docker部署Nginx。你可以通过在浏览器中访问`http://localhost`来验证Nginx是否正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值