nginx平滑升级,location案例

 

 

统计ip访问次数

 

[root@localhost ~]# awk '{ip[$1]++}END{for(c in ip) print c,ip[c]}' /var/log/nginx/access.log 
[root@localhost ~]# awk '{ip[$1]++}END{for(c in ip) print c,ip[c]}' /var/log/nginx/access.log 
192.168.170.1 3
[root@localhost ~]# curl 192.168.170.132
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
//排序,取前十个
[root@localhost ~]# awk '{ip[$1]++}END{for(c in ip) print c,ip[c]}' /var/log/nginx/access.log|sort -rk 2 | head
192.168.170.1 3
192.168.170.132 1

 

 

 

平滑升级的步骤

1.获取之前的编译参数

2.下载新模块

3重新编译软件,加上--add-module=新模块的解压路径

4.备份原程序并停止服务

5.把原程序用新程序覆盖

6.启动新程序

 

 

 

平滑升级是基于原来的功能上增加新的功能

//查看安装的时候用了哪些模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
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 ~]# ls
anaconda-ks.cfg  echo-nginx-module-master.zip  mei  nginx-1.20.0.tar.gz
[root@localhost ~]# yum install -y unzip

[root@localhost ~]# unzip echo-nginx-module-master.zip
[root@localhost ~]# ls
anaconda-ks.cfg           echo-nginx-module-master.zip  nginx-1.20.0.tar.gz
echo-nginx-module-master  mei

//下载下来的模块要与安装目录平级
[root@localhost ~]# mv echo-nginx-module-master /usr/local/src/
[root@localhost ~]# 
[root@localhost ~]# cd /usr/local/src/  && ls
echo-nginx-module-master  nginx-1.20.0


//编译,增加新的模块

[root@localhost nginx-1.20.0]# ./configure --help|grep add
  --with-http_addition_module        enable ngx_http_addition_module
  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

[root@localhost 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=/usr/local/src/echo-nginx-module-master
[root@localhost nginx-1.20.0]# make

//先备份一下之前的程序
[root@localhost nginx-1.20.0]# cp /usr/local/nginx/sbin/nginx  /opt/

//停止服务并将原程序覆盖
[root@localhost nginx-1.20.0]# nginx -s stop && mv /usr/local/nginx/sbin/nginx /tmp && cp objs/nginx /usr/local/nginx/sbin/nginx  && /usr/local/nginx/sbin/nginx 
[root@localhost nginx-1.20.0]# 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@localhost nginx-1.20.0]# nginx -V
nginx version: nginx/1.20.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
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=/usr/local/src/echo-nginx-module-master

 

测试一下新功能

[root@localhost conf]# vim nginx.conf
...
...
...    ​

server {
       ​listen       80;
       ​server_name  localhost;

       ​#charset koi8-r;

       ​#access_log  logs/host.access.log  main;

       ​location / {
           ​echo "hello world";
           ​
       ​}

...

...


//测试一下语法有无问题
[root@localhost conf]# 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

 

 

 

 

 

 

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

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

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

 

 

常用修饰符说明:

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

 

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

server {
  server_name www.idfsoft.com;
  location /abc {
    ......
  }
}

 

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

       location /abc {
           echo "hehe";
    

 

 

 

 

 

 

 

 

 

用    “=”  精准匹配

[root@localhost conf]# vim nginx.conf 
       location = /abc {
           echo "nihao";
        }

 

 

 

 

 

 

 

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

server {
  server_name www.idfsoft.com;
  location ~ ^/abc$ {
  ......
  }
}

 

//需要先注释掉= ,因为他的优先级更高

[root@localhost conf]# vim nginx.conf
        location / {
           root html;
           index index.html;
        }
        location /abc {
           echo "hehe";
        }
# location
= /abc { echo "nihao"; } location ~ ^/abc$ { echo "hello"; } [root@localhost conf]# nginx -s reload

 

 

 

 

 

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

server {
  server_name www.idfsoft.com;
  location ~* ^/abc$ {
    ......
  }
}

 

[root@localhost conf]# vim nginx.conf
        location / {
           root html;
           index index.html;
        }
        location /abc {
           echo "hehe";
        }
#       location = /abc {
#          echo "nihao";
#       }       
        location ~ ^/abc$ {
           echo "hello";
        }
        location ~* ^/abc$ {
           echo "123";
        }

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

 

 

 

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

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

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

优先级次序如下:

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

 

posted @ 2021-05-30 15:03  取个名字真滴难  阅读(126)  评论(0)    收藏  举报