Nginx Rewrite
Rewirte规则也称为规则重写,主要功能是实现浏览器访问HTTP URL的跳转(当访问不存在的路径时 -- 跳转到首页
通常而言,几乎所有的WEB服务器均可以支持URL重写。Rewrite URL规则重写的用途:
- 对搜索引擎优化(Search Engine Optimization,SEO)友好,利于搜索引擎抓取网站页面;(百度|360|搜狗|google --> 对于静态html 的 收录 远比 动态收录要强) --> www.baidu.com/index.html | index.php --> 网站排名 --> 伪静态
- 隐藏网站URL真实地址,浏览器显示更加美观;
- 网站变更升级(网页建设中),可以基于Rewrite临时重定向到其他页面。
a、百度搜“网站建设中”
b、爬下所有源码 wget -r -x
c、把源码里的域名改成自己的 - 放到默认网站目录下
d、实现跳转
Nginx Rewrite规则使用中有三个概念需要理解
分别是:Rewrite结尾标识符、Rewrite规则常用表达式、Nginx Rewrite变量,如下为三个概念的详解:
- Nginx Rewrite结尾标识符,用于Rewrite规则末尾,表示规则的执行属性。
last :相当于Apache里的(L)标记,表示完成rewrite匹配;
break:本条规则匹配完成后,终止匹配,不再匹配后面的规则。
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址。 ---> 安装Discuz | WordPress页面
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。
其中last和break用来实现URL重写时,浏览器地址栏URL地址不变。
临时跳转比301灵活
Nginx Rewrite规则常用表达式,主要用于匹配参数、字符串及过滤设置。
. 匹配任何单字符;
[word] 匹配字符串:word;
[^word] 不匹配字符串:word;
thinkmoedu|thinkmoteach 可选择的字符串:thinkmoedu|thinkmoteach;
? 匹配0到1个字符;
* 匹配0到多个字符;
+ 匹配1到多个字符;
^ 字符串开始标志;
$ 字符串结束标志;
\n 转义符标志。
Nginx Rewrite变量,常用于匹配HTTP请求头信息、浏览器主机名、URL等。
HTTP headers:HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT;
connection & request: REMOTE_ADDR, QUERY_STRING;
server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL;
system stuff: TIME_YEAR, TIME_MON, TIME_DAY。
详解如下:
HTTP_USER_AGENT 用户使用的代理,例如浏览器(IE | Chrome & 360 --> 都是谷歌内核) | 底层系统(苹果(iphoneX ipaid) 安卓等);
HTTP_REFERER 告知服务器,从哪个页面来访问的; --> 投付费广告(百度SEM - 论坛|贴吧) --> 落地到官网 --> 路径
HTTP_COOKIE 客户端缓存,主要用于存储用户名和密码等信息;
HTTP_HOST 匹配服务器ServerName域名;
HTTP_ACCEPT 客户端的浏览器支持的MIME类型;
REMOTE_ADDR 客户端的IP地址
QUERY_STRING URL中访问的字符串;
DOCUMENT_ROOT 服务器发布目录;
SERVER_PORT 服务器端口;
SERVER_PROTOCOL 服务器端协议;
TIME_YEAR 年;
TIME_MON 月;
TIME_DAY 日;
开启Nginx rewrite的错误日志,方便排错
日志要开,检查的
vim /etc/nginx/nginx.conf
#设置nginx的错误日志级别为notice
errot_log /var/log/nginx/error.log notice;
#在http层,添加一行rewrite_log日志
http {
.....
errot_log /var/log/nginx/error.log notice;
rewrite_log on;
.....
}
Rewrit的使用位置
写在server,location,if
Nginx Rewrite以下配置均配置在nginx.conf或者vhosts.conf中,企业中常用的Nginx Rewrite案例如下:
将hebbao.com跳转至www.hebbao.com。
if ($host = 'hebbao.com' ) {
rewrite ^/$ https://www.hebbao.com/ permanent;
}
if ($host = 'hebbao.com' ) {
rewrite ^/(.*)$ https://www.hebbao.com/$1 permanent;
}
用户访问/abc/1.html实际上真实访问是/ccc/bbb/2.html
#当我们网站访问的是/abc/1.html这样的信息替换成后面的/ccc/bbb/2.html
location ~* /abc/1.html {
rewrite .* /ccc/bbb/2.html;
}
#2.另一种方式,这种直接是跳转
location ~* /abc/1.html {
return 302 /ccc/bbb/2.html;
}
只要是2018目录下的任意内容就跳转到2014目录下对应的内容
location /2018 {
rewrite ^/2018(.*)$ /2014/$1 redirect;
}
用户访问course-11-22-33.html实际上真实访问时/course/11/22/33/course_33.html
location / {
rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
}
用户访问/test目录下任何内容,实际上真实访问是http://baidu.com
location /test {
rewrite (.*)$ http://www.baidu.com redirect;
}
访问www.hebbao.com跳转www.hbbao.com/index.html。
rewrite ^/>nbsp; http://www.test.com/index01.html permanent;
访问/thinkmoedu/test01/跳转至/newindex.html,浏览器地址不变
这个你必须 在游览器中地址输入这个目录才能看到效果
if ($host = 'hebbao.com') { rewrite ^/tinkmoedu/test01$ https://www.hebbao.com/newindex.html redirect; }
多域名跳转到www.hebbao.com。
这种写法就是,你访问www.hebbao.com不管在地址栏里这个地址后面输入什么,都会给你跳转到首页
if ($host = 'www.hebbao.com' ) {
rewrite ^/(.*)$ https://www.hebbao.com$1 redirect;
}
访问文件和目录不存在跳转至index.php。
if ( !-e $request_filename )
{
rewrite ^/(.*)$ http:www.hebbao.com/index.php last;
}
目录对换 /xxxx/123456 ====> /xxxx?id=123456。
效果就是,在游览器上比如www.hebbao.com/xxx/dd 实际就会显示hebbao.com/xxx?id=dd
要是输入www.hebbao.com/xxx 是看不到效果的
rewrite ^/(.+)/(\d+) /$1?id=$2 last;
判断浏览器User Agent跳转。
if( $http_user_agent ~ MSIE)
{
rewrite ^(.*)$ /ie/$1 break;
}
if ( $http_user_agent ~* "Chrome" ) { rewrite http://www.hebbao,com/403.html redirect; }
禁止访问以.sh,.flv,.mp3为文件后缀名的文件。
location ~ .* \.(sh|flv|mp3)$
{
return 403;
}
将移动用户访问跳转至移动端。
if ( $http_user_agent ~* "(Android)|(iPhone)|(Mobile)|(WAP)|(UCWEB)" )
{
rewrite ^/$ http://m.hebbao.com.com/ permanent;
}
匹配URL访问字符串跳转。
if ($args ~* tid=13){
return 404;
}
访问/10690/thinkmoedu/123跳转至/index.php?tid/10690/items=123,[0-9]表示任意一个数字,+表示多个,(.+)表示任何多个字符。
rewrite ^/([0-9]+)/thinkmoedu/(.+)$ /index.php?tid/$1/items=$2 last;
继续阅读

我的微信
这是我的微信扫一扫
评论