Skip to content

Commit

Permalink
reverse proxy: rewrite http response body
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jul 31, 2024
1 parent e9a31da commit d048323
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
44 changes: 43 additions & 1 deletion docs/tutorials/reverse-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ services:

### URL路径重写

通过设置`http.rewrite`选项定义URL路径重写规则。`rewrite.match`指定路径匹配模式(支持正则表达式),`rewrite.replacement`设置路径替换内容。
通过设置`http.rewrite`选项定义URL路径重写规则。

```yaml hl_lines="16-21"
services:
Expand All @@ -316,10 +316,52 @@ services:
replacement: /$1
```

`rewrite.match` (string)
: 指定路径匹配模式(支持正则表达式)。

`rewrite.replacement` (string)
: 设置路径替换内容。

`http://example.com/api/login`会被重写为`http://example.com/user/login`。

`http://example.com/api/logout`会被重写为`http://example.com/logout`。

### 重写响应体

通过设置`http.rewriteBody`选项定义响应体重写规则。

```yaml hl_lines="16-20"
services:
- name: http
addr: :80
handler:
type: tcp
metadata:
sniffing: true
listener:
type: tcp
forwarder:
nodes:
- name: example-com
addr: example.com:80
filter:
host: example.com
http:
rewriteBody:
- match: foo
replacement: bar
type: text/html
```

`rewriteBody.match` (string)
: 指定内容匹配模式(支持正则表达式)。

`rewrite.replacement` (string)
: 设置替换内容。

`rewriteBody.type` (string, default=text/html)
: 设置响应的内容类型,与`Content-Type`匹配。可以是`,`分割的多个类型或`*`代表匹配所有类型。

## TLS请求设置

如果转发的目标节点启用了TLS,可以通过设置`forwarder.nodes.tls`来建立TLS连接。
Expand Down
44 changes: 43 additions & 1 deletion en/docs/tutorials/reverse-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ When requesting http://example.com directly, HTTP status code 401 will be return

### Rewrite URL Path

Define URL path rewriting rules by setting the `http.rewrite` option. `rewrite.match` specifies the path matching mode (supports regular expression), and `rewrite.replacement` sets the path replacement content.
Define URL path rewriting rules by setting the `http.rewrite` option.

```yaml hl_lines="16-21"
services:
Expand All @@ -314,10 +314,52 @@ services:
replacement: /$1
```

`rewrite.match` (string)
: specify path matching pattern (supports regular expression).

`rewrite.replacement` (string)
: set the path replacement content.

`http://example.com/api/login` will be rewritten to `http://example.com/user/login`.

`http://example.com/api/logout` will be rewritten to `http://example.com/logout`.

### Rewrite Response Body

Define the response body rewriting rules by setting the `http.rewriteBody` option.

```yaml hl_lines="16-20"
services:
- name: http
addr: :80
handler:
type: tcp
metadata:
sniffing: true
listener:
type: tcp
forwarder:
nodes:
- name: example-com
addr: example.com:80
filter:
host: example.com
http:
rewriteBody:
- match: foo
replacement: bar
type: text/html
```

`rewriteBody.match` (string)
: Specify content matching pattern (regular expressions are supported).

`rewrite.replacement` (string)
: Set the replacement content.

`rewriteBody.type` (string, default=text/html)
: Set the content type of the response, matching the `Content-Type` header. It can be multiple types separated by `,` or `*` to match all types.

## TLS Settings

If the forwarding target node has TLS enabled, you can establish a TLS connection by setting `forwarder.nodes.tls`.
Expand Down

0 comments on commit d048323

Please sign in to comment.