平滑升级添加echo模块、location配置、rewrite配置

平滑升级添加echo模块、location配置、rewrite配置

平滑升级流程:

  1. 获取老版本的编译信息
  2. 老版本备份
  3. 编译新版本或者新功能(不能执行make install)
  4. 手动替换新版本并重启
  5. 验证新版本

环境准备

操作系统旧版本新版本新增功能
CentOS-8nginx-1.22.1nginx-1.24.0echo-nginx-module

平滑升级并添加echo模块

查看nginx版本号和编译信息

[root@localhost ~]# nginx -v    #查看版本号
nginx version: nginx/1.22.1
[root@localhost ~]# nginx -V    #查看编译参数
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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

[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-1.22.1
[root@localhost ~]# ls /opt/
nginx-1.22.1
[root@localhost ~]# 

下载新版本的nginx

[root@localhost ~]# cd /usr/src/
[root@localhost src]# 
[root@localhost src]# ls
debug  kernels  nginx-1.22.1  
[root@localhost src]# 
[root@localhost src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-19 20:44:52--  http://nginx.org/download/nginx-1.24.0.tar.gz
...
[root@localhost src]# ls
debug  kernels  nginx-1.22.1  nginx-1.24.0.tar.gz
[root@localhost src]# 

安装git工具,克隆echo模块

[root@localhost src]# yum -y install git
[root@localhost src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
remote: Enumerating objects: 3061, done.
remote: Counting objects: 100% (43/43), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 3061 (delta 21), reused 30 (delta 12), pack-reused 3018
Receiving objects: 100% (3061/3061), 1.18 MiB | 1.74 MiB/s, done.
Resolving deltas: 100% (1645/1645), done.
[root@localhost src]# ls
debug  echo-nginx-module  kernels  nginx-1.22.1  nginx-1.24.0.tar.gz

解压新版本nginx包,再次编译nginx,在老版本编译的基础上添加 --add-module模块

[root@localhost ~]# cd /usr/src/
[root@localhost src]# tar xf nginx-1.24.0.tar.gz 
[root@localhost src]# ls
debug  echo-nginx-module  kernels  nginx-1.22.1  nginx-1.24.0  nginx-1.24.0.tar.gz
[root@localhost src]# cd nginx-1.24.0
[root@localhost nginx-1.24.0]# 
[root@localhost nginx-1.24.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=../echo-nginx-module   #等于后面接模块包所在的位置
[root@localhost nginx-1.24.0]# make

平滑升级要先关闭旧版nginx服务,然后把新编译的nginx主程序替换掉旧版,再启动服务即可

[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# ls
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
[root@localhost objs]# ss -antl    #端口号80已经启动
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                         [::]:*                        
[root@localhost objs]# 

查看版本号

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

测试新添加的echo功能

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
location / {
            echo "hello world";    #添加echo模块
            root   html;
            index  index.html index.htm;
        }
......
#nginx支持热部署,修改配置文件后不需要重启服务
[root@localhost ~]# 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@localhost ~]# 

location配置

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

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

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

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

  1. 带有=的精确匹配优先
  2. 正则表达式按照他们在配置文件中定义的顺序
  3. 带有^~修饰符的,开头匹配
  4. 带有~~*修饰符的,如果正则表达式与URI匹配
  5. 没有修饰符的精确匹配
没有修饰符表示必须以指定模式开始
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
        location /abc { 
            echo "this is /abc";
        }
......

[root@localhost ~]# 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@localhost ~]# nginx -s reload  #重新加载nginx

使用命令行的方式访问

在没有修饰符的情况下只要你的域名后面更上了/abc,后面无论跟什么都可以匹配到。

[root@localhost ~]# curl http://192.168.10.130/abc
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc$sayhduig
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc/
this is /abc
[root@localhost ~]# 
=:表示必须与指定的模式精确匹配
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
        location /abc {
            echo "this is /abc";
        }

        location = /abc {
            echo "this is =abc";
        }
......
[root@localhost ~]# 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@localhost ~]# nginx -s reload

使用命令行的方式访问

[root@localhost ~]# curl http://192.168.10.130/abcdaf     
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc    #只有匹配内容与等于号后面的内容一样和传参数的情况下才能精确匹配到
this is =abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10  #?是用来传参数的\是转义符
this is =abc
[root@localhost ~]# curl http://192.168.10.130/abc/    #如果abc后面有/也是匹配不到的
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc/asd  #/后面有东西也是匹配不到的,只能是/abc才能匹配到
this is /abc

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

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
        location /abc {
            echo "this is /abc";
        }
        
#        location = /abc {
#            echo "this is =abc";
#       }

        location ~ ^/abc$ {
            echo "~abc";
        }
......
[root@localhost ~]# 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@localhost ~]# nginx -s reload

只有在开头是 /abc结尾不能有任何东西的情况下才能匹配到,且区分大小写

[root@localhost ~]# curl http://192.168.10.130/abc
~123
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~123

以下三种无法匹配到

[root@localhost ~]# curl http://192.168.10.130/abcl
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc/
this is /abc
[root@localhost ~]# curl http://192.168.10.130/ABC
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>
[root@localhost ~]# 

~*:表示指定的正则表达式不区分大小写

#注意,在配置location时应该把区分大小写的放在前面,因为约往上优先级越高,如果把不区分大小写的放在前面则会覆盖区分大小写的访问
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
        location ~ ^/abc$ {
            echo "~abc"
        }

        location ~* ^/abc$ {
            echo "~*abc"
        }

......

只有在开头是 /abc结尾不能有任何东西的情况下才能匹配到,且不区分大小写

[root@localhost ~]# curl http://192.168.10.130/AbC
~*abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~abc
[root@localhost ~]# 

以下是无法匹配到的

[root@localhost ~]# curl http://192.168.10.130/abc/
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abcadas/
this is /abc
[root@localhost ~]# 

^~:表示开头匹配,不支持正则表达式

[root@localhost ~]# curl http://192.168.10.130/abc/
^~abc
[root@localhost ~]# curl http://192.168.10.130/abc/asda
^~abc
[root@localhost ~]# curl http://192.168.10.130/abc/ads\?a\=10/ads
^~abc

以下是无法匹配的

[root@localhost ~]# curl http://192.168.10.130/abc
~abc
[root@localhost ~]# curl http://192.168.10.130/abcasd
this is /abc
[root@localhost ~]# curl http://192.168.10.130/abc\?a\=10
~abc
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式

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

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

优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )

rewrite配置

rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)

URL重定向的作用,比如:

当我们搜索:http://www.baidu.com时URL重定向可以以自动帮我们改为https://www.baidu.com

或者当我访问images目录时可以自动帮我换到imgs目录

语法:rewrite regex replacement flag;

​ #rewrite开头 正则表达式 要更换的内容 标记

以下是例子:

#  images目录下面的所有.jpg结尾的文件 都可以在imgs目录下找到。 break到这里终止
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

此处的$1用于引用(.*.jpg)匹配到的内容,又如:

#  访问bbs目录下所有的内容时 转发到这个域名访问。redirect
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

如上例所示,replacement可以是某个路径,也可以是某个URL

重定向操作

重定向目录

#创建一目录添加一个html文件
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir 123
[root@localhost html]# ls
123  50x.html  index.html
[root@localhost html]# echo "hello world" > 123/index.html
[root@localhost html]# 

访问测试

[root@localhost ~]# curl http://192.168.10.130/123/index.html
hello world
[root@localhost ~]# 

更改目录名

[root@localhost html]# mv 123 666
[root@localhost html]# ls
50x.html  666  index.html

正常情况下新的目录下访问不到任何东西

[root@localhost ~]# curl http://192.168.10.130/666/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

添加 location 做重定向

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
......
        location /123 {
            rewrite ^/123/(.*)$ /666/$1 break;
}

[root@localhost html]# nginx -s reload
[root@localhost html]# ls
50x.html  666  index.html
[root@localhost html]# 

访问测试

[root@localhost ~]# curl http://192.168.10.130/123/       #原路径依然可以访问到
hello world
[root@localhost ~]# 

重定向URL

#当我访问/666目录下的任何内容时都转发到https://www.4399.com/这个网页
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
......
        location /666 {
            rewrite ^/666/(.*)$ https://www.4399.com/ break;
}

网页测试

在这里插入图片描述

在这里插入图片描述

常见的flag

flag作用
last基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个 一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理 而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break中止Rewrite,不再继续匹配 一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求, 且不再会被当前location内的任何rewrite规则所检查
redirect以临时重定向的HTTP状态302返回新的URL
permanent以永久重定向的HTTP状态301返回新的URL

rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
|匹配 | 之前或之后的实体
()分组,组成一组用于匹配的实体,通常会有 | 来协助
兼容正则表达式(PCRE)库,基本语法如下:
标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
|匹配 | 之前或之后的实体
()分组,组成一组用于匹配的实体,通常会有 | 来协助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值