改写代 理请求
proxy-rewrite 插件允许你在客户端请求发送到上游服务之前修改该请求。它适用于修改 URI 路径、覆盖 HTTP 方法,或添加后端所需的强制请求头等任务。
前置条件
- API7 企业版实例正在运行。
- 已创建网关组,并且网关实例正在运行。
- 从控制台获取 Token。
改写静态 URI 路径
你可以在请求到达上游之前替换整个 URI 路径。例如,将 /rewrite/get 改写为 /get。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/proxy-rewrite-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "proxy-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/static-rewrite-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "static-rewrite-route",
"methods": ["GET"],
"paths": ["/rewrite/get"],
"service_id": "proxy-rewrite-service",
"plugins": {
"proxy-rewrite": {
"uri": "/get"
}
}
}'
adc.yaml
services:
- name: proxy-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: static-rewrite-route
uris:
- /rewrite/get
methods:
- GET
plugins:
proxy-rewrite:
uri: /get
labels:
docs-example: proxy-rewrite-service
预览本示例管理的资源变更,确认没有非预期的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite-service
审查变更后同步配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite-service
使用正则表达式改写
正则改写更灵活,可用于剥离前缀或转换路径的一部分。例如,剥离 /rewrite-regex 前缀,使上游收 到 /get。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/regex-rewrite-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "regex-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/regex-rewrite-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "regex-rewrite-route",
"methods": ["GET"],
"paths": ["/rewrite-regex/*"],
"service_id": "regex-rewrite-service",
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/rewrite-regex/(.*)", "/$1"]
}
}
}'
adc.yaml
services:
- name: regex-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: regex-rewrite-route
uris:
- /rewrite-regex/*
methods:
- GET
plugins:
proxy-rewrite:
regex_uri:
- "^/rewrite-regex/(.*)"
- "/$1"
labels:
docs-example: regex-rewrite-service
预览本示例管理的资源变更,确认没有非预期的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=regex-rewrite-service
审查变更后同步配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=regex-rewrite-service
regex_uri 字段接收一组 [pattern, replacement] 配对数组。数组中第一个匹配的模式会用于改写。
修改请求头
你可以在代理到上游之前添加、设置或移除请求头。这通常用于注入身份认证 Token,或移除敏感的客户端请求头。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/header-rewrite-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "header-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/header-rewrite-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "header-rewrite-route",
"methods": ["GET"],
"paths": ["/anything/headers-rewrite"],
"service_id": "header-rewrite-service",
"plugins": {
"proxy-rewrite": {
"headers": {
"set": {
"X-Internal-Token": "secret-value"
},
"add": {
"X-Added-Header": "example"
},
"remove": ["User-Agent"]
}
}
}
}'
adc.yaml
services:
- name: header-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: header-rewrite-route
uris:
- /anything/headers-rewrite
methods:
- GET
plugins:
proxy-rewrite:
headers:
set:
X-Internal-Token: "secret-value"
add:
X-Added-Header: "example"
remove:
- "User-Agent"
labels:
docs-example: header-rewrite-service
预览本示例管理的资源变更,确认没有非预期的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=header-rewrite-service
审查变更后同步配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=header-rewrite-service
请求头操作
set:将请求头设置为指定值,并覆盖已有值。add:向请求添加请求头,不覆盖已有值。remove:从请求中移除指定请求头。
覆盖 HTTP 方法
你可以为所有上游请求强制指定 HTTP 方法。例如,将 PUT 请求改为 POST 请求。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/method-rewrite-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "method-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/method-rewrite-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "method-rewrite-route",
"paths": ["/anything/method-rewrite"],
"service_id": "method-rewrite-service",
"plugins": {
"proxy-rewrite": {
"method": "POST"
}
}
}'
adc.yaml
services:
- name: method-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: method-rewrite-route
uris:
- /anything/method-rewrite
plugins:
proxy-rewrite:
method: POST
labels:
docs-example: method-rewrite-service
预览本示例管理的资源变更,确认没有非预期的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=method-rewrite-service
审查变更后同步配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=method-rewrite-service
验证配置
向已配置的路由发送请求,并观察响应或检查上游日志,以验证改写是否生效。
curl -i "http://127.0.0.1:9080/rewrite/get"
对于请求头改写示例,可以查看 httpbin 返回的上游请求头:
curl -s "http://127.0.0.1:9080/anything/headers-rewrite"
对于方法改写示例,发送 PUT 请求并确认上游看到的是 POST