Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服务器的地址从新定向到其余URL的过程。html
#rewrite表达式能够应用在server,location, if标签下 Syntax: rewrite regex replacement [flag]; Default: -- Context: server, location, if #用于切换维护页面场景 #rewrite ^(.*)$ /page/wh.html break;
rewrite指令根据表达式来重定向URI,或者修改URI字符串。
每行rewrite指令最后跟一个flag标记,支持的flag标记有以下表格所示:nginx
flag | |
---|---|
last | 本条规则匹配完成后,中止匹配,不在匹配后面的规则 |
break | 本条规则匹配完成后,中止匹配,不在匹配后面的规则 |
redirect | 返回302临时重定向, 地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向, 地址栏会显示跳转后的地址 |
[root@bgx conf.d]# cat rewrite.conf server { listen 80; server_name rewrite.oldboy.com; root /code; location ~ ^/break { rewrite ^/break /test/ break; } location ~ ^/last { rewrite ^/last /test/ last; } location /test/ { return 200 'ok'; } } #重启Nginx服务 [root@bgx conf.d]# systemctl restart nginx
break匹配到规则,则会去本地路径中目录中寻找对应请求的文件。
last匹配到规则,会对其所在的server{...}标签从新发起请求。
因此,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在了,理论上都应该返回404,可是实际请求/last的时候,是会有后面localtion所匹配到的结果返回的,若是last匹配不到location的结果则在返回错误。web
[root@Nginx ~]# cat /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name rewrite.oldboy.com; root /code; location ~ ^/bgx { rewrite ^(.*)$ https://www.xuliangwei.com redirect; rewrite ^(.*)$ https://www.xuliangwei.com permanent; #return 301 http://kt.xuliangwei.com; #return 302 http://kt.xuliangwei.com; } }
http://www.bgx.com/abc/1.html ==> http://www.bgx.com/ccc/bbb/2.html浏览器
#1.准备真实的访问路径 [root@web03 ~]# mkdir /code/ccc/bbb -p [root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html #2.Nginx跳转配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; location / { root /code; index index.html; } location /abc { rewrite (.*) /ccc/bbb/2.html redirect; #return 302 /ccc/bbb/2.html; } } #3.重启Nginx服务 [root@web03 ~]# systemctl restart nginx
http://www.bgx.com/2018/ccc/bbb/2.html ==> http://www.bgx.com/2014/ccc/bbb/2.html安全
#1.准备真实的访问路径 [root@web03 ~]# mkdir /code/2014/ccc/bbb -p [root@web03 ~]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html #2.Nginx跳转配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; location / { root /code; index index.html; } location /2018 { rewrite ^/2018/(.*)$ /2014/$1 redirect; } } #3.重启Nginx服务 [root@web03 ~]# systemctl restart nginx
location /test { rewrite (.*) http://www.xuliangwei.com redirect; }
http://www.bgx.com/course-11-22-33.html ==> http://www.bgx.com/course/11/22/33/course_33.html服务器
#1.准备真实的访问路径 [root@web03 ~]# mkdir /code/course/11/22/33/ -p [root@web03 ~]# echo "Curl docs.etiantian.org" > /code/course/11/22/33/course_33.html #2.Nginx跳转配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; root /code; index index.html; location / { #灵活 rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect; #固定 #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect; } #3.重启Nginx服务 [root@web03 ~]# systemctl restart nginx
server { listen 80; server_name bgx.com; rewrite ^(.*) https://$server_name$1 redirect; #return 302 https://$server_name$request_uri; } server { listen 443; server_name bgx.com; ssl on; }
$server_name 当前用户请求的域名
$request_filename 当前请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径名(不带网站的主目录/images/test.jpg)
$scheme用的协议,好比http或者https测试
server { listen 80; server_name www.oldboyedu.com oldboyedu.com; if ($http_host = oldboyedu.com){ rewrite (.*) http://www.nginx.org$1; } } #推荐的书写格式 server { listen 80; server_name oldboyedu.com; rewrite ^ http://www.oldboyedu.com$request_uri; } server { listen 80; server_name www.oldboyedu.com; }