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

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

1.环境说明:

主机名称IP地址所需服务系统版本
nginx192.168.195.133nginx-1.22.1(非最新版即可)centos 8
zabbix192.168.195.130无(充当访问nginx主机的客户机)centos 8

2.nginx平滑升级原理:

Nginx 平滑升级是指在不中断服务的情况下,将旧版本的 Nginx 服务器升级到新版本。其原理主要基于以下几个步骤:

  1. 复制新版本的 Nginx 可执行文件:首先,将新版本的 Nginx 可执行文件复制到服务器上的指定目录。这个目录可以是旧版本 Nginx 的安装目录或其他合适的位置。
  2. 重启旧版本的 Nginx 进程:使用旧版本的 Nginx 启动脚本或命令来重启 Nginx 进程。这样做会使新的 Nginx 可执行文件生效,但并不会立即中断正在运行的连接。
  3. 新旧版本并存:旧版本的 Nginx 进程会继续接收和处理来自客户端的连接,同时新版本的 Nginx 进程在相同的端口上启动并开始监听新的连接。
  4. 逐渐切换连接到新版本:新版本的 Nginx 进程会逐步接管旧版本的连接。这可以通过逐渐降低旧版本 Nginx 的 worker 进程数,并相应增加新版本 Nginx 的 worker 进程数来实现。当旧版本的连接逐渐结束时,新版本的 Nginx 进程将完全接管所有连接。

通过这种平滑升级的方式,可以最大程度地减少服务中断时间,确保用户无感知地完成升级过程。但是在实际操作中,仍需谨慎进行,确保升级过程的稳定性和可靠性。

3.平滑升级nginx,并添加echo模块

3.1.查看当前nginx版本以及老版本编译参数信息

//首先查看我们当前的nginx版本,使用-v选项,查看得知为nginx-1.22.1
[root@nginx ~]# nginx -v
nginx version: nginx/1.22.1

//使用-V选项可以查看nginx编译时的参数信息
[root@nginx ~]# 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@nginx ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-1.22.1

3.2.下载nginx-1.24.0源码包和echo模块

nginx官网nginx: download
在这里插入图片描述

