nginx平滑升级、重定向

nginx平滑升级、重定向

平滑升级步骤:(升级版本、增加新功能)

1、获取老版本的编译信息

2、对老版本进行备份

3、编译新版本或者新功能(不能执行make install 否则会直接将老版本替换)

4、手动替换新版本并重启

5、验证新版本

查看版本和编译参数
//查看nginx版本
[root@centos ~]# nginx -v
nginx version: nginx/1.22.1

//查看nginx编译参数
[root@centos ~]# 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

nginx  -t  //检查语法

升级nginx

首先在nginx news官网 点击download > 下载1.24.0版本

在这里插入图片描述

//下载1.24.0的版本
[root@centos ~]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-19 23:10:31--  http://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: ‘nginx-1.24.0.tar.gz’

nginx-1.24.0.tar.gz 100%[=================>]   1.06M   925KB/s    in 1.2s    

2023-10-19 23:10:32 (925 KB/s) - ‘nginx-1.24.0.tar.gz’ saved [1112471/1112471]
下载功能模块
//因为目前版本的nginx不支持echo模块 所以我们需要自己去添加
在https://github.com/ 官网搜索echo nginx包
//首先需要下载git(克隆)命令
[root@centos ~]# yum -y install git
//然后克隆nginx包
[root@centos ~]# git clone https://github.com/openresty/echo-nginx-module.git

