From 0d7286b2ac59ea34cccbcd4922a9074394528889 Mon Sep 17 00:00:00 2001 From: revolyssup Date: Tue, 6 Jun 2023 12:14:51 +0530 Subject: [PATCH] fix: Refactor logic for enabling L4/L7 proxy Signed-off-by: revolyssup --- apisix/cli/ops.lua | 20 +++++++++++++++----- apisix/cli/schema.lua | 4 ++++ conf/config-default.yaml | 6 +++++- docs/en/latest/stream-proxy.md | 14 +------------- docs/zh/latest/stream-proxy.md | 17 +++-------------- 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index ebd3061e0c12..662f1fc20fad 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -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 = {} @@ -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 diff --git a/apisix/cli/schema.lua b/apisix/cli/schema.lua index 56d7e2f630cc..de582ab767c0 100644 --- a/apisix/cli/schema.lua +++ b/apisix/cli/schema.lua @@ -136,6 +136,10 @@ local config_schema = { } } }, + proxy_mode = { + type = "string", + enum = {"http", "stream", "http&stream"}, + }, stream_proxy = { type = "object", properties = { diff --git a/conf/config-default.yaml b/conf/config-default.yaml index d41df397b9e5..0d166eccd825 100755 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -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. @@ -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 diff --git a/docs/en/latest/stream-proxy.md b/docs/en/latest/stream-proxy.md index c68173d09c59..e9df19ef2f1f 100644 --- a/docs/en/latest/stream-proxy.md +++ b/docs/en/latest/stream-proxy.md @@ -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: @@ -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: diff --git a/docs/zh/latest/stream-proxy.md b/docs/zh/latest/stream-proxy.md index e2c1de110f43..c5e7258a4f0d 100644 --- a/docs/zh/latest/stream-proxy.md +++ b/docs/zh/latest/stream-proxy.md @@ -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: @@ -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 简例如下: