rewrite
rewrite 作用是地址重定向,语法:rewrite regex replacement[flag];
根据 regex(正则表达式)匹配请求地址,然后跳转到 replacement,结尾是flag标记
如下例子,请求地址是 http://192.168.25.131:9003/baidu/开头的,都会跳转到百度
location /baidu/ {
rewrite ^/(.*) http://www.baidu.com/ permanent;
}
nginx rewrite参数 以及 $1、$2参数解析(附有生产配置实例)
1、先看一个nginx配置
rewrite ^/(user_\d)/(\d).html$ https://$host/?$1 permanent;
上面是我写的重写规则,先说$代表的是参数,所以一定是 ()
包含的
-
()
: 用于匹配括号之间的内容,通过$1
、$2
调用 -
$1
就是user_\d
-
$2
就是\d
举个例子
https://maven.jettech.com/user_1/2.html
这里$1
就是 user_1
$2
就是2
这实际上就是正则在nginx里面的运用, 下面看一下 正则 的规则,会帮助你理解
2、正则
2.1、replace
stringObject.replace(regexp/substr, replacement)
replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement
中的 $
字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。
字符 | 替换文本 |
---|---|
$1、$2、…、$99 | 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。 |
$& | 与 regexp 相匹配的子串。 |
$` | 位于匹配子串左侧的文本。 |
$’ | 位于匹配子串右侧的文本。 |
$$ | 直接量符号。 |
2.2、正则匹配重写例子
- 参数匹配,互换位置
name = "Tom, Jerry";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
'Jerry Tom'
- 参数获取
var str = '/pgk/api/pgk-abc'
var reg = /^\/pgk(.*)$/str.replace(reg, "$1") // '/api/pgk-abc'var str = '/pgk/api/pgk-abc/n/abc'
var reg = /^\/pgk\/api\/pgk-abc\/(.*)$/str.replace(reg, "$1") // 'n/abc'
3、生产配置示例
下面是工作中的实际配置, 重写系统简称 ABC
location /wubo/api/abc/ {rewrite ^/wubo/(.*)$ /$1 break;proxy_pass http://ip:port;# 这里重写掉 wubo,proxy_http_version 1.1;
}
$1 就是 api/abc/
实际请求地址是:http://ip:port/api/abc/
rewrite 最后一项flag参数:
标记符号 | 描述 |
---|---|
last | 本条规则匹配完成后继续向下匹配新的location URI规则 |
break | 本条规则匹配完成后终止,不在匹配任何规则 |
redirect | 返回302临时重定向 |
permanent | 返回301永久重定向 |
准备工作
在 /home/cookcyq/web/ 目录下新建 c 目录结构如下home
cookcyq
web
c
c.html 内容为:Hello, c.html.
r
1.html 内容为:Hello, 1.html.
2.html 内容为:Hello, 2.html.
3.html 内容为:Hello, 3.html.
5.2 使用
语法:rewrite [匹配模式] [重定向目标] [指令(可选)]
5.2.1 简单使用
location /c {rewrite ^(.*)$ /r/1.html;
}
当访问
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
都会重定向访问 root/r/1.html 在server中定义的总root路径,当然也可以在localtion中重新定义root
带有 last 指令
location /c {rewrite ^(.*)$ /r/1.html last;# 不会触发这个,因为 last 会跳过下面的执行语句rewrite ^(.*)$ /r/2.html last;
}
当访问
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
都会重定向访问 root/r/1.html
带有 last 指令
location /c {rewrite ^(.*)$ /r/1.html last;# 不会触发这个,因为 last 会跳过下面的执行语句,但它会继续走进下一个 location 块rewrite ^(.*)$ /r/2.html last;
}
# 这里监听 /r/1.html 再做一次重定向
location = /r/1.html {rewrite ^(.*)$ /r/3.html last;
}
当访问
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
都将重定向访问 root/r/3.html
5.2.4 带有 break 指令
location /c {rewrite ^(.*)$ /r/1.html break;# 不会触发这个,因为 break 会跳过下面的执行语句rewrite ^(.*)$ /r/2.html last;
}
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
都会重定向访问 root/r/1.html
5.2.5 带有 break 指令
location /c {rewrite ^(.*)$ /r/1.html break;# 不会触发这个,因为 break 会跳过下面的执行语句,同时也会跳过下面的 location 块rewrite ^(.*)$ /r/2.html last; }# 由于 break 这里不会触发到location = /r/1.html {rewrite ^(.*)$ /r/3.html last;}
http://127.0.0.1:8000/c
http://127.0.0.1:8000/c/c.html
http://127.0.0.1:8000/c/xxx/xxx/xxx
都会重定向访问 root/r/1.html
5.2.6 容易犯错的重定向案例
location /c {rewrite ^(.*)$ /r/1.html last;
}
location = /r/1.html {index /r/2.html;
}
思考一下,最终会重定向到哪里?
其实如果你知道 index 的作用,答案就简单多了,这里只会重定向到 root/r/1.html 而不会访问 root/r/2.html,因为 index 只是未访问任何文件时作为默认访问索引文件的指令。
5.3 注意事项
- rewrite 重定向目标如果不存在则走进 error_page 404 /40x.html
- 如果 root 是 /home/cookcyq/web 没有加
/
,那么重定向目标时得加/
,反之 root 有加/
则重定向目标可加可不加/
6.2 root
root /home/cookcyq/web/;
location /d {root /home/cookcyq/my/;try_files $uri $uri/ /d/index.html;
当我们访问 http://127.0.0.1:8000/d/foo.html 就会访问 /home/cookcyq/my/d/foo.html
6.3 alias
root /home/cookcyq/web/;
location /d {alias /home/cookcyq/my/d/;try_files $uri $uri/ /d/index.html;
}
当我们访问 http://127.0.0.1:8000/d/foo.html 就会访问 /home/cookcyq/my/d/foo.html
6.4 root 与 alias 的区别
root 指令最终形成的路径为:root+location xxx/*
alias 指令最终形成的路径为:alias/*
Nginx:proxy_pass、root、rewrite、alias 的使用详解_proxy_pass root-CSDN博客
Nginx & 详细举例 location -> index、return、rewrite、try_files、alias 各个属性的含义和注意事项_nginx location index_cookcyq的博客-CSDN博客
详解nginx的rewrite应用,Nginx高级之Rewrite规则 - 知乎