Nginx中rewrite

Nginx中rewrite

rewrite

语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

此处的$1用于引用(.*.jpg)匹配到的内容,又如:

rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

如上例所示,replacement可以是某个路径,也可以是某个URL

常见的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

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

实例

#先在主页目录下创建一个image的目录。
[root@localhost html]# mkdir images
[root@localhost html]# ls
50x.html  images  index.html
[root@localhost html]# cd images/
[root@localhost images]# ls
[root@localhost images]# echo "hello" >images.html
[root@localhost images]# ls
images.html
[root@localhost images]# pwd
/usr/local/nginx/html/images

#进入配置文件配置rewrite重写规则,匹配images的目录
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location /images {
            rewrite ^/images/(.*)$ /images/images.html break;  
        }
#匹配根下的images目录然后,images目录下的任意字符,都转发到/images/images.html。所以此时我们不管怎么访问,只要是在/images/目录下随便输入什么字符都可以匹配到。
[root@localhost images]# systemctl restart nginx.service 
[root@localhost images]# curl 192.168.245.128/images/
hello
[root@localhost images]# curl 192.168.245.128/images/aaaz
hello 
[root@localhost images]# curl 192.168.245.128/images/aaaaaaaa
hello 
#如果这个时候访问的文件发生了改变,比如名字发生了改变如 
那么怎么样才能让那个访问方式继续访问到文件的内容。
[root@localhost images]# ls
images.html
[root@localhost images]# mv images.html imgs.html
[root@localhost images]# ls
imgs.html
[root@localhost images]# 
#修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location /images {
            rewrite ^/images/(.*)$ /images/imgs.html break;
        }
[root@localhost ~]# !systemctl
systemctl restart nginx.service
#匹配到的images/目录下的任意字符都转发给后面的/images/imgs.html  
#所以就算文件名字发生了改变照样能完成之前的效果,因为我们在后面转发的位置指向了文件。
[root@localhost ~]# curl 192.168.245.128/images/1
hello 
[root@localhost ~]# curl 192.168.245.128/images/12
hello 

if

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

应用场景:

  • server段
  • location段

常见的condition

  • 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
  • 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
  • 正则表达式的模式匹配操作
    • ~:区分大小写的模式匹配检查
    • ~*:不区分大小写的模式匹配检查
    • !和!*:对上面两种测试取反
  • 测试指定路径为文件的可能性(-f,!-f)
  • 测试指定路径为目录的可能性(-d,!-d)
  • 测试文件的存在性(-e,!-e)
  • 检查文件是否有执行权限(-x,!-x)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值