Nginx基础配置二与平滑升级

Nginx基础配置二与平滑升级

博客接Nginx基础配置一

1. Nginx平滑升级

1.1 获取老版本的编译信息以及版本
//获取信息
[root@kiwi123 ~]# 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
1.2 备份老版本
[root@kiwi123 ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx1.22_backup
[root@kiwi123 ~]# 
1.3 编译新版本

注意:不能执行make install

//获取nginx最新版的包
[root@kiwi123 ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz


//获取echo模块
[root@kiwi123 ~]# yum install -y git
[root@kiwi123 ~]# git clone https://github.com/openresty/echo-nginx-module.git
[root@kiwi123 ~]# ls
echo-nginx-modul


//解压新版nginx并进入
[root@kiwi123 ~]# tar xf nginx-1.24.0.tar.gz && cd nginx-1.24.0
[root@kiwi123 nginx-1.24.0]# 


//开始编译,注意:要在老版本编译信息的基础之上加上--add-module=../echo-nginx-module
[root@kiwi123 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


//开始make
[root@kiwi123 nginx-1.24.0]# make

1.4 手动替换新版本的主要配置
//先看看旧版本的主程序文件与新版本的主程序文件
[root@kiwi123 nginx-1.24.0]# ll /usr/local/nginx/sbin/nginx 
-rwxr-xr-x. 1 root root 6192528 Oct 16 03:05 /usr/local/nginx/sbin/nginx 
[root@kiwi123 nginx-1.24.0]# ll /root/nginx-1.24.0/objs/nginx
-rwxr-xr-x 1 root root 6740696 Oct 19 02:34 /root/nginx-1.24.0/objs/nginx
[root@kiwi123 nginx-1.24.0]# 


//停止旧版本的服务
[root@kiwi123 nginx-1.24.0]# cd /root/nginx-1.24.0/objs/
[root@kiwi123 objs]# /usr/local/nginx/sbin/nginx -s stop


//覆盖旧版本的主程序文件并启动nginx
[root@kiwi123 objs]# \cp nginx /usr/local/nginx/sbin/nginx;nginx


//升级成功
[root@kiwi123 objs]# ss -antl
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             [::]:*             
[root@kiwi123 objs]# nginx -v
nginx version: nginx/1.24.0

2. http{}段的配置参数location区段

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

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

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

常用修饰符说明:

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


//则有四种都可以匹配
[root@kiwi123 ~]# curl http://192.168.234.123/qeki
This is /qeki
[root@kiwi123 ~]# curl http://192.168.234.123/qeki?1231
This is /qeki
[root@kiwi123 ~]# curl http://192.168.234.123/qeki/
This is /qeki
[root@kiwi123 ~]# curl http://192.168.234.123/qekigfhh
This is /qeki
2.2 “=”:表示必须与指定的模式精确匹配
        location = /qeke {
        echo "This is qeke";
        }

//则有两种都可以被匹配
[root@kiwi123 ~]# curl http://192.168.234.123/qeke
This is qeke
[root@kiwi123 ~]# curl http://192.168.234.123/qeke?11ww
This is qeke

//有两种无法匹配
[root@kiwi123 ~]# curl http://192.168.234.123/qeke/

[root@kiwi123 ~]# curl http://192.168.234.123/qekeqweqw
2.3 “~”:表示指定的正则表达式必须区分大小写
        location ~ ^/daxie$ {
        echo "This is daxie";
        }
        
//则以下两种可以匹配
[root@kiwi123 ~]# curl 192.168.234.123/daxie?123
This is daxie
[root@kiwi123 ~]# curl 192.168.234.123/daxie?123
This is daxie

//以下三种无法匹配
[root@kiwi123 ~]# curl 192.168.234.123/Daxie
[root@kiwi123 ~]# curl 192.168.234.123/daxieqwewq
[root@kiwi123 ~]# curl 192.168.234.123/daxie/
2.4 “~*”:表示指定的正则表达式不区分大小写
        location ~* ^/daxiao$ {
        echo "This is daxiao";
        }
        
//则以下四种可以匹配
[root@kiwi123 ~]# curl http://192.168.234.123/daxiao
This is daxiao
[root@kiwi123 ~]# curl http://192.168.234.123/DaXiao
This is daxiao
[root@kiwi123 ~]# curl http://192.168.234.123/daxiao?ewqwe
This is daxiao
[root@kiwi123 ~]# curl http://192.168.234.123/DaXIAO?ewqwe
This is daxiao

//以下两种无法匹配
[root@kiwi123 ~]# curl http://192.168.234.123/daxiao/

[root@kiwi123 ~]# curl http://192.168.234.123/daxiaoqew
2.5 “^~”:表示精确匹配并当匹配成功之后则停止匹配其它匹配项
        location ^~ /kiwi/{
        echo "^~kiwi";
        }
        
//则以下两种可以匹配
[root@kiwi123 ~]# curl http://192.168.234.123/kiwi/
^~kiwi
[root@kiwi123 ~]# curl http://192.168.234.123/kiwi/wewq.12312
^~kiwi

//以下三种无法匹配
[root@kiwi123 ~]# curl http://192.168.234.123/kiwi
[root@kiwi123 ~]# curl http://192.168.234.123/kiwiqwe
[root@kiwi123 ~]# curl http://192.168.234.123/kiwi?qweq

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

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

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

优先级次序如下:

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

3. http{}段的配置参数rewrite

也就是URL重定向

语法:rewrite regex replacement flag;,如:

rewrite ^/picture/(.*\.jpg)$ /imgs/$1 break;
//原本的路径可以访问
[root@kiwi123 ~]# curl 192.168.234.123/kiwi/index.html
This is kiwi

//现在将kiwi目录进行重命名
[root@kiwi123 html]# ls
50x.html  index.html  index.php  kiwi
[root@kiwi123 html]# mv kiwi/ kiwi123

//则用原来的路径无法访问
[root@kiwi123 ~]# curl 192.168.234.123/kiwi/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@kiwi123 ~]# 

//做重定向
[root@kiwi123 html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        location /kiwi {
            rewrite ^/kiwi/(.*)$ /kiwi123/$1 break;
        }
        
        
//重新加载配置文件
[root@kiwi123 html]# nginx -s reload


//原来路径可以访问
[root@kiwi123 html]# ls
50x.html  index.html  index.php  kiwi123
[root@kiwi123 ~]# curl 192.168.234.123/kiwi/index.html
This is kiwi

重定向的位置不仅目录,URL也可以

        location /kiwi {
            rewrite ^/kiwi/(.*)$ https://www.nginx.org/ 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

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

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
|匹配 | 之前或之后的实体
()分组,组成一组用于匹配的实体,通常会有 | 来协助

捕获子表达式,可以捕获放在()之间的任何文本,比如:

^(hello|sir)$       //字符串为“hi sir”捕获的结果:$1=hi$2=sir

//这些被捕获的数据,在后面就可以当变量一样使用了

6.15 if

语法:if (condition) {...}

应用场景:

  • server段
  • location段

常见的condition

  • 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
  • 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
  • 正则表达式的模式匹配操作
    • ~:区分大小写的模式匹配检查
    • ~*:不区分大小写的模式匹配检查
    • !和!*:对上面两种测试取反
  • 测试指定路径为文件的可能性(-f,!-f)
  • 测试指定路径为目录的可能性(-d,!-d)
  • 测试文件的存在性(-e,!-e)
  • 检查文件是否有执行权限(-x,!-x)
6.15.1 基于浏览器实现分离案例
if ($http_user_agent ~ Firefox) {
  rewrite ^(.*)$ /firefox/$1 break;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
}

if ($http_user_agent ~ Chrome) {
  rewrite ^(.*)$ /chrome/$1 break;
}
6.15.2 防盗链案例
location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.kiwi.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.kiwi.com/403.html;
  }
}
reak;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
}

if ($http_user_agent ~ Chrome) {
  rewrite ^(.*)$ /chrome/$1 break;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值