什么是rewrite
1 |
Rewrite主要实现url地址重写,以及重定向,就是把传入`web`的请求重定向到其他`url`的过程。 |
Rewrite使用场景
1 2 3 4 |
1.地址跳转,用户访问www.baidu.com这个URL时,将其定向至一个新的域名mobile.baidu.com 2.协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式 3.伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。 4.搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入 |
rewrite语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if rewrite #调用模块 regex #请求的链接(可以使用正则表达式) replacement #跳转的链接 [flag]; #标签 #示例 server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; ... } |
rewrite匹配优先级
1 2 3 4 |
1.先执行server层的rewrite 2.再根据location匹配优先级匹配 3.再执行location下的rewrite 4.最后再执行location下if配置的rewrite |
rewrite的flag标记
1 |
rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示: |
flag | 作用 |
---|---|
last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
last和break的区别
1)配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf server { listen 80; server_name linux.rewrite.com; root /code; location ~ ^/break { rewrite ^/break /test/ break; } location ~ ^/last { rewrite ^/last /test/ last; } location /test/ { default_type application/json; return 200 "ok"; } } |
2)重启
1 2 |
[root@web01 ~]# nginx -t [root@web01 ~]# systemctl restart nginx |
3)配置hosts测试
1 2 3 4 5 |
10.0.0.7 linux.rewrite.com #结果 1.访问 linux.rewrite.com/break,返回结果404 2.访问 linux.rewrite.com/last,返回结果ok |
4)结论
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
break只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件; 而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。 break请求: 1.请求 linux.rewrite.com/break 2.匹配 location ~ ^/break 会跳转请求 到 linux.rewrite.com/test 3.请求跳转后,会去查找本地的站点目录下的 test/index.html; 4.如果找到了,则返回站点目录下的 test/index.html的内容; 5.如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403 last请求: 1.请求 rewrite.drz.com/last 2.匹配 location ~ ^/last 会跳转请求 到 linux.rewrite.com/test 2.请求跳转后,会去查找本地的站点目录下的 test/index.html; 3.如果找到了,则返回站点目录下的 test/index.html的内容; 4.如果没找到,会对当前server重新的发起一次请求,linux.rewrite.com/test/ 5.如果有location匹配上,则直接返回该location的内容。 4.如果也没有location匹配,再返回404; |
redirect和permanent的区别
1)配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rw.com.conf server { listen 80; server_name linux.rw.com; root /code; location /test { #rewrite ^(.*)$ https://www.baidu.com redirect; #rewrite ^(.*)$ https://www.baidu.com permanent; #return 301 https://www.baidu.com; return 302 https://www.baidu.com; } } |
2)访问测试
1 2 3 4 5 6 7 8 |
#配置redirect时 关闭nginx之后访问失败 #配置permanent时 关闭nginx仍然访问成功 #结论: redirect,每次访问服务器都会进行询问,是否进行跳转 permanent,记录一次跳转,以后都不会询问,直接跳转页面,除非清空缓存 |
案例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
1)创建页面
1 2 |
[root@web01 ~]# mkdir /code/ccc/bbb/ -p [root@web01 ~]# echo "/ccc/bbb/2.html" > /code/ccc/bbb/2.html |
2)配置nginx
1 2 3 4 5 6 7 8 9 10 |
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf server { listen 80; server_name nginx.rewrite.com; root /code; location ~* /abc { rewrite ^(.*)$ /ccc/bbb/2.html redirect; } } |
案例二:用户访问/2018/ccc/bbb/2.html
实际上真实访问的是/2014/bbb/ccc/2.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@web01 ~]# mkdir /code/2014/bbb/ccc/ -p [root@web01 ~]# echo "/2014/bbb/ccc/2.html" > /code/2014/bbb/ccc/2.html #配置nginx [root@web01 ~]# vim /etc/nginx/conf.d/rw.conf server { listen 80; server_name nginx.rewrite.com; root /code; location ~* ^/2018 { rewrite ^/2018/(.*)/(.*)/(.*) /2014/$2/$1/$3 redirect; } } |
案例三:用户访问course-11-22-33.html
实际上真实访问的是/course/11/22/33/course_33.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@web01 ~]# mkdir /code/course/11/22/33/ -p [root@web01 ~]# echo "/course/11/22/33/course_33.html" >/code/course/11/22/33/course_33.html #配置 [root@web01 ~]# vim /etc/nginx/conf.d/rw.conf server { listen 80; server_name nginx.rewrite.com; root /code; location ~* ^/course { #灵活配法 rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect; #固定配法 #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect; } } |
案例四:将http
请求跳转到https
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Nginx跳转配置 server { listen 80; server_name www.baidu.com; rewrite ^(.*) https://$server_name$1 redirect; #return 302 https://$server_name$request_uri; } server { listen 443; server_name www.baidu.com; ssl on; } 浏览器输入:baidu.com 浏览器转换:http://www.baidu.com/index.html server层转换,rewrite跳转:https://www.baidu.com/index.html |
rewrite的环境变量
1)$server_name
1 2 3 4 5 6 7 8 9 10 |
$server_name #当前请求的域名 server { listen 80; server_name test.linux.com; rewrite ^(.*)$ https://$server_name$1; } http://test.linux.com/1.html https://test.linux.com/1.html |
2)请求文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$request_filename #请求的文件路径名(带网站的站点目录 /code/images/test.jpg) $request_uri #当前请求的文件路径(不带网站的站点目录 /images/test.jpg) #大多数用于http协议转https协议 server { listen 80; server_name test.linux.com; root /code; return 302 https://$server_name$request_uri; } URL: http://test.linux.com/images/test.jpg URI: /images/test.jpg |
3)$scheme
1 |
$scheme 用的协议,比如http或者https |
4)$http_host
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#很久之前的配置方法 server { listen 80; server_name www.baidu.com baidu.com; if ($http_host = baidu.com){ rewrite (.*) http://www.baidu.com$1; } } #推荐书写格式 server { listen 80; server_name baidu.com; return 302 http://www.baidu.com$request_uri; } server { listen 80; server_name www.drz.com; } |
rewrite开启日志
1 2 3 4 5 6 7 8 9 10 |
#配置 [root@web01 /code]# vim /etc/nginx/nginx.conf ... ... error_log /var/log/nginx/error.log notice; ... ... http { ... ... rewrite_log on; ... ... } |