2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
|
|
|
|
title: proxy-rewrite
|
|
|
|
|
---
|
|
|
|
|
|
2019-10-31 09:27:28 +08:00
|
|
|
|
<!--
|
|
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
2021-02-26 21:40:08 +08:00
|
|
|
|
## 目录
|
2019-09-27 15:14:49 +08:00
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
- [简介](#简介)
|
|
|
|
|
- [属性](#属性)
|
|
|
|
|
- [如何启用](#如何启用)
|
|
|
|
|
- [测试插件](#测试插件)
|
|
|
|
|
- [禁用插件](#禁用插件)
|
2019-09-27 15:14:49 +08:00
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
## 简介
|
2020-09-23 08:11:27 +08:00
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
proxy-rewrite 是上游代理信息重写插件,支持对 `scheme`、`uri`、`host` 等信息的重写。
|
2019-09-27 15:14:49 +08:00
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
## 属性
|
|
|
|
|
|
|
|
|
|
| Name | Type | Requirement | Default | Valid | Description |
|
|
|
|
|
| --------- | ------------- | ----------- | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
2021-02-24 14:37:30 +08:00
|
|
|
|
| scheme | string | 可选 | "http" | ["http", "https"] | 不推荐使用。应该在 Upstream 的 scheme 字段设置上游的 scheme。
|
2020-12-28 20:42:47 +08:00
|
|
|
|
| uri | string | 可选 | | | 转发到上游的新 `uri` 地址。 |
|
|
|
|
|
| regex_uri | array[string] | 可选 | | | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的uri,当匹配成功后使用模板替换转发到上游的uri, 未匹配成功时将客户端请求的uri转发至上游。当`uri`和`regex_uri`同时存在时,`uri`优先被使用。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的uri正则表达式,第二个元素代表匹配成功后转发到上游的uri模板。 |
|
|
|
|
|
| host | string | 可选 | | | 转发到上游的新 `host` 地址,例如:`iresty.com` 。 |
|
|
|
|
|
| headers | object | 可选 | | | 转发到上游的新`headers`,可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可。支持使用 Nginx 的变量,需要以 `$` 开头,如 `client_addr: $remote_addr` :表示请求头 `client_addr` 为客户端IP。 |
|
|
|
|
|
|
|
|
|
|
## 如何启用
|
2019-09-27 15:14:49 +08:00
|
|
|
|
|
|
|
|
|
下面是一个示例,在指定的 route 上开启了 `proxy rewrite` 插件:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-09-27 15:14:49 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/test/index.html",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"proxy-rewrite": {
|
|
|
|
|
"uri": "/test/home.html",
|
|
|
|
|
"scheme": "http",
|
|
|
|
|
"host": "iresty.com",
|
2019-10-09 15:10:16 +08:00
|
|
|
|
"headers": {
|
|
|
|
|
"X-Api-Version": "v1",
|
2019-10-15 16:06:28 +08:00
|
|
|
|
"X-Api-Engine": "apisix",
|
2019-11-14 11:50:15 +08:00
|
|
|
|
"X-Api-useless": ""
|
2019-10-09 15:10:16 +08:00
|
|
|
|
}
|
2019-09-27 15:14:49 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
## 测试插件
|
|
|
|
|
|
2019-09-27 15:14:49 +08:00
|
|
|
|
基于上述配置进行测试:
|
2020-12-28 20:42:47 +08:00
|
|
|
|
|
2019-09-27 15:14:49 +08:00
|
|
|
|
```shell
|
|
|
|
|
curl -X GET http://127.0.0.1:9080/test/index.html
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
发送请求,查看上游服务`access.log`,如果输出信息与配置一致:
|
2020-12-28 20:42:47 +08:00
|
|
|
|
|
2019-09-27 15:14:49 +08:00
|
|
|
|
```
|
|
|
|
|
127.0.0.1 - [26/Sep/2019:10:52:20 +0800] iresty.com GET /test/home.html HTTP/1.1 200 38 - curl/7.29.0 - 0.000 199 107
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
即表示 `proxy rewrite` 插件生效了。
|
|
|
|
|
|
2020-12-28 20:42:47 +08:00
|
|
|
|
## 禁用插件
|
|
|
|
|
|
2019-09-27 15:14:49 +08:00
|
|
|
|
当你想去掉 `proxy rewrite` 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-09-27 15:14:49 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/test/index.html",
|
|
|
|
|
"plugins": {},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
现在就已经移除了 `proxy rewrite` 插件了。其他插件的开启和移除也是同样的方法。
|