39-Nginx-rewrite跳转
Rewrite基本概述
什么是rewrite?
rewrite主要实现rul地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。
rewirte的使用场景
1.地址跳转,用户访问www.lzy.com这个url时,将其定向到一个新的域名mobile.lzy.com
2.协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3.伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性
4.搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
Rewrite配置示例
Syntax:rewrite regex replacement [flag]
Default:-
Context:server,location,if
Rewrite标记flag
rewrite 指令根据表达式来重定向url,或者修改字符串,可以应用于server location if环境下,每行rewrite后跟一个flag标记,支持的flag标记有如下所示:
flag | 作用 |
---|
last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
Rewrite跳转标记
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code/test/;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@web01 conf.d]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
访问测试:
http://test.oldboy.com/1.html 返回b.html
http://test.oldboy.com/2.html 返回a.html
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code/test/;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
访问http://test.oldboy.com/1.html 返回a.html
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code/test/;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
访问:http://test.oldboy.com/1.html 返回2.html
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code;
location /test {
rewrite ^(.*)$ http://www.baidu.com redirect;
}
}
访问www.oldboy.com/test
临时跳转到www.baidu.com 。redirect 每次都要请求原站,然后跳转新的站点。permanent只请求一次源站。
Rewrite资源跳转
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
location / {
root /code;
index index.html;
}
location /abc {
rewrite (.*) /ccc/bbb/2.html redirect;
}
}
[root@web01 conf.d]
[root@web01 conf.d]
访问:test.oldboy.com/abcxxx
通过后向引用,调用用户输入的路径
root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
location / {
root /code;
index index.html;
}
location /2018 {
rewrite ^/2018/(.*)$ /2014/$1 redirect;
}
访问:
https://test.oldboy.com/2018/aaa/index.html
访问:http://test.oldboy.com/course/11/22/33/course_33.html
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code;
index index.html;
location / {
rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
}
}
错误页跳转
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
root /code;
index index.html;
location / {
rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
}
error_page 403 404 500 501 502 @error_test;
location @error_test {
rewrite ^(.*)$ /404.html break;
}
[root@web01 conf.d]
server {
listen 80;
server_name test.oldboy.com;
set $args "&showoffline=1";
location / {
root /code;
index index.html;
}
if ($remote_addr = 10.0.0.1 ){
rewrite (.*) http://test.oldboy.com$1;
}
}
server {
listen 80;
server_name test.oldboy.com;
root /code/test;
charset utf-8,gbk;
location / {
index index.html;
set $ip 0;
if ($remote_addr = "10.0.0.2"){
set $ip 1;
}
if ($ip = 0){
rewrite ^(.*)$ /wh.html break;
}
}
}