nginx平滑升级+location案例

这里是接着上一边文章的实验继续做的

一、步骤

1、获取之前的编译参数
2、下载新模块
3、重新编译软件,加上–add-module=新模块的解压路径
4、停止服务并备份原程序
5、把源程序用新程序覆盖
6、启动新程序

二、搭建nginx

nginx搭建详情:http://t.csdnimg.cn/B1QsL

三、平滑升级

1、查看nginx编译参数

[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
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

2、下载包文件

https://github.com/openresty/echo-nginx-module
下载到本地,然后上传到nginx服务器中

[root@nginx ~]# cd /opt/software/
[root@nginx software]# ls
nginx-1.24.0.tar.gz
[root@nginx software]# rz -E
rz waiting to receive.
[root@nginx software]# ls
echo-nginx-module-master.zip  nginx-1.24.0.tar.gz

3、下载工具解压模块包

[root@nginx software]# yum -y install unzip
[root@nginx software]# unzip echo-nginx-module-master.zip 
//再次解压nginx包文件,解压到哪都行但是不要放到usr/local/下面
[root@nginx software]# tar -zxvf nginx-1.24.0.tar.gz
[root@nginx software]# ls
echo-nginx-module-master  echo-nginx-module-master.zip  nginx-1.24.0  nginx-1.24.0.tar.gz

4、添加模块编译安装

[root@nginx software]# cd nginx-1.24.0/
[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-master

5、编译安装

[root@nginx nginx-1.24.0]# ls objs/
addon  autoconf.err  Makefile  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  src
[root@nginx nginx-1.24.0]# make
//编译完成之后再次进行查看objs目录下是否有nginx程序
[root@nginx nginx-1.24.0]# ls objs/
addon  autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src

6、备份源程序并停止、覆盖、启动服务

[root@nginx nginx-1.24.0]# nginx -s stop
[root@nginx nginx-1.24.0]# cp /usr/local/nginx/sbin/nginx /opt/
[root@nginx nginx-1.24.0]# cp objs/nginx /usr/local/nginx/sbin/
cp: overwrite '/usr/local/nginx/sbin/nginx'? y
[root@nginx nginx-1.24.0]# cd /usr/local/nginx
[root@nginx nginx]# nginx
[root@nginx nginx]# ss -anlt
State         Recv-Q        Send-Q               Local Address:Port               Peer Address:Port        Process        
LISTEN        0             511                        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                         [::]:*              

//查看nginx信息

[root@nginx nginx]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
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 --add-module=../echo-nginx-module-master

7、测试引用echo

[root@nginx nginx]# vim conf/nginx.conf
 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                echo "shiqian";
        }

8、检查配置文件

[root@nginx 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 nginx]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

//重载nginx
[root@nginx nginx]# nginx -s reload

在自己电脑的cmd上访问

C:\Users\Emperor>curl http://192.168.100.111
shiqian

四、location案例

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

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

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

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /abc {
                echo "shiqian";
        }

        }

[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 ~]# nginx -s reload

在自己主机的cmd进行访问

//只有当http://192.168.100.111后面接的是/abc时,不管在其后面加上任何内容都能访问
C:\Users\Emperor>curl http://192.168.100.111/abc
shiqian

C:\Users\Emperor>curl http://192.168.100.111/abc/
shiqian

C:\Users\Emperor>curl http://192.168.100.111/abcakhdakd
shiqian

C:\Users\Emperor>curl http://192.168.100.111/abc/dnakwndj
shiqian

2、用“=”精准匹配

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location = /abc {
                echo "shiqian";
        }

        }

[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 ~]# nginx -s reload

在自己电脑使用cmd访问

//只有当http://192.168.100.111后面接的是/abc时,才能访问
C:\Users\Emperor>curl http://192.168.100.111/abc
shiqian

C:\Users\Emperor>curl http://192.168.100.111/abc/daw
<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>

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~ /abc$ {
                echo "shiqian";
        }

        }
        
[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 ~]# nginx -s reload

在自己电脑上使用cmd访问

//只有当http://192.168.100.111后面接的是/abc时,才能访问
C:\Users\Emperor>curl http://192.168.100.111/abc
shiqian

C:\Users\Emperor>curl http://192.168.100.111/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>

4、当“=”和“~”同时存在时,此时时 = 优先于 ~

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~ /abc$ {
                echo "shiqian";
        }

         location = /abc {
                echo "sq";
        }

[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 ~]# nginx -s reload

在自己电脑上使用cmd访问

C:\Users\Emperor>curl http://192.168.100.111/abc
sq

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~* /abc$ {
                echo "shiqian";
        }

在自己电脑上使用cmd访问

C:\Users\Emperor>curl http://192.168.100.111/abc
shiqian

C:\Users\Emperor>curl http://192.168.100.111/aBC
shiqian

location总结

查找顺序和优先级:由高到低
1、带有“=”的精确匹配优先
2、正则表达式按照他们在配置文件中定义的顺序
3、带有“^~”修饰符的,开头匹配
4、带有或者*修饰符的,如果正则表达式与URI匹配
5、没有修饰符的精确匹配

location = 路径
location ^~ 路径
location ~ 正则
location ~* 正则
location 路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值