Skip to content

Commit

Permalink
feat: the proxy-rewrite plugin support pass nginx variable within hea…
Browse files Browse the repository at this point in the history
…der (#3144)

related #3117
  • Loading branch information
Firstsawyou committed Dec 28, 2020
1 parent 45e909a commit c4180cc
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 18 deletions.
3 changes: 2 additions & 1 deletion apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ function _M.rewrite(conf, ctx)

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.req.set_header(conf.headers_arr[i], conf.headers_arr[i+1])
ngx.req.set_header(conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
end
end

Expand Down
12 changes: 9 additions & 3 deletions doc/plugins/proxy-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [中文](../zh-cn/plugins/proxy-rewrite.md)

# Summary

- [**Name**](#name)
- [**Attributes**](#attributes)
- [**How To Enable**](#how-to-enable)
Expand All @@ -28,17 +29,17 @@

## Name

upstream proxy info rewrite plugin.
The `proxy-rewrite` is an upstream proxy information rewriting plugin, which supports the rewriting of information such as `scheme`, `uri`, and `host`.

## Attributes

| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| scheme | string | optional | "http" | ["http", "https"] | Upstream new `schema` forwarding protocol |
| scheme | string | optional | "http" | ["http", "https"] | Upstream new `schema` forwarding protocol. |
| uri | string | optional | | | Upstream new `uri` forwarding address. |
| regex_uri | array[string] | optional | | | Upstream new `uri` forwarding address. Use regular expression to match URL from client, when the match is successful, the URL template will be forwarded upstream. If the match is not successful, the URL from the client will be forwarded to the upstream. When `uri` and` regex_uri` are both exist, `uri` is used first. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is forwarded to the upstream. |
| host | string | optional | | | Upstream new `host` forwarding address, example `iresty.com`. |
| headers | object | optional | | | Forward to the new `headers` of the upstream, can set up multiple. If it exists, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. |
| headers | object | optional | | | Forward to the new `headers` of the upstream, can set up multiple. If it exists, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. Support the use of Nginx variables. Need to start with `$`, such as `client_addr: $remote_addr`: it means that the request header `client_addr` is the client IP. |

## How To Enable

Expand Down Expand Up @@ -71,22 +72,27 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1
```

## Test Plugin

Testing based on the above examples :

```shell
curl -X GET http://127.0.0.1:9080/test/index.html
```

Send the request and see upstream `access.log', if the output information is consistent with the configuration :

```
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
```

This means that the `proxy rewrite` plugin is in effect.

## Disable Plugin

When you want to disable the `proxy rewrite` plugin, it is very simple,
you can delete the corresponding json configuration in the plugin configuration,
no need to restart the service, it will take effect immediately :

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
Expand Down
39 changes: 25 additions & 14 deletions doc/zh-cn/plugins/proxy-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,30 @@

- [English](../../plugins/proxy-rewrite.md)

# proxy-rewrite
# 目录

上游代理信息重写插件。
- [简介](#简介)
- [属性](#属性)
- [如何启用](#如何启用)
- [测试插件](#测试插件)
- [禁用插件](#禁用插件)

#### 配置参数
## 简介

| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| scheme | string | 可选 | "http" | ["http", "https"] | 转发到上游的新 `schema` 协议 |
| 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 的话,把对应的值设置为空字符串即可 |
proxy-rewrite 是上游代理信息重写插件,支持对 `scheme``uri``host` 等信息的重写。

### 示例
## 属性

| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| scheme | string | 可选 | "http" | ["http", "https"] | 转发到上游的新 `schema` 协议。 |
| 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。 |

## 如何启用

#### 开启插件
下面是一个示例,在指定的 route 上开启了 `proxy rewrite` 插件:

```shell
Expand Down Expand Up @@ -64,20 +71,24 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1
}'
```

#### 测试插件
## 测试插件

基于上述配置进行测试:

```shell
curl -X GET http://127.0.0.1:9080/test/index.html
```

发送请求,查看上游服务`access.log`,如果输出信息与配置一致:

```
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` 插件生效了。

#### 禁用插件
## 禁用插件

当你想去掉 `proxy rewrite` 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:

```shell
Expand Down
118 changes: 118 additions & 0 deletions t/plugin/proxy-rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -1131,3 +1131,121 @@ GET /t
additional properties forbidden, found invalid_att
--- no_error_log
[error]



=== TEST 38: set route(header contains nginx variables)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri",
"headers": {
"x-api": "$remote_addr",
"name": "$arg_name",
"x-key": "$http_key"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 39: hit route(header supports nginx variables)
--- request
GET /hello?name=Bill HTTP/1.1
--- more_headers
key: X-APISIX
--- response_body
uri: /uri
host: localhost
key: X-APISIX
name: Bill
x-api: 127.0.0.1
x-key: X-APISIX
x-real-ip: 127.0.0.1
--- no_error_log
[error]



=== TEST 40: set route(nginx variable does not exist)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/uri",
"headers": {
"x-api": "$helle",
"name": "$arg_world",
"x-key": "$http_key",
"Version": "nginx_var_does_not_exist"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 41: hit route(get nginx variable is nil)
--- request
GET /hello HTTP/1.1
--- response_body
uri: /uri
host: localhost
version: nginx_var_does_not_exist
x-real-ip: 127.0.0.1
--- no_error_log
[error]

0 comments on commit c4180cc

Please sign in to comment.