设为首页 加入收藏

TOP

nginx配置文件,rewrite,if
2023-07-23 13:36:21 】 浏览:18
Tags:nginx 文件 rewrite

nginx配置文件,rewrite,if

rewrit

语法:rewrite regex replacement flag;

比如

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

又如

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)库,基本语法如下:

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

rewrit应用

#进入默认主页创建一个目录上传一张图片
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mkdir images
[root@localhost html]# cd images/
[root@localhost images]# ls
dog.jpg

浏览器访问

image

#修改images的名称
[root@localhost images]# cd ..
[root@localhost html]# mv images imgs
[root@localhost html]# ls
50x.html  imgs  index.html

#此时用以前的访问方式访问不到
[root@localhost html]# curl 192.168.118.129/images/dog.jpg
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>

#配置nginx.conf文件,让我们用以前的访问方式也可以访问到
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
        location / {
            root   html;
            index  index.html index.htm;
        }
#编写下面内容
        location /images {
            rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
        }
...
[root@localhost html]# systemctl restart nginx

此时再用以前的访问方式在浏览器上访问

image

重写URL

#此时只要访问/images/下的内容就会重写到添加的URL上
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
        location /images {
            rewrite ^/images/(.*\.jpg)$ https://c-ssl.duitang.com/uploads/item/201301/16/20130116230750_FBfRF.jpeg break;
        }
...
[root@localhost html]# systemctl restart nginx

再次访问

image

image

if

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

应用场景:

  • server段
  • location段

常见的condition

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

基于浏览器实现分离案例

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;
}

防盗链案例

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.idfsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.idfsoft.com/403.html;
  }
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux apache实现https的配置 下一篇华为云1核2G云服务器s3实例1M带宽..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目