//下载nginx-1.24.0的源码包
[root@nginx ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-19 16:33:35--  https://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: ‘nginx-1.24.0.tar.gz.1’

nginx-1.24.0.tar.gz.1       100%[==========================================>]   1.06M   648KB/s    in 1.7s    

2023-10-19 16:33:39 (648 KB/s) - ‘nginx-1.24.0.tar.gz.1’ saved [1112471/1112471]

[root@nginx ~]# ls
anaconda-ks.cfg   nginx-1.22.1.tar.gz  nginx-1.24.0.tar.gz

github网站拉取echo模块Repository search results · GitHub
在这里插入图片描述
在这里插入图片描述

//安装所需命令的软件包
[root@nginx ~]# yum -y install git
[root@nginx ~]# which git
/usr/bin/git

//通过git命令拉取到虚拟机中
[root@nginx ~]# 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 | 944.00 KiB/s, done.
Resolving deltas: 100% (1645/1645), done.
[root@nginx ~]# ls
anaconda-ks.cfg  echo-nginx-module  nginx-1.22.1.tar.gz  nginx-1.24.0.tar.gz

3.3.编译安装nginx-1.24.0

//解压nginx-1.24.0
[root@nginx ~]# tar xf nginx-1.24.0.tar.gz 
[root@nginx ~]# ls
anaconda-ks.cfg  echo-nginx-module  nginx-1.22.1.tar.gz  nginx-1.24.0  nginx-1.24.0.tar.gz

//编译安装nginx-1.24.0,并添加一个echo模块
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ./configure --help | grep add-module
  --add-module=PATH                  enable external module
[root@nginx 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
省略. . .
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

//编译新版本或新功能(不能使用make install)
[root@nginx nginx-1.24.0]# make                  //直接使用make编译,不能使用make install

//编译完成后,我们的主程序文件是安装目录中的objs目录中的nginx,我们也可以看出/usr/local/nginx/sbin/nginx同当前目录下objs目录下的nginx文件大小的区别
[root@nginx nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@nginx nginx-1.24.0]# ls objs/
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src
[root@nginx nginx-1.24.0]# ll /usr/local/nginx/sbin/nginx 
-rwxr-xr-x 1 root root 6192536 Oct 16 15:54 /usr/local/nginx/sbin/nginx
[root@nginx nginx-1.24.0]# ll objs/nginx
-rwxr-xr-x 1 root root 6740696 Oct 19 16:46 objs/nginx

//我们需要将objs/nginx替换掉/usr/local/nginx/sbin/nginx,此时我们只能使用一条命令的方式将原来版本启动的nginx停止,然后将其替换后,立即使用新版本的nginx启动服务
[root@nginx nginx-1.24.0]# ss -antl   //此时nginx服务处于开启状态
State        Recv-Q        Send-Q                  Local Address:Port                 Peer Address:Port        
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                           [::]:*           
[root@nginx nginx-1.24.0]# cd objs/
[root@nginx 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@nginx objs]# /usr/local/nginx/sbin/nginx -s stop;\cp nginx /usr/local/nginx/sbin/nginx;nginx     //一个分号为一条命令,\cp是为了覆盖时不询问是否覆盖,而是直接覆盖
[root@nginx objs]# ss -antl     //端口正常存在
State        Recv-Q        Send-Q                  Local Address:Port                 Peer Address:Port        
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                           [::]:*           
[root@nginx objs]# nginx -v         //再次查看版本,成功升级为nginx-1.24.0版本
nginx version: nginx/1.24.0

平滑升级完成

3.4.echo模块的使用

Nginx中的echo模块是一个第三方模块,用于自定义响应内容。它的主要用途是在Nginx服务器中生成自定义响应,而不是从后端服务器获取内容。echo模块通常用于以下一些情况:

  1. 测试和调试:在开发和调试过程中,echo模块允许管理员轻松生成自定义的HTTP响应,以验证Nginx配置是否按预期工作。
  2. 自定义错误页面:你可以使用echo模块来创建自定义的错误页面,以替代Nginx默认的错误页面。这样,你可以提供更有吸引力或品牌一致的错误信息给用户。
  3. 重定向和重写echo模块允许你创建自定义的重定向规则或URL重写规则,以满足特定需求。这可以用于URL映射或路由规则。
  4. 动态内容生成:虽然不是最有效的方式,但你可以使用echo模块来生成一些动态内容,例如当前时间戳或其他简单的信息。这通常不如通过后端应用程序生成内容高效。
  5. HTTP头信息操作echo模块也允许你操作HTTP响应的头信息。你可以添加、删除或修改HTTP头,以满足特定的需求。

以下是一个简单的示例,展示了如何使用echo模块来生成自定义的HTTP响应:

location /custom {
    echo "This is a custom response";
    echo "Generated by the echo module";
}

需要注意的是,echo模块是一个第三方模块,可能需要手动编译Nginx以包含该模块。此外,它通常不建议用于生成大量动态内容,因为Nginx的性能在处理静态内容和代理请求方面更出色。对于大规模的动态内容生成,通常会使用专门的Web应用程序服务器,如Node.js、Django、或Ruby on Rails。

4.location的配置

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

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

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

常用修饰符说明:

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

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . 
location /abc {
            echo "this is /abc";
            root   html;
        }
. . . . .
[root@nginx ~]# nginx -s reload
        
//以下方法都能访问到,?后面是用来传参数的,相当于登录用户输入账号密码后,会以变量的方式传给服务器
[root@zabbix ~]# curl http://192.168.195.137/abc
this is /abc
[root@zabbix ~]# curl http://192.168.195.137/abc?a=10\&b=20
this is /abc
[root@zabbix ~]# curl http://192.168.195.137/abc/
this is /abc

=:表示必须与指定的模式精确匹配,如:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . 
location = /abc {
            echo "this is =abc";
            root   html;
        }
. . . . .
[root@nginx ~]# nginx -s reload

//以下方法都能访问到
[root@zabbix ~]# curl http://192.168.195.137/abc
this is =abc
[root@zabbix ~]# curl http://192.168.195.137/abc?a=10\&b=20
this is =abc

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . 
location ~ ^/abc$ {
            echo "this is ~abc";
            root   html;
        }
. . . . .
[root@nginx ~]# nginx -s reload

//以下方法都能访问到
[root@zabbix ~]# curl http://192.168.195.137/abc
this is ~abc
[root@zabbix ~]# curl http://192.168.195.137/abc?a=10\&b=20
this is ~abc

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . 
location ~* ^/abc$ {
            echo "this is ~*abc";
            root   html;
        }
. . . . .
[root@nginx ~]# nginx -s reload

//以下方法都能访问到
[root@zabbix ~]# curl http://192.168.195.137/abc
this is ~*abc
[root@zabbix ~]# curl http://192.168.195.137/ABC
this is ~*abc
[root@zabbix ~]# curl http://192.168.195.137/abc?a=10\&b=20
this is ~*abc
[root@zabbix ~]# curl http://192.168.195.137/ABC?a=10\&b=20
this is ~*abc

^~:表示指定的正则表达式的路径,以他开头的都能匹配到,如:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . 
location ^~ /abc/ {
            echo "this is ^~abc";
            root   html;
        }
. . . . .
[root@nginx ~]# nginx -s reload

//以下方法都能访问到
[root@zabbix ~]# curl http://192.168.195.137/abc/
this is ^~abc
[root@zabbix ~]# curl http://192.168.195.137/abc/fdf
this is ^~abc

~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式

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

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

优先级次序如下:

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

5.rewrite(也叫URL重定向)

语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

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

rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

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

常见的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)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
|匹配 | 之前或之后的实体
()分组,组成一组用于匹配的实体,通常会有 | 来协助

捕获子表达式,可以捕获放在()之间的任何文本,比如:

^(hello|sir)$       //字符串为“hi sir”捕获的结果:$1=hi$2=sir

//这些被捕获的数据,在后面就可以当变量一样使用了

5.1.工作原理:

  1. 匹配请求URIrewrite 首先使用正则表达式来匹配请求的URI。这个正则表达式通常出现在 location 块内,它会尝试与请求URI进行匹配。
  2. 捕获组:如果正则表达式中包含捕获组,那么匹配成功后,捕获组可以用于后续的处理。例如,你可以在正则表达式中使用括号来创建捕获组,然后在重写字符串中使用 $1$2 等来引用这些捕获组。
  3. 替换URI:如果正则表达式匹配成功,rewrite 将使用指定的替换字符串来修改请求的URI。替换字符串通常包含替代变量,例如 $1$2 等,这些变量引用了正则表达式中的捕获组。
  4. 选择重定向类型rewrite 通常还支持一些标志,如 permanentredirect,用于指定重定向类型。例如,permanent 表示永久重定向(HTTP 301),而 redirect 表示临时重定向(HTTP 302)。
  5. 继续处理:一旦URI被重写,Nginx会继续处理重写后的URI。这可能涉及进一步的location块匹配、其他rewrite指令的处理或将请求传递给后端服务器或静态资源。

下面是一个示例,演示了如何使用rewrite来重定向请求:

location /old-path/ {
    rewrite ^/old-path/(.*)$ /new-path/$1 permanent;
}

在这个示例中,如果请求URI匹配/old-path/rewrite将捕获匹配的部分(例如,xxx),然后将请求重定向到/new-path/xxx,使用permanent标志表示永久重定向。

总之,rewrite指令的工作原理是基于正则表达式的匹配和URI的替换,使得你可以对传入的HTTP请求进行重定向和修改。

5.2.配置rewrite

//在html目录下新建一个目录文件,存放跳转页面
[root@nginx html]# cd
[root@nginx ~]# cd /usr/local/nginx/html
[root@nginx html]# ls
50x.html  index.html
[root@nginx html]# mkdir images
[root@nginx html]# ls
50x.html  images  index.html
[root@nginx html]# cd images/
[root@nginx images]# echo "hello world" > index.html
[root@nginx images]# cat index.html
hello world
[root@nginx images]#

能够访问
在这里插入图片描述

//现在将html下面的images目录改为其他的名字(imgs),然后再次访问images看是否能够访问
[root@nginx images]# cd ..
[root@nginx html]# ls
50x.html  images  index.html
[root@nginx html]# mv images imgs
[root@nginx html]# ls
50x.html  imgs  index.html

无法访问
在这里插入图片描述

然而我们并不能因为更改了域名就让用户无法通过原地址访问,所以我们就需要用到rewrite

[root@nginx conf]# vim nginx.conf
[root@nginx conf]# cat nginx.conf
. . . . . 
location /images {
            rewrite ^/images/(.*)$ /imgs/$1 break;
        }
. . . . . 
[root@nginx conf]# nginx -s reload

再次访问,访问成功
在这里插入图片描述

如果想让URL跳转到网络上的某一个网站,也是同样的配置方法
在这里插入图片描述

[root@nginx conf]# vim nginx.conf
[root@nginx conf]# cat nginx.con
. . . . .
location /images {
            rewrite ^/images/(.*)$ https://www.bing.com/images/search?q=%E5%B0%8F%E7%8B%97%E5%9B%BE%E7%89%87&form=IQFRBA&id=52207A2CC7D8B74EFE142A76A4CDE2B26A8594C3&first=1&disoverlay=1 break;
        }
. . . . .
[root@nginx conf]# nginx -s reload

成功跳转
在这里插入图片描述

flag中last和break的用法

先通过images跳转到imgs,然后再次请求,请求发现还有一个URL跳转,最终跳转到风景图的页面,break停止规则检查
[root@nginx conf]# vim nginx.conf
[root@nginx conf]# cat nginx.conf
. . . . .
        location /images {
            rewrite ^/images/(.*)$ /imgs/$1 last;
        }

        location /imgs {
            rewrite ^/imgs/(.*)$ /ftx/$1 last;
        }

        location /ftx {
            rewrite ^/ftx/(.*)$ http://baidu.com break;
        }

成功访问风景图页面
在这里插入图片描述

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值