proxy-rewrite
proxy-rewrite 插件提供了重写 APISIX 转发到上游服务请求的选项。使用该插件,你可以修改 HTTP 方法、请求目标上游地址、请求头等。
示例
以下示例展示了如何在不同场景下的路由上配置 proxy-rewrite。
重写 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
}
}
}'
adc.yaml
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 sync -f adc.yaml
- Gateway API
- APISIX CRD
proxy-rewrite-ic.yaml
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
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 并设置其他请求头值。如果客户端请求中存在相同的请求头,则插件中设置的相应请求头值将覆盖客户端请求中的值。
- 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",
"X-Api-Engine": "apisix"
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
adc.yaml
services:
- name: httpbin
routes:
- uris:
- /
name: proxy-rewrite-route
methods:
- GET
plugins:
proxy-rewrite:
uri: /anything
headers:
set:
X-Api-Version: v1
X-Api-Engine: apisix
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
proxy-rewrite-ic.yaml
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
headers:
set:
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
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: /anything
headers:
set:
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'
你应该看到类似于以下的响应:
{
"args": {},
"data": "",
"files": {},
"form": {},
"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": "v1",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "GET",
"origin": "::1, 103.248.35.179",
"url": "http://localhost/anything"
}
请注意,现有的请求头和请求中传递的 X-Api-Version 请求头值都被插件中配置的值覆盖。
重写 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
}
}
}'
adc.yaml
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 sync -f adc.yaml
- Gateway API
- APISIX CRD
proxy-rewrite-ic.yaml
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
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
}
}
}'
adc.yaml
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 sync -f adc.yaml
- Gateway API
- APISIX CRD
proxy-rewrite-ic.yaml
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
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"
}
}