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

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

平滑升级添加echo模块

查看nginx版本号和编译信息

[root@localhost ~]# nginx -v
nginx version: nginx/1.22.1
[root@localhost ~]# 
[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 ~]# cd /usr/src/
[root@localhost src]# 
[root@localhost src]# ls
debug  kernels  nginx-1.22.1  nginx-1.22.1.tar.gz
[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.22.1.tar.gz  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
...
Resolving deltas: 100% (1645/1645), done.
[root@localhost src]# 
[root@localhost src]# ls
debug  echo-nginx-module  kernels  nginx-1.22.1  nginx-1.22.1.tar.gz  nginx-1.24.0.tar.gz
[root@localhost src]# 

备份旧版本nginx

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

解压新版本nginx包,再次编译nginx添加 --add-module

注意:新版本只编译不安装,不要执行 make install

[root@localhost src]# tar xf nginx-1.24.0.tar.gz 
[root@localhost src]# 
[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
[root@localhost nginx-1.24.0]# 

手动替换新版本并重启

[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# ls
addon  autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@localhost objs]# 
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;nginx
[root@localhost objs]# 
[root@localhost objs]# 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                             [::]:*                            
[root@localhost objs]# 

查看版本号

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

验证echo模块是否成功添加

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        location / {
            echo "hello world";
            root   html;
            index  index.html index.htm;
        }
...
[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相匹配

常用修饰符说明:

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

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

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

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

[root@localhost ~]# vi /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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

使用命令行的方式访问

在没有修饰符的情况下只要你的域名后面带上了 /abc 这样一个字样都能匹配到。

[root@node1 ~]# curl http://192.168.200.10/abc
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc/
this is /abc
[root@node1 ~]#

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

[root@localhost ~]# vi /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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

只有匹配内容与等于号后面的内容一样和传参数的情况下才能精确匹配到

[root@node1 ~]# curl http://192.168.200.10/abc
this is =abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is =abc
[root@node1 ~]# 

以下两种无法匹配到

[root@node1 ~]# curl http://192.168.200.10/abc/
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abcbb
this is /abc
[root@node1 ~]# 

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

[root@localhost ~]# vi /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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

只有在开头是 /abc 结尾不能有任何东西的情况下才能匹配到

[root@node1 ~]# curl http://192.168.200.10/abc
this is ~abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is ~abc
[root@node1 ~]# 

以下三种无法匹配到

[root@node1 ~]# curl http://192.168.200.10/abc/
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abcbb
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/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@node1 ~]# 

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

[root@localhost ~]# vi /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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

只有在大小写都有和全是大写的情况下才能被不区分大小写匹配到

[root@node1 ~]# curl http://192.168.200.10/abC
this is ~*abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/ABC
this is ~*abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/aBc\?a\=10
this is ~*abc
[root@node1 ~]# 

以下两种无法匹配到

[root@node1 ~]# curl http://192.168.200.10/abc/
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abcde
this is /abc
[root@node1 ~]# 

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

[root@localhost ~]# vi /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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

只有在以 abc 开头并且后面有东西的情况下,才能被 ^~ 匹配到

[root@node1 ~]# curl http://192.168.200.10/abc/ 
this is ^~abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc/abab
this is ^~abc
[root@node1 ~]#

以下几种情况无法匹配到

[root@node1 ~]# curl http://192.168.200.10/abcde
this is /abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc
this is ~abc
[root@node1 ~]# 
[root@node1 ~]# curl http://192.168.200.10/abc\?a\=10
this is ~abc
[root@node1 ~]#

rewrite配置

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

语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 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

重定向操作

创建一目录添加一个html文件

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir mhy
[root@localhost html]# 
[root@localhost html]# ls
50x.html  index.html  mhy
[root@localhost html]# cd mhy/
[root@localhost mhy]# 
[root@localhost mhy]# echo "hello world" > index.html

可以访问到内容

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

将mhy目录重命名

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html  mhy
[root@localhost html]# 
[root@localhost html]# mv mhy op
[root@localhost html]# 
[root@localhost html]# ls
50x.html  index.html  op
[root@localhost html]# 

使用原路径再次访问,无法访问到内容

[root@localhost html]# curl http://192.168.200.10/mhy/index.html
<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 html]#

添加 location 做重定向

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
...
        location /mhy {
            rewrite ^/mhy/(.*)$ /op/$1 break;
        }
...
[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 ~]# 
[root@localhost ~]# nginx -s reload
[root@localhost ~]#

再次使用原路径访问,可以访问到内容

[root@localhost html]# curl http://192.168.200.10/mhy/index.html
hello world
[root@localhost html]# 
[root@localhost html]# ls
50x.html  index.html  op
[root@localhost html]# 

重定向可以是某个路径,也可以是某个URL

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
...
        location /mhy {
            rewrite ^/mhy/(.*)$ http://www.baidu.com/index.html break;
        }
...
[root@localhost html]# 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 html]# 
[root@localhost html]# nginx -s reload                    
[root@localhost html]#

重定向访问百度

image-20231020005219904
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这linux不学也罢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值