nginx进阶----平滑升级和location实战

nginx进阶----平滑升级和location实战

nginx平滑升级的概述

随着当今时代,nginx越来越流行,nginx版本换代越来越频繁,最新版本的nginx更新了很多新功能,生产环境中版本升级必然的但是线上业务不能停,此时nginx的升级就是运维的重要工作了。

nginx平滑升级原理

(1)多进程模式下的请求分配方式

​ Nginx默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。

(2)信号的接受和处理

​ nginx主进程在启动完成后进入等待状态。负载相应各类系统信息。如:SIGCHLD,SIGHUP,SIGUSR2

(3)nginx信号介绍

①、主进程支撑的信号

信号解释
TERM,INT立即退出
QUIT等待工作进程结束后再退出
KILL强制终止进程
HUP重新加载配置文件,使用新的配置启动进程,并逐步关闭旧进程
usr1重新打开日志文件
usr2启动新的主进程,实现热升级
WINCH逐步关闭工作进程

②、工作进程支持的信号

信号解释
TERM,INT立即退出
QUIT等待请求结束后再退出
USR重新打开日志文件

平滑升级过程

不能执行安装操作

  • 获取老版本的编译信息
  • 获取新版本安装包或功能包
  • 配置新版本或功能,配置时加上老版本的编译信息和新版本或功能(一add-module)
  • 编译,编译完后不能执行安装操作
  • 备份老版本程序,使用复制的方式
  • 停掉老版本程序的进程 _
  • 将新版本程序复制到老版本所在位置替换掉老版本
  • 启动新版

获取新版本的软件包或功能包,下载地址:https://gitee.com/wujunze/nginx_module_echo

#安装git
[root@nginx src]# yum -y install git

#克隆下载nginx_module_echo这个包
[root@nginx src]# git clone https://gitee.com/wujunze/nginx_module_echo.git
Cloning into 'nginx_module_echo'...
remote: Enumerating objects: 80, done.
remote: Total 80 (delta 0), reused 0 (delta 0), pack-reused 80
Unpacking objects: 100% (80/80), 14.32 KiB | 771.00 KiB/s, done.
[root@nginx src]# ls
debug  kernels  nginx-1.20.2  nginx-1.20.2.tar.gz  nginx_module_echo



#下载最新的nginx包
[root@nginx src]# wget https://nginx.org/download/nginx-1.23.1.tar.gz
--2022-10-12 10:50:41--  https://nginx.org/download/nginx-1.23.1.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
[root@nginx ~]# 
[root@nginx ~]# 
[root@nginx ~]# cd /usr/src/
[root@nginx src]# ls
debug  kernels  nginx-1.20.2.tar.gz  nginx-1.23.1  nginx-1.23.1.tar.gz  nginx_module_echo


#将新功能或新版本进行编译
[root@nginx src]# tar xf nginx-1.23.1.tar.gz 
[root@nginx nginx-1.23.1]# ./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   --add-module=../nginx_module_echo/

[root@nginx nginx-1.23.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)


#查看版本和类型以及大小
[root@nginx nginx-1.23.1]# file objs/nginx
objs/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=0bc5372450debe3c28ab9f1f1afbdfefc35a21a0, with debug_info, not stripped

[root@nginx nginx-1.23.1]# objs/nginx -v
nginx version: nginx/nginx-1.23.1

[root@nginx nginx-1.23.1]# du -sh objs/nginx
6.1M    objs/nginx


#杀掉之前的nginx进程
[root@nginx nginx-1.23.1]# ps -aux |grep nginx
root       42568  0.0  0.0   9208  1144 pts/0    S+   15:27   0:00 grep --color=auto nginx
[root@nginx nginx-1.23.1]# pkill nginx
[root@nginx nginx-1.23.1]# ps -aux |grep nginx
root       42969  0.0  0.0   9208  1136 pts/0    S+   15:27   0:00 grep --color=auto nginx
[root@nginx nginx-1.23.1]# ss -anlt
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                       [::]:22                     [::]:*                

#备份原程序
[root@nginx nginx-1.23.1]# mv /usr/local/nginx/sbin/nginx{,-bak}
[root@nginx nginx-1.23.1]# ls /usr/local/nginx/sbin/
nginx-bak
[root@nginx nginx-1.23.1]# cp objs/nginx /usr/local/nginx/sbin/
[root@nginx nginx-1.23.1]# nginx -v
nginx version: nginx-1.23.1

#,检测配置规则,升级成功
[root@nginx nginx-1.23.1]# systemctl start nginx.service 
[root@nginx nginx-1.23.1]# /usr/local/nginx/sbin/nginx-bak -t
nginx: [emerg] unknown directive "echo" in /usr/local/nginx/conf/nginx.conf:44
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@nginx nginx-1.23.1]# /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


#停掉旧程序使用新程序,并测试
[root@nginx local]# vim /usr/local/nginx/conf/nginx.conf
location / {
                echo "yyz";
        }
[root@nginx local]# 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 local]# systemctl reload nginx.service 
[root@nginx nginx-1.23.1]# ss -anlt
State      Recv-Q     Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                       [::]:22                     [::]:*     


验证访问

image-20221012154841080

location区段

location区段,通过指定模式来与客户端请求的URI相匹配

//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能

//语法:location [ 修饰符 ] pattern {......}

常用修饰符说明:

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写
~*正则表达式模式匹配,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

location实战

没有修饰符表示必须以指定模式开始

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
 location /test {	#匹配/test下的所有
                echo "test";
        }#error_page  404              /404.html;

[root@nginx ~]# systemctl start nginx.service 
[root@nginx ~]# systemctl reload nginx.service 
[root@nginx ~]# curl  http://192.168.17.147
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#如下内容可正确匹配
[root@nginx ~]# curl http://192.168.17.147/test
test

精确查询

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location /test {
                echo "test";
        }
                location = /test {
                        echo "yyz";
        }
[root@nginx ~]# systemctl reload nginx.service   

访问:

精确匹配

image-20221012174824301

内容匹配不到

image-20221012175026654

指定的正则表达式要区分大小写

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location /test {
                echo "test";
        }
                location ~ ^/test$ {	#这个是匹配正则表达式以^/t开头平且区分大小写匹配
                        echo "yyz";
        }
[root@nginx ~]# 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 ~]# systemctl reload nginx.service 

image-20221012180254238

也能这样访问

image-20221012180228334

指定的正则表达式不区分大小写

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location /test {
                echo "test";
        }
                location ~* ^/test$ {	#表示匹配正则表达式以^/t开头的,并且不区分大小写
                        echo "yyz";
        }
[root@nginx ~]# 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 ~]# systemctl reload nginx.service 

#发现这里是不区分大小写的
[root@nginx ~]# curl http://192.168.17.147/TEST
yyz
[root@nginx ~]# curl http://192.168.17.147/test
yyz

查找顺序和优先级:由高到底依次为

带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有~或 ~*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配

优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值