赞
踩
Rewrite又称URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程。
URL Rewrite最常见的应用是URL的伪静态化,是将动态界面显示为静态页面的一种技术。比如http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123.com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。
• 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
• 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。例如当用户访问tianyun.com的 80端口时,将其跳转到443端口。
Nginx Rewrite 相关指令有 if、rewrite、set、return
应用环境
server,location
语法:
if (condition) { … }
if 可以支持如下条件判断匹配符号
~ 正则匹配 (区分大小写)
~* 正则匹配 (不区分大小写)
!~ 正则不匹配 (区分大小写)
!~* 正则不匹配 (不区分大小写)
-f 和!-f 用来判断是否存在文件
-d 和!-d 用来判断是否存在目录
-e 和!-e 用来判断是否存在文件或目录
-x 和!-x 用来判断文件是否可执行
在匹配过程中可以引用一些Nginx的全局变量
$args 请求中的参数;
$document_root 针对当前请求的根路径设置值;
$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$limit_rate 对连接速率的限制;
$request_method 请求的方法,比如"GET"、"POST"等;
$remote_addr 客户端地址;
$remote_port 客户端端口号;
$remote_user 客户端用户名,认证用;
$request_filename 当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images /a.jpg)
$request_uri 当前请求的文件路径名(不带网站的主目录/images/a.jpg)
q u e r y s t r i n g 与 query_string 与 querystring与args相同;
$scheme 用的协议,比如http或者是https
$server_protocol 请求的协议版本,“HTTP/1.0"或"HTTP/1.1”;
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name 请求到达的服务器名;
d o c u m e n t u r i 与 document_uri 与 documenturi与uri一样,URI地址;
$server_port 请求到达的服务器端口号;
举例:
匹配访问的url地址是否是个目录
if (-d $request_filename) {
…;
}
匹配访问的地址是否以www开头
if ( $host ~* ^www) {
…;
}
rewrite 指令根据表达式来重定向URI,或者修改字符串。可以应用于server,location, if环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有:
last 相当于Apache里的[L]标记,表示完成rewrite。默认为last。
break 本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址会显示跳转后URL地址
实现访问 http://www.zhangyu.com/a/1.html 后跳转至http://www.zhangyu.com/b/2.html
1、准备工作:配置本地hosts域名解析,创建发布目录
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
实现访问 http://www.zhangyu.com/2021/a/1.html 时跳转至http://www.zhangyu.com/2022/a/1.html
1、准备工作:配置本地hosts域名解析,创建对应的网站发布目录
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
实现访问http://www.zhangyucom/a/1.html 跳转至 http://jd.com
1、准备工作:本地域名解析、默认发布目录
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
实现访问http://www.zhangyu.com/a/1.html 时跳转至 http://jd.com/a/1.html
1、准备工作:本地域名解析、默认发布目录
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
在访问目录后添加/ (如果目录后已有/,则不加/)
实现访问 http://www.tianyun.com/a/b/c时跳转至http://$host$1$2/
$1:/a/b
$2:c
1、准备工作
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
实现访问http://www.zhangyu.com/login/zy.html时跳转至http://www.zhangyu.com/reg/login.html?user=zy
1、准备工作
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
实现访问http://www.zhangyu.com/qf/11-22-33/1.html 时跳转至http://www.zhangyu.com/qf/11/22/33/1.html
1、准备工作
2、编写配置文件
[root@localhost conf.d]# vim rewrite.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
set指令用于定义一个变量,并且赋值
应用环境:
server,location,if
应用示例:
实现访问http://alice.zhangyu.com/ 时跳转至http://www.zhangyu.com/alice;访问http://jack.zhangyu.com/ 时跳转至http://www.zhangyu.com/jack;
1、准备工作、本地hosts文件域名解析,创建发布目录
2、编写配置文件
[root@localhost conf.d]# vim set.conf
3、重新加载配置文件
[root@localhost conf.d]# nginx -s reload
4、验证
return 指令用于返回状态码给客户端
应用环境
server,location,if
应用示例:
实现当访问.sh结尾的文件则返回403操作拒绝错误
1、准备工作
2、编译配置文件
[root@localhost conf.d]# vim return.conf
3、加载环境变量
[root@localhost conf.d]# nginx -s reload
4、验证
实现当访问80端口时跳转至443端口,即http转https
1、编译配置文件
[root@localhost conf.d]# vim return.conf
2、加载环境变量
[root@localhost conf.d]# nginx -s reload
3、验证
1、准备工作
2、编译配置文件
[root@localhost conf.d]# vim return.conf
3、加载环境变量
[root@localhost conf.d]# nginx -s reload
4、验证
1、准备工作
2、编译配置文件
[root@localhost conf.d]# vim return.conf
3、加载环境变量
[root@localhost conf.d]# nginx -s reload
4、验证
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。