Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: refactor logic for enabling L4/L7 proxy #9607

Merged
merged 21 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,24 @@ 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
if yaml_conf.apisix.proxy_mode then
--- check for "http"
if yaml_conf.apisix.proxy_mode == "http" then
enable_http = true
enable_stream = false
--- check for "stream"
elseif yaml_conf.apisix.proxy_mode == "stream" then
enable_stream = true
enable_http = false
--- check for "http&stream"
elseif yaml_conf.apisix.proxy_mode == "http&stream" then
enable_stream = true
enable_http = true
end
end

local enabled_discoveries = {}
Expand Down Expand Up @@ -488,7 +501,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


monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved
#http is the default proxy mode. proxy_mode can be one of `http` or `stream` or `http&stream`
proxy_mode: http
Revolyssup marked this conversation as resolved.
Show resolved Hide resolved

# 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
15 changes: 1 addition & 14 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.
Revolyssup marked this conversation as resolved.
Show resolved Hide resolved

```yaml
apisix:
Expand All @@ -42,19 +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
15 changes: 1 addition & 14 deletions docs/zh/latest/stream-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ APISIX 可以对 TCP/UDP 协议进行代理并实现动态负载均衡。在 ngi

## 如何开启 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 +40,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