好的,阿杰,我给你整理一份 Nginx URL 重写(rewrite)配置及详解,包括概念、常用指令、配置示例和注意事项,方便你理解和应用。
🌀 Nginx URL 重写(rewrite)详解
1. 什么是 URL 重写?
URL 重写是将请求的 URL 按规则进行 修改、重定向或内部跳转。
在 Nginx 中,rewrite
指令常用于:
- 将动态 URL 转换成静态 URL(美化 URL)
- 实现网站跳转(301/302)
- URL 路径重定向
- 分流不同请求到不同处理逻辑
2. rewrite 指令语法
rewrite regex replacement [flag];
- regex:正则匹配客户端请求的 URI
- replacement:替换后的 URI 或 URL
- flag(可选):rewrite 的行为
last
:停止当前rewrite
执行,重新匹配新的 URI(类似内部跳转)break
:停止匹配,使用当前 replacement,不再向下匹配 locationredirect
:返回 302 临时重定向permanent
:返回 301 永久重定向
3. rewrite 常用示例
3.1 动态 URL 美化
将 /article.php?id=123
转成 /article/123.html
:
location /article/ {
rewrite ^/article/([0-9]+)\.html$ /article.php?id=$1 last;
}
解释:
^/article/([0-9]+)\.html$
→ 匹配 article 后面数字 +.html
/article.php?id=$1
→ 将数字作为参数传给 PHPlast
→ 内部跳转,继续匹配 location
3.2 301 永久重定向
将 /old-page.html
永久跳转到 /new-page.html
:
rewrite ^/old-page\.html$ /new-page.html permanent;
浏览器访问 /old-page.html
会收到 301 状态码,并跳转到新页面。
3.3 302 临时重定向
rewrite ^/temp/(.*)$ /new/$1 redirect;
- 302 临时重定向,浏览器不会缓存跳转结果。
3.4 带条件的 rewrite
使用 if
判断请求条件:
if ($host = example.com) {
rewrite ^/(.*)$ https://www.example.com/$1 permanent;
}
- 将不带
www
的请求跳转到www.example.com
- 注意:
if
嵌套rewrite
时要谨慎,否则可能引发 Nginx 配置问题
4. rewrite 与 location 区别
特性 | rewrite | location |
---|---|---|
功能 | URL 重写/重定向 | URI 匹配请求,指定处理逻辑 |
应用 | 修改请求 URI,跳转页面 | 匹配请求路径,调用后端处理 |
配合 | 可和 location 配合 | 常用 rewrite 内部跳转 |
示例 | /a.html → /b.php | location /images/ { ... } |
✅ 实际使用中,一般 先匹配 location,再在 location 内用 rewrite 实现 URL 美化或跳转。
5. rewrite 注意事项
- 正则顺序
- Nginx 匹配 location 时,先精确匹配 (
=
) → 前缀匹配 (^~
) → 正则匹配 (~
/~*
) - rewrite 应写在最合适的 location 中,避免误匹配
- Nginx 匹配 location 时,先精确匹配 (
- 避免无限循环
rewrite ^/a/(.*)$ /b/$1 last; rewrite ^/b/(.*)$ /a/$1 last; # 会无限循环
- if 配合 rewrite
- 尽量避免复杂
if
,可用map
或try_files
替代 if
只能在 location 内使用
- 尽量避免复杂
- 性能优化
- 对于静态 URL 建议用
try_files
,比 rewrite 性能更高:
location / { try_files $uri $uri/ /index.php?$query_string; }
- 对于静态 URL 建议用
6. 总结
rewrite
是 URL 重写和跳转的核心指令flag
决定跳转行为(last / break / redirect / permanent)- 与
location
搭配使用,适合 动态 URL 美化、重定向和分流 - 注意 正则顺序、避免无限循环,必要时用
try_files
替代
好的,阿杰,我给你画一张 Nginx rewrite 流程图,清晰展示请求从客户端到后端的处理逻辑,包括 location
匹配和 rewrite
流程。
🖼️ Nginx rewrite 流程图说明
客户端请求
|
v
+--------------------+
| Nginx 入口(请求URI) |
+--------------------+
|
v
+--------------------+
| 精确匹配 location (=) |
+--------------------+
|否
v
+--------------------+
| 前缀匹配 location (^~) |
+--------------------+
|否
v
+--------------------+
| 正则匹配 location (~ / ~*) |
+--------------------+
|
v
+--------------------+
| location 内 rewrite 检查 |
| - last / break / redirect / permanent |
+--------------------+
|
v
+----------------------------+
| 是否内部跳转或外部重定向? |
+----------------------------+
| 内部跳转 (last/break)
v
+--------------------+
| 重新匹配 location |
+--------------------+
|
v
+--------------------+
| 调用后端处理 / 返回静态资源 |
+--------------------+
|
v
客户端收到响应
🔑 流程重点解释
- Location 匹配优先级
=
精确匹配优先^~
前缀匹配优先于正则~
/~*
正则匹配最后匹配
- rewrite flag 作用
- last → 内部跳转,重新匹配 location
- break → 停止 rewrite,不再匹配 location
- redirect → 302 临时重定向
- permanent → 301 永久重定向
- 内部跳转 vs 外部重定向
- 内部跳转不通知客户端,Nginx 内部处理 URI
- 外部重定向通知客户端,浏览器地址栏改变
发表回复