zoukankan      html  css  js  c++  java
  • nginx 平滑升级和location配置案例

    nginx平滑升级

    概念:保证原有功能的基础上新增功能(模块)

    流程:

    1. 获取原编译参数
    2. 下载需要的模块(GitHub等网站拉取)
    3. 重新编译,加上--add-module=/MODULE_PATH
    4. 备份程序并停止服务
    5. 覆盖新程序
    6. 启动新程序
    //查看已编译的模块
    [root@node1 ~]# 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
    
    //(已echo模块为例)拉取echo模块
    [root@node1 ~]# git clone https://github.com/openresty/echo-nginx-module
    ...
    [root@node1 ~]# ll
    total 4
    -rw-------. 1 root root 1179 Jan  4 00:36 anaconda-ks.cfg
    drwxr-xr-x. 6 root root  186 May 30 18:00 echo-nginx-module
    
    [root@node1 ~]# cd /usr/src/nginx-1.20.0/
    [root@node1 nginx-1.20.0]# ll objs/ |grep nginx$
    -rwxr-xr-x. 1 root root 6303992 May 24 22:07 nginx
    
    //编译安装
    [root@node1 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=/root/echo-nginx-module	#添加该行
    [root@node1 nginx-1.20.0]# make
    ...略
    [root@node1 nginx-1.20.0]# ll objs/|grep nginx$
    -rwxr-xr-x. 1 root root 6826280 May 30 18:14 nginx
    
    //备份并升级
    [root@node1 nginx-1.20.0]# nginx -s stop && cp /usr/local/nginx/sbin/nginx /tmp && cp objs/nginx /usr/local/nginx/sbin/ && nginx
    cp: overwrite '/usr/local/nginx/sbin/nginx'? y
    
    //查看编译后参数
    [root@node1 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=/root/echo-nginx-module
    
    

    echo模块演示,通过echo一段信息判断是否能匹配location的资源

    [root@node1 nginx]# vim conf/nginx.conf
    ...
    server段追加
      location / {
                echo "rua";
            }
    [root@node1 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@node1 nginx]# nginx -s reload
        
    

    网页浏览器不支持echo

    location配置案例

    location相关配置

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

    Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }	#修饰符,uri
     		location @name { ... }
    Default:	—
    Context:	server, location(支持嵌套)
    
    • root模式:指定一个目录,会搜索该目录下的index.html文件,不会去该目录下一级去找
    [root@node1 ~]# cd /usr/local/nginx/html/
    [root@node1 html]# ll
    total 8
    -rw-r--r--. 1 root root 494 May 24 22:07 50x.html
    -rw-r--r--. 1 root root 612 May 24 22:07 index.html
    //移动默认网页到test目录
    [root@node1 html]# mkdir test
    [root@node1 html]# mv index.html test/
    [root@node1 html]# ll test/
    total 4
    -rw-r--r--. 1 root root 612 May 24 22:07 index.html
    
    [root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
    ...
    server段追加
            location / {	#“/”表示访问时默认网页
                root   html/test/;	#指定html下的test目录
                index  index.html index.htm;	#默认网页名称
            }
    [root@node1 ~]# nginx -s reload
    

    • alias模式(不会暴露网页存放目录,更安全)
    Syntax:	alias path;
    Default:	—
    Context:	location
    
    [root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
    ...server段添加
     location /a {		#定义一个访问别名
         alias  html/test/;	#指定别名默认网页存放路径,不在安装目录下则需要绝对路径
         index  index.html index.htm;
            }
    [root@node1 ~]# nginx -s reload
    

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

    location匹配案例

    通过echo来验证

    //通配
     server {
            listen       80;
            server_name  a.example.com;
    
            location  /a {
                echo "this is /";
            }
    
    

    //=精确匹配
      server {
            listen       80;
            server_name  a.example.com;
    
            location = /a {
                echo "this is =";
            }
    
    

    //正则匹配
    //区分大小写
     location  ~ /a {
                echo "this is ~";
            }
    
    //不区分大小写
    
            location  ~* /a {
                echo "this is ~*";
            }
    
    

    只有开头含有小写a才能匹配

    开头大小写a都能匹配

    //正则前缀匹配uri开始
    
            location  ^~ /a {
                echo "this is ~*";
            }
    
    

    只匹配小写a开头的

    匹配优先级:=, ^~, ~/~*,/ 
    
    location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
    
  • 相关阅读:
    常用Linux命令
    SQL必知必会-笔记
    【ubuntu】install openbox+tint2+bmenu on ubuntu12.04.4
    【ruby】安装Ruby
    【ruby】快速安装gems的方法
    【sinatra】设置默认的端口
    【sinatra】修改默认ip绑定
    【sinatra】结合Padrino framework
    【sinatra】安装测试
    【rails3教材】博客构建过程2
  • 原文地址:https://www.cnblogs.com/fangxinxin/p/14829747.html
Copyright © 2011-2022 走看看