nginx平滑升级

平滑升级的步骤

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

部署nginx

获取之前安装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

下载新的模块

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


此时echo-nginx模块已经上传
[root@nginx ~]# ls
anaconda-ks.cfg  echo-nginx-module-master.zip  nginx-1.24.0.tar.gz

重新编译软件

//安装unzip工具
[root@nginx ~]# yum -y install unzip

//解压新的模块包
[root@nginx ~]# unzip echo-nginx-module-master.zip

//再次解压nginx源码包
[root@nginx ~]# tar -zxf nginx-1.20.0.tar.gz

//添加新的模块进行编译安装
[root@nginx ~]# cd nginx-1.20.0/
[root@nginx nginx-1.20.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


//查看objs目录下没有nginx程序
[root@nginx nginx-1.20.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@nginx nginx-1.20.0]# ls objs/
addon  autoconf.err  Makefile  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  src

//使用make编译
[root@nginx nginx-1.20.0]# make

//编译完成之后再次进行查看objs目录下是否有nginx程序
[root@nginx nginx-1.20.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.20.0]#

5、备份源程序并停止、覆盖、启动服务
//先查看升级前和升级后的版本区别,主要看编译参数
//升级前
[root@nginx nginx-1.20.0]# nginx -V
nginx version: nginx/1.20.0
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 nginx-1.20.0]# objs/nginx -V
nginx version: nginx/1.20.0
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 --add-module=../echo-nginx-module-master

//停止、备份、覆盖、启动程序
//停止服务
[root@nginx nginx-1.20.0]# nginx -s stop
//备份
[root@nginx nginx-1.20.0]# cp /usr/local/nginx/sbin/nginx /opt/
//覆盖
[root@nginx nginx-1.20.0]# cp objs/nginx /usr/local/nginx/sbin/ 
cp:是否覆盖'/usr/local/nginx/sbin/nginx'? y
//启动服务
[root@nginx nginx-1.20.0]# /usr/local/nginx/sbin/nginx
//查看端口
[root@nginx nginx-1.20.0]# ss -anlt
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN     0           128                    0.0.0.0:111                 0.0.0.0:*        
LISTEN     0           128                    0.0.0.0:80                  0.0.0.0:* 

//查看nginx信息
[root@nginx nginx-1.20.0]# nginx -V
nginx version: nginx/1.20.0
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 --add-module=../echo-nginx-module-master

测试

引用echo模块

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
//修改location参数内容就行
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                echo "hello";
        }

重载nginx

nginx -s reload

//使用浏览器访问,发现浏览器无法支持echo模块,会当成文件进行下载

//使用命令进行访问

location案例

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

功能:

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

语法:

location [修饰符] pattern {......}

修饰符

=                        精确匹配

~                        正则表达式模式匹配,区分大小写

~*                      正则表达式模式匹配,不区分大小写

^~                      前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式

@                      定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或者error_page等

 无修饰符

//没有修饰符表示必须以指定模式开始,如:
server {
  server_name localhost;
  location /abc {
    ......
  }
}

//增加一个location字段
 [root@localhost conf]# vim nginx.conf

       location /abc {
           echo "hello";
       }

 

用“=”精准匹配 

//用“=”精准匹配
location = /abc {
                echo "chenyu";
        }


重载 nginx -s reload

访问

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

// ~:表示指定的正则表达式要区分大小写
        location ~ /abc$ {
                echo "hello";
        }

此时已abc结尾就可以访问 

 不区分大小写

// ~*:表示指定的正则表达式不区分大小写:
        location ~*  /abc$ {
                echo "hello";
        }

当=与~同时存在

//当“=”和“~”同时存在时,此时时 = 优先于 ~
        location ~ /abc$ {
                echo "hello";
        }
        location = /abc {
                echo "hi";
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值