nginx平滑升级
   
    
    
    nginx平滑升级原理
   
    
     多进程模式下的请求分配方式
    
   
Nginx默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。
    
     信号的接收和处理
    
   
Nginx主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。
    
     Nginx信号简介
    
   
    主进程支持的信号
    
    TERM, INT: 立刻退出
    
    QUIT: 等待工作进程结束后再退出
    
    KILL: 强制终止进程
    
    HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
    
    USR1: 重新打开日志文件
    
    USR2: 启动新的主进程,实现热升级
    
    WINCH: 逐步关闭工作进程
   
    工作进程支持的信号
    
    TERM, INT: 立刻退出
    
    QUIT: 等待请求处理结束后再退出
    
    USR1: 重新打开日志文件
   
    
    
    nginx平滑升级
   
//下载nginx包并克隆
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug  kernels  nginx-1.20.2  nginx-1.20.2.tar.gz  nginx-1.22.0.tar.gz
[root@localhost src]# tar xf nginx-1.22.0.tar.gz 
[root@localhost src]# ls
debug    nginx-1.20.2         nginx-1.22.0
kernels  nginx-1.20.2.tar.gz  nginx-1.22.0.tar.gz
[root@localhost src]# git clone https://gitee.com/wujunze/nginx_module_echo.git
[root@localhost src]# ls
debug    nginx-1.20.2         nginx-1.22.0         nginx_module_echo
kernels  nginx-1.20.2.tar.gz  nginx-1.22.0.tar.gz
//编译安装
[root@localhost src]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../nginx_module_echo/
[root@localhost nginx-1.22.0]# make
//查看nginx之前和现在的版本参数
[root@localhost src]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.22.0]# objs/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../nginx_module_echo/
//备份并启动
[root@localhost nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak}
[root@localhost nginx-1.22.0]# pkill nginx
[root@localhost nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
cp: overwrite '/usr/local/nginx/sbin/nginx'? yes
[root@localhost nginx-1.22.0]# systemctl start nginx
[root@localhost nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0
[root@localhost nginx-1.22.0]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*              
LISTEN  0       128            0.0.0.0:80          0.0.0.0:*              
LISTEN  0       128               [::]:22             [::]:*    
    
    
    location实战
   
    
     location匹配规则规则说明:
    
   
| 修饰符 | 功能 | 
|---|---|
| = | 精确匹配 | 
| ~ | 正则表达式模式匹配,区分大小写 | 
| ~* | 正则表达式模式匹配,不区分大小写 | 
| ^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 | 
| @ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 | 
    
     优先级
    
   
( location = ) > ( location 完整路径 ) > ( location ^~ 路径 ) > ( location ,* 正则顺序 ) > ( location 部分起始路径 ) > ( / )
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  localhost;
        location / {                            //匹配所有请求
            echo "love A";
        }
        location ^~ /yue/ {						//匹配任何以/yue/开头的地址
            echo "love B";
        }
        location ~ ^/y*.x$  {					//区分大小写的匹配
            echo "love C";
        }
        location = /zhang {						//精确匹配
            echo "love D";
        }
         location ~* ^/y*.x$ {					//不区分大小写的匹配
            echo "love E";
        }
[root@localhost ~]# systemctl reload nginx
    可以匹配到所有请求
    
    
    
    只能匹配代有yue
    
    
    
    区分大小写的匹配,只能用小写,字母开始只能是y,结尾是x,中间只能是一个字符,并且只能小写
   
     
   
    精确匹配到zhang
    
    
    
    不区分大小写,字母开始只能是Y,字母结尾只能是X,中间可以是任意字符,其中*.代表的是一个字符
   
     
   
 
