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)库,基本语法如下:
| 标识符 | 意义 |
|---|---|
| ^ | 必须以^后的实体开头 |
| $ | 必须以$前的实体结尾 |
| . | 匹配任意字符 |
| [] | 匹配指定字符集内的任意字符 |
| [^] | 匹配任何不包括在指定字符集内的任意字符串 |
| () | 分组,组成一组用于匹配的实体,通常会有 |
实例:
//在/usr/local/nginx/html/下创建image目录并上传张照片里面
[root@localhost html]# mkdir image
[root@localhost html]# ls
50x.html image index.html
[root@localhost html]# cd image
[root@localhost image]# ls
01.jpg
[root@localhost nginx]# vim conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location /image {
alias html/image;
}

改变url并写rewrite规则
//修改目录图片名字
[root@localhost html]# ls
50x.html image index.html
[root@localhost html]# mv image imgs
[root@localhost html]# ls
50x.html imgs index.html
[root@localhost html]# cd imgs
[root@localhost imgs]# ls
01.jpg
[root@localhost imgs]# mv 01.jpg 1.jpg
[root@localhost imgs]# ls
1.jpg
//写rewrite规则
location / {
root html;
index index.html index.htm;
}
location /image {
rewrite ^/image/(.*\.jpg)$ /imgs/$1 break;
**//表示访问images目录下任何一张图片都会重写到/imgs目录下images匹配到的图片**
}
location /imgs {
root html;
}

把资源放到/opt/目录下,不是默认网页目录下了
[root@localhost html]# ls
50x.html imgs index.html
[root@localhost html]# mv imgs /opt/
//重写规则
location / {
root html;
index index.html index.htm;
}
location /image {
root /opt;
rewrite ^/image/(.*\.jpg)$ /imgs/$1 break;
}
location /imgs { 可以不要
root html; 可以不要
}
仍可以访问

也可以重定向网上的资源
location / {
root html;
index index.html index.htm;
}
location /image {
root /opt; rewrite ^/image/(.*\.jpg)$ https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597295244074&di=7efbd882c49859921d7b73296f1ab780&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg break;
}

last 一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理,而是由UserAgent重新对重写后的URL再一次发起请求,并从头开
location /image {
root /opt;
rewrite ^/image/(.*\.jpg)$ /imgs/$1 last;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com break;
}
;
}

if
语法:if (condition) {…}
应用场景:
- server段
- location段
常见的condition
- 变量名(变量值为空串,或者以“0”开始,则为- - - false,其它的均为true)
- 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
- 正则表达式的模式匹配操作
- ~:区分大小写的模式匹配检查
- ~*:不区分大小写的模式匹配检查
- !和!*:对上面两种测试取反
- 测试指定路径为文件的可能性(-f,!-f)
- 测试指定路径为目录的可能性(-d,!-d)
- 测试文件的存在性(-e,!-e)
- 检查文件是否有执行权限(-x,!-x)
基于浏览器分离实例
//创建测试页面
[root@localhost html]# pwd
/usr/local/nginx/html
[root@localhost html]# mkdir chrome
[root@localhost html]# echo "hello" > chrome/index.html
[root@localhost html]# mkdir firefox
[root@localhost html]# echo "he he" > firefox/index.html
//编辑配置文件
location / {
root html;
index index.html index.htm;
if ($http_user_agent ~ Chrome){
rewrite ^(.*)$ /chrome/$1 break;
}
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
}
location /chrome {
root html;
index index.html;
}
location /firefox {
root html;
index index.html;
}
在谷歌浏览

在火狐浏览

本文介绍了nginx的rewrite规则,包括语法、常见flag及其作用。通过示例解释了如何利用rewrite规则进行URL重定向,以实现资源访问优化和防止恶意URL。同时,讲解了if条件语句的应用场景和各种条件表达式的用法,帮助读者更好地掌握nginx的URL管理策略。


被折叠的 条评论
为什么被折叠?



