Skip to content

Commit

Permalink
fix: Refactor logic for enabling L4/L7 proxy
Browse files Browse the repository at this point in the history
Signed-off-by: revolyssup <[email protected]>
  • Loading branch information
Revolyssup committed Jun 6, 2023
1 parent ec0aa49 commit 0d7286b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
20 changes: 15 additions & 5 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,21 @@ Please modify "admin_key" in conf/config.yaml .
"your openresty, please check it out.\n")
end

--- http is enabled by default
local enable_http = true
if not yaml_conf.apisix.enable_admin and yaml_conf.apisix.stream_proxy and
yaml_conf.apisix.stream_proxy.only ~= false
then
enable_http = false
--- stream is disabled by default
local enable_stream = false
local http = "http"
local stream = "stream"
if yaml_conf.apisix.proxy_mode then
-- check for "http" as prefix
if string.sub(yaml_conf.apisix.proxy_mode,1,#http) ~= http then
enable_http = false
end
-- check for "stream" as suffix
if string.sub(yaml_conf.apisix.proxy_mode, -#stream) == stream then
enable_stream = true
end
end

local enabled_discoveries = {}
Expand Down Expand Up @@ -488,7 +498,7 @@ Please modify "admin_key" in conf/config.yaml .

local tcp_enable_ssl
-- compatible with the original style which only has the addr
if yaml_conf.apisix.stream_proxy and yaml_conf.apisix.stream_proxy.tcp then
if enable_stream and yaml_conf.apisix.stream_proxy and yaml_conf.apisix.stream_proxy.tcp then
local tcp = yaml_conf.apisix.stream_proxy.tcp
for i, item in ipairs(tcp) do
if type(item) ~= "table" then
Expand Down
4 changes: 4 additions & 0 deletions apisix/cli/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ local config_schema = {
}
}
},
proxy_mode = {
type = "string",
enum = {"http", "stream", "http&stream"},
},
stream_proxy = {
type = "object",
properties = {
Expand Down
6 changes: 5 additions & 1 deletion conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ apisix:
memory_size: 50m

delete_uri_tail_slash: false # delete the '/' at the end of the URI


#http is the default proxy mode. proxy_mode can be oneOf http or stream or http&stream
proxy_mode: http

# The URI normalization in servlet is a little different from the RFC's.
# See https://github.com/jakartaee/servlet/blob/master/spec/src/main/asciidoc/servlet-spec-body.adoc#352-uri-path-canonicalization,
# which is used under Tomcat.
Expand All @@ -82,7 +87,6 @@ apisix:
# more details.
ssl: radixtree_sni # radixtree_sni: match route by SNI(base on radixtree)
#stream_proxy: # TCP/UDP proxy
# only: true # use stream proxy only, don't enable HTTP stuff
# tcp: # TCP proxy port list
# - addr: 9100
# tls: true
Expand Down
14 changes: 1 addition & 13 deletions docs/en/latest/stream-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ APISIX can serve as a stream proxy, in addition to being an application layer pr

By default, stream proxy is disabled.

To enable the option, add the `apisix.stream_proxy` option in `conf/config.yaml` and specify a list of addresses which APISIX should act as a stream proxy and listen for incoming requests.
To enable this option, set `apisix.proxy_mode` to `stream` or `http&stream`, depending on whether you want stream proxy only or http and stream. Then add the apisix.stream_proxy option in conf/config.yaml and specify the list of addresses where APISIX should act as a stream proxy and listen for incoming requests.

```yaml
apisix:
Expand All @@ -42,18 +42,6 @@ apisix:
- "127.0.0.1:9211"
```
If `apisix.enable_admin` is true, both HTTP and stream proxy are enabled with the configuration above.

If you have set the `enable_admin` to false, and need to enable both HTTP and stream proxy, set the `only` to false:

```yaml
apisix:
enable_admin: false
stream_proxy:
only: false
tcp:
- 9100
```
If `apisix.stream_proxy` is undefined in `conf/config.yaml`, you will encounter an error similar to the following and not be able to add a stream route:

Expand Down
17 changes: 3 additions & 14 deletions docs/zh/latest/stream-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ title: TCP/UDP 动态代理

APISIX 可以对 TCP/UDP 协议进行代理并实现动态负载均衡。在 nginx 世界,称 TCP/UDP 代理为 stream 代理,在 APISIX 这里我们也遵循了这个声明。



## 如何开启 Stream 代理

`conf/config.yaml` 配置文件设置 `stream_proxy` 选项,指定一组需要进行动态代理的 IP 地址。默认情况不开启 stream 代理
要启用该选项,请将 `apisix.proxy_mode` 设置为 `stream``http&stream`,具体取决于您是只需要流代理还是需要 http 和流。然后在 conf/config.yaml 中添加 apisix.stream_proxy 选项并指定 APISIX 应充当流代理并侦听传入请求的地址列表

```yaml
apisix:
Expand All @@ -40,19 +42,6 @@ apisix:
- "127.0.0.1:9211"
```
如果 `apisix.enable_admin` 为 true,上面的配置会同时启用 HTTP 和 stream 代理。

如果你设置 `enable_admin` 为 false,且需要同时启用 HTTP 和 stream 代理,设置 `only` 为 false:

```yaml
apisix:
enable_admin: false
stream_proxy: # TCP/UDP proxy
only: false
tcp: # TCP proxy address list
- 9100
```

## 如何设置 route
简例如下:
Expand Down

0 comments on commit 0d7286b

Please sign in to comment.