Proxy Rewrite
proxy-rewrite 插件提供了重写 APISIX 转发到上游服务请求的选项。使用该插件,你可以修改 HTTP 方法、请求目标上游地址、请求头等。
示例
以下示例展示了如何在不同场景下的路由上配置 proxy-rewrite。
配置重写前检查 URI 规范化
在 APISIX 3.19.0 中,应区分路由匹配使用的路径、原始请求目标以及发送给上游的路径:
| 值 | 路由插件重写请求前的含义 |
|---|---|
uri | 不含查询字符串的规范化路径。NGINX 已解码转义内容并解析路径段,APISIX 的规范化配置还可以进一步调整路径。 |
插件上下文中的 request_uri | 该规范化路径及查询字符串。 |
real_request_uri | 原始请求目标,包括其编码和查询字符串。 |
uri-blocker 插件在 rewrite 阶段检查插件上下文中的 request_uri。因此规则看到的是规范化路径及查询字符串。对于 /a%2Fb?x=%2F,默认的规范化路径为 /a/b,而查询字符串仍为 x=%2F。按字面匹配编码路径的规则无法等效地检查规范化后的资源。
proxy-rewrite 默认以 uri 为重写输入。路由匹配完成后,插件的 uri 和 regex_uri 设置决定转发内容,不会选择另一条路由。use_real_request_uri_unsafe 将重写输入改为原始请求目标,并跳过常规安全处理。不要仅为了让拦截表达式看到原始字节而启用该选项。
请同时核对匹配的路由、网关日志中的值和上游实际收到的路径。当测试需要保留客户端通常会移除的 路径段时,请使用 curl --path-as-is。在实际部署的版本上测试普通路径、编码路径、包含重复斜杠的路径和包含查询字符串的情况。URI 转发修复和路由器设置都可能改变结果。单条拒绝表达式无法构成完整的访问控制边界。
被拒绝的请求仍可能产生访问日志、错误或警告日志以及指标,也可能触发已配置的外部日志投递。拦截上游调用后,网关仍会产生处理请求和记录日志的开销。
重写 Host 请求头
以下示例演示了如何修改请求中的 Host 头。请注意,你不应使用 headers.set 来设置 Host 头。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/headers",
"plugins": {
"proxy-rewrite": {
"host": "myapisix.demo"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /headers
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
host: myapisix.demo
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
host: myapisix.demo
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /headers
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
host: myapisix.demo
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
向 /headers 发送请求以检查发送到上游的所有请求头:
curl "http://127.0.0.1:9080/headers"
你应该看到类似于以下的响应:
{
"headers": {
"Accept": "*/*",
"Host": "myapisix.demo",
"User-Agent": "curl/8.2.1",
"X-Amzn-Trace-Id": "Root=1-64fef198-29da0970383150175bd2d76d",
"X-Forwarded-Host": "127.0.0.1"
}
}
重写 URI 并设置请求头
以下示例重写上游 URI 并设置请求头。标量值会设置一行请求头,数组则按数组顺序设置多行同名请求头。此处 X-Api-Engine 仍使用标量,X-Api-Version 则用两个值替换客户端提供的任意值。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/",
"plugins": {
"proxy-rewrite": {
"uri": "/anything",
"headers": {
"set": {
"X-Api-Version": [
"v1",
"v2"
],
"X-Api-Engine": "apisix"
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbingo.org:80": 1
}
}
}'
services:
- name: httpbingo
routes:
- uris:
- /
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
uri: /anything
headers:
set:
X-Api-Version:
- v1
- v2
X-Api-Engine: apisix
upstream:
type: roundrobin
nodes:
- host: httpbingo.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbingo-external-domain
spec:
type: ExternalName
externalName: httpbingo.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
uri: /anything
headers:
set:
X-Api-Version:
- v1
- v2
X-Api-Engine: apisix
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbingo-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbingo-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbingo.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /
upstreams:
- name: httpbingo-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
uri: /anything
headers:
set:
X-Api-Version:
- v1
- v2
X-Api-Engine: apisix
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
发送请求进行验证:
curl "http://127.0.0.1:9080/" -H 'X-Api-Version: client'
你应该看到类似于以下的响应:
{
"headers": {
"X-Api-Engine": [
"apisix"
],
"X-Api-Version": [
"v1",
"v2"
]
}
}
插件会按配置顺序发送两行 X-Api-Version 请求头。由于 headers.set 会替换现有值,客户端传入的 client 值不会保留。标量配置 X-Api-Engine 仍只设置一行请求头。
重写 URI 并追加请求头
以下示例演示了如何重写上游 URI 并追加请求头值。如果客户端请求中已经存在同名请求头,插件配置的值会追加在客户端原有值之后。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/",
"plugins": {
"proxy-rewrite": {
"uri": "/headers",
"headers": {
"add": {
"X-Api-Version": "v1",
"X-Api-Engine": "apisix"
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
uri: /headers
headers:
add:
X-Api-Version: v1
X-Api-Engine: apisix
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
uri: /headers
headers:
add:
X-Api-Version: v1
X-Api-Engine: apisix
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
uri: /headers
headers:
add:
X-Api-Version: v1
X-Api-Engine: apisix
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
发送请求进行验证:
curl "http://127.0.0.1:9080/" -H 'X-Api-Version: v2'
你应该看到类似于以下的响应:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/8.2.1",
"X-Amzn-Trace-Id": "Root=1-64fed73a-59cd3bd640d76ab16c97f1f1",
"X-Api-Engine": "apisix",
"X-Api-Version": "v2,v1",
"X-Forwarded-Host": "127.0.0.1"
}
}
请注意,这两个请求头都存在,且请求中传入的 X-Api-Version 值会保留,插件配置的值追加在其后。
删除现有请求头
以下示例演示了如何删除现有的请求头 User-Agent。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/headers",
"plugins": {
"proxy-rewrite": {
"headers": {
"remove":[
"User-Agent"
]
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /headers
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
headers:
remove:
- User-Agent
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
headers:
remove:
- User-Agent
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /headers
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
headers:
remove:
- User-Agent
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
发送请求以验证指定的请求头是否已被删除:
curl "http://127.0.0.1:9080/headers"
你应该看到类似于以下的响应,其中 User-Agent 请求头不存在:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-64fef302-07f2b13e0eb006ba776ad91d",
"X-Forwarded-Host": "127.0.0.1"
}
}
使用正则表达式重写 URI
以下示例演示了如何从原始上游 URI 路径中解析文本,并使用它们来组成新的上游 URI 路径。在此示例中,APISIX 配置为将所有请求从 /test/user/agent 转发到 /user-agent。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"uri": "/test/*",
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/test/(.*)/(.*)", "/$1-$2"]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /test/*
name: proxy-rewrite-route
plugins:
proxy-rewrite:
regex_uri:
- ^/test/(.*)/(.*)
- /$1-$2
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
regex_uri:
- ^/test/(.*)/(.*)
- /$1-$2
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /test/
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /test/*
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
regex_uri:
- ^/test/(.*)/(.*)
- /$1-$2
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
向 /test/user/agent 发送请求,验证请求是否转发到 /user-agent:
curl "http://127.0.0.1:9080/test/user/agent"
你应该看到类似于以下的响应:
{
"user-agent": "curl/8.2.1"
}
添加 URL 参数
以下示例演示了如何向请求添加 URL 参数。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/get",
"plugins": {
"proxy-rewrite": {
"uri": "/get?arg1=apisix&arg2=plugin"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /get
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
uri: /get?arg1=apisix&arg2=plugin
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
uri: /get?arg1=apisix&arg2=plugin
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
uri: /get?arg1=apisix&arg2=plugin
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
发送请求以验证 URL 参数是否也转发到了上游:
curl "http://127.0.0.1:9080/get"
你应该看到类似于以下的响应:
{
"args": {
"arg1": "apisix",
"arg2": "plugin"
},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.2.1",
"X-Amzn-Trace-Id": "Root=1-64fef6dc-2b0e09591db7353a275cdae4",
"X-Forwarded-Host": "127.0.0.1"
},
"origin": "127.0.0.1, 103.248.35.148",
"url": "http://127.0.0.1/get?arg1=apisix&arg2=plugin"
}
重写 HTTP 方法
以下示例演示了如何将 GET 请求重写为 POST 请求。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-rewrite-route",
"methods": ["GET"],
"uri": "/get",
"plugins": {
"proxy-rewrite": {
"uri": "/anything",
"method":"POST"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- uris:
- /get
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
uri: /anything
method: POST
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
uri: /anything
method: POST
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-rewrite-route
spec:
ingressClassName: apisix
http:
- name: proxy-rewrite-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
uri: /anything
method: POST
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
向 /get 发送 GET 请求,以验证它是否被转换为对 /anything 的 POST 请求:
curl "http://127.0.0.1:9080/get"
你应该看到类似于以下的响应:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.2.1",
"X-Amzn-Trace-Id": "Root=1-64fef7de-0c63387645353998196317f2",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "POST",
"origin": "::1, 103.248.35.179",
"url": "http://localhost/anything"
}
将消费者名称转发到上游
以下示例演示了如何将成功通过身份认证的消费者名称转发到上游服务。作为示例,你将使用 key-auth 作为身份认证方法。
- Admin API
- ADC
- Ingress Controller
创建一个消费者 JohnDoe:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "JohnDoe"
}'
为消费者创建 key-auth 凭证:
curl "http://127.0.0.1:9180/apisix/admin/consumers/JohnDoe/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-john-key-auth",
"plugins": {
"key-auth": {
"key": "john-key"
}
}
}'
接下来,创建一个启用了密钥身份认证的路由,并配置 proxy-rewrite 以将消费者名称添加到请求头:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "consumer-restricted-route",
"uri": "/get",
"plugins": {
"key-auth": {},
"proxy-rewrite": {
"headers": {
"set": {
"X-Apisix-Consumer": "$consumer_name"
},
"remove": [ "Apikey" ]
}
}
},
"upstream" : {
"nodes": {
"httpbin.org":1
}
}
}'
按以下方式创建带有 key-auth 凭据的消费者,以及配置了 key-auth 和 proxy-rewrite 插件的路由:
consumers:
- username: JohnDoe
credentials:
- name: cred-john-key-auth
type: key-auth
config:
key: john-key
services:
- name: httpbin
routes:
- name: consumer-restricted-route
uris:
- /get
plugins:
key-auth: {}
proxy-rewrite:
headers:
set:
X-Apisix-Consumer: $consumer_name
remove:
- Apikey
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: johndoe
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: cred-john-key-auth
config:
key: john-key
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
- name: proxy-rewrite
config:
headers:
set:
X-Apisix-Consumer: $consumer_name
remove:
- Apikey
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: consumer-restricted-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: johndoe
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: john-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: consumer-restricted-route
spec:
ingressClassName: apisix
http:
- name: consumer-restricted-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
enable: true
- name: proxy-rewrite
enable: true
config:
headers:
set:
X-Apisix-Consumer: $consumer_name
remove:
- Apikey
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
❶ 使用内置变量将消费者名称添加 到请求头 X-Apisix-Consumer。
❷ 删除身份认证密钥,使其对上游服务不可见。
作为消费者 JohnDoe 向路由发送请求:
curl -i "http://127.0.0.1:9080/get" -H 'apikey: john-key'
你应该收到带有以下正文的 HTTP/1.1 200 OK 响应:
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.4.0",
"X-Amzn-Trace-Id": "Root=1-664b01a6-2163c0156ed4bff51d87d877",
"X-Apisix-Consumer": "JohnDoe",
"X-Forwarded-Host": "127.0.0.1"
},
"origin": "172.19.0.1, 203.12.12.12",
"url": "http://127.0.0.1/get"
}
使用 Ingress Controller 时,消费者名称会带有命名空间前缀。例如,aic 命名空间中名为 JohnDoe 的消费者会在 X-Apisix-Consumer 请求头中显示为 aic_johndoe。
向路由发送另一个不带有效凭证的请求:
curl -i "http://127.0.0.1:9080/get"
你应该收到 HTTP/1.1 401 Unauthorized 响应。
在 radixtree_uri_with_parameter 路由模式下动态转发请求
以下示例演示了如何使用 uri_param_* 变量提取 URL 路径的一部分,并将该值作为新请求头转发到上游服务。此示例假设 APISIX 运行在 radixtree_uri_with_parameter 路由模式下。
创建如下路由:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "httpbin",
"uri": "/anything/user/:user_id/profile",
"plugins":{
"proxy-rewrite": {
"headers": {
"set": {
"X-User-ID": "$uri_param_user_id"
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- name: httpbin
uris:
- /anything/user/:user_id/profile
plugins:
proxy-rewrite:
headers:
set:
X-User-ID: $uri_param_user_id
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,确认仅影响本指南使用的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=proxy-rewrite
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-rewrite-plugin-config
spec:
plugins:
- name: proxy-rewrite
config:
headers:
set:
X-User-ID: $uri_param_user_id
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: httpbin
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /anything/user/
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: httpbin
spec:
ingressClassName: apisix
http:
- name: httpbin
match:
paths:
- /anything/user/*/profile
upstreams:
- name: httpbin-external-domain
plugins:
- name: proxy-rewrite
enable: true
config:
headers:
set:
X-User-ID: $uri_param_user_id
将配置应用到集群:
kubectl apply -f proxy-rewrite-ic.yaml
❶ 匹配请求 /anything/user/:user_id/profile,其中 user_id 是一个参数。
❷ 将 user_id 参数值分配给新请求头 X-User-ID。
向路由发送请求:
curl "http://127.0.0.1:9080/anything/user/123/profile"
你应该看到以下响应:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.6.0",
"X-Amzn-Trace-Id": "Root=1-68873cf5-7248f64d19d607ea50aa9735",
"X-Forwarded-Host": "127.0.0.1",
"X-User-Id": "123"
},
...
}
路由参数也可以接受 URL 编码的字符串。例如,如果你发送如下请求:
curl -i "http://127.0.0.1:9080/anything/user/123%20456/profile"
用户 ID 将被提取为 123 456:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.6.0",
"X-Amzn-Trace-Id": "Root=1-68873d37-7634825b20d05dee3a852cb9",
"X-Forwarded-Host": "127.0.0.1",
"X-User-Id": "123 456"
},
...
}