//此时查看发现 添加了echo-nginx-module
[root@centos ~]# ls
anaconda-ks.cfg  echo-nginx-module  nginx-1.24.0.tar.gz
解压新模块
//解压
[root@centos ~]# tar xf nginx-1.24.0.tar.gz 
[root@centos ~]# ls
anaconda-ks.cfg  echo-nginx-module  nginx-1.24.0  nginx-1.24.0.tar.gz
[root@centos ~]# cd nginx-1.24.0
[root@centos nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

此时因为我们需要对老版本进行升级, 我们得在原来老版本的基础上来进行编译,为了防止升级导致老版本的一些功能丧失了

//首先使用nginx -V 查看老版本编译过程,在此基础上增加新功能
[root@centos nginx-1.24.0]# 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@centos nginx-1.24.0]# ./configure --help | grep add-module
  --add-module=PATH                  enable external module

//此处通过查看[root@centos ~]# ls
anaconda-ks.cfg  echo-nginx-module  nginx-1.24.0  nginx-1.24.0.tar.gz
我们在nginx-1.24.0里面进行编译,而我们需要添加的模块是echo-nginx-module 所以此处写的路径为上级目录下面的echo-nginx-module文件

//给老版本添加echo模块
//编译
[root@centos 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的老版本
[root@centos ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-20231019
编译
[root@centos nginx-1.24.0]# make
          //通过make编译型版本的程序文件
[root@centos nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@centos 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
                //此处的nginx就是主程序
                
[root@centos nginx-1.24.0]# cd objs/     
[root@centos objs]# ll /usr/local/nginx/sbin/nginx 
-rwxr-xr-x 1 root root 6192528 Oct 18 06:03 /usr/local/nginx/sbin/nginx  //此处显示老版本
[root@centos objs]# ll nginx
-rwxr-xr-x 1 root root 6740696 Oct 19 23:48 nginx   //此处显示新版本

//由于nginx是静态编译,所以他的所有功能在编译之后都在主程序里面,所以将主程序替换就可以得到新的版本
[root@centos objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;systemctl start nginx

//查看版本是否升级
[root@centos objs]# nginx -v
nginx version: nginx/1.24.0
升级成功

访问:
在这里插入图片描述

echo模块的使用

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

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

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

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

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

常用修饰符说明:

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写
~*正则表达式模式匹配,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等
没有修饰符表示必须以指定模式开始
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
//添加
location /abc {
                    echo "this is /abc";
                    root   html;
                 }
//重新加载
[root@centos ~]# nginx -s reload
//访问:以下方法都能访问到,?后面是用来传参数的,相当于登录用户输入账号密码后,会以变量的方式传给服务器
[root@centos ~]# curl http://192.168.134.155/abc
this is /abc
[root@centos ~]# curl http://192.168.134.155/abc?a=10\&b=20
this is /abc
[root@centos ~]# curl http://192.168.134.155/abc/
this is /abc

=:表示必须与指定的模式精确匹配
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
//添加:
        location = /abc {
                    echo "this is =abc";
                    root   html;
               }
[root@centos ~]# nginx -s reload
//访问:
[root@centos ~]# curl http://192.168.134.155/abc
this is =abc
[root@centos ~]# curl http://192.168.134.155/abc?a=10\&b=20
this is =abc
~:表示指定的正则表达式要区分大小写
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
//添加
        location ~ ^/abc${
                    echo "this is ~abc";
                    root   html;
               }
[root@centos ~]# nginx -s reload
//访问
[root@centos ~]# curl http://192.168.134.155/abc
this is =abc
[root@centos ~]# curl http://192.168.134.155/abc?a=10\&b=20
this is =abc
~*:表示指定的正则表达式不区分大小写
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
//添加
location ~* ^/abc$ {
            echo "this is ~*abc";
            root   html;
        }

[root@centos ~]# nginx -s reload
//访问
[root@centos ~]# curl http://192.168.134.155/abc
this is ~*abc
[root@centos ~]# curl http://192.168.134.155/ABC
this is ~*abc
[root@centos ~]# curl http://192.168.134.155/abc?a=10\&b=20
this is ~*abc
[root@centos ~]# curl http://192.168.134.155/ABC?a=10\&b=20
this is ~*abc

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

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

//访问
[root@centos ~]# curl http://192.168.134.155/abc/
this is ^~abc
[root@centos ~]# curl http://192.168.134.155/abc/fdb
this is ^~abc
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为

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

优先级次序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )

rewrite(也叫URL重定向)

语法:rewrite regex replacement flag;,(replacement可以是某个路径,也可以是某个URL)如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
//此处的$1用于引用(.*.jpg)匹配到的内容

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

常见的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

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

1、匹配请求URI:rewrite 首先使用正则表达式来匹配请求的URI。这个正则表达式通常出现在 location 块内,它会尝试与请求URI进行匹配。
2、捕获组:如果正则表达式中包含捕获组,那么匹配成功后,捕获组可以用于后续的处理。例如,你可以在正则表达式中使用括号来创建捕获组,然后在重写字符串中使用 $1、$2 等来引用这些捕获组。
3、替换URI:如果正则表达式匹配成功,rewrite 将使用指定的替换字符串来修改请求的URI。替换字符串通常包含替代变量,例如 $1、$2 等,这些变量引用了正则表达式中的捕获组。
4、选择重定向类型:rewrite 通常还支持一些标志,如 permanent 和 redirect,用于指定重定向类型。例如,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请求进行重定向和修改。

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

访问:

在这里插入图片描述

//现在将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

访问 发现无法访问: 192.168.134.155/images/index.html

在这里插入图片描述

我们不能让其更改了域名就无法访问,所以此时用到了rewrite

[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
//添加
location /images {
            rewrite ^/images/(.*)$ /imgs/$1 break;
        }
[root@centos ~]# nginx -s reload

在这里插入图片描述

想让url跳转到你指定的网页,首先复制一个网址路径:肖战(中国内地男演员、歌手)_百度百科 (baidu.com)
在这里插入图片描述

[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
...
location /images {
            rewrite ^/images/(.*)$ https://baike.baidu.com/item/%E8%82%96%E6%88%98/18866899?fr=ge_ala break;
        }
...
[root@centos ~]# nginx -s reload
成功 跳转访问

此时再通过192.168.134.155/images/index.html 访问

在这里插入图片描述

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/(.*)$ /lcy/$1 last;
        }

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

[root@centos ~]# nginx -s reload

通过访问192.168.134.155/images/index.html 访问百度成功

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值