diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh index ae13fba06ca0..d58aeeed03c8 100755 --- a/.travis/apisix_cli_test.sh +++ b/.travis/apisix_cli_test.sh @@ -78,6 +78,49 @@ fi echo "passed: change default ssl port" +# check support multiple ports listen in http and https + +echo " +apisix: + node_listen: + - 9080 + - 9081 + - 9082 + ssl: + listen_port: + - 9443 + - 9444 + - 9445 +" > conf/config.yaml + +make init + +count_http_ipv4=`grep -c "listen 908." conf/nginx.conf || true` +if [ $count_http_ipv4 -ne 3 ]; then + echo "failed: failed to support multiple ports listen in http with ipv4" + exit 1 +fi + +count_http_ipv6=`grep -c "listen \[::\]:908." conf/nginx.conf || true` +if [ $count_http_ipv6 -ne 3 ]; then + echo "failed: failed to support multiple ports listen in http with ipv6" + exit 1 +fi + +count_https_ipv4=`grep -c "listen 944. ssl" conf/nginx.conf || true` +if [ $count_https_ipv4 -ne 3 ]; then + echo "failed: failed to support multiple ports listen in https with ipv4" + exit 1 +fi + +count_https_ipv6=`grep -c "listen \[::\]:944. ssl" conf/nginx.conf || true` +if [ $count_https_ipv6 -ne 3 ]; then + echo "failed: failed to support multiple ports listen in https with ipv6" + exit 1 +fi + +echo "passed: support multiple ports listen in http and https" + # check default env echo " nginx_config: diff --git a/FAQ.md b/FAQ.md index 11d13baaa0fa..520dc60d80a7 100644 --- a/FAQ.md +++ b/FAQ.md @@ -270,3 +270,30 @@ If your APISIX node does not open the Admin API, then you can manually load the ```shell apisix reload ``` + +## How to make APISIX listen on multiple ports when handling HTTP or HTTPS requests? + +By default, APISIX only listens on port 9080 when handling HTTP requests. If you want APISIX to listen on multiple ports, you need to modify the relevant parameters in the configuration file as follows: + +1. Modify the parameter of HTTP port listen `node_listen` in `conf/config.yaml`, for example: + + ``` + apisix: + node_listen: + - 9080 + - 9081 + - 9082 + ``` + + Handling HTTPS requests is similar, modify the parameter of HTTPS port listen `ssl.listen_port` in `conf/config.yaml`, for example: + + ``` + apisix: + ssl: + listen_port: + - 9443 + - 9444 + - 9445 + ``` + +2. Restart APISIX diff --git a/FAQ_CN.md b/FAQ_CN.md index 483770a5ec82..eb03f98c9339 100644 --- a/FAQ_CN.md +++ b/FAQ_CN.md @@ -220,3 +220,30 @@ curl http://127.0.0.1:9080/apisix/admin/plugins/reload -H 'X-API-KEY: edd1c9f034 ```shell apisix reload ``` + +## 如何让 APISIX 在处理 HTTP 或 HTTPS 请求时监听多个端口 + +默认情况下,APISIX 在处理 HTTP 请求时只监听 9080 端口。如果你想让 APISIX 监听多个端口,你需要修改配置文件中的相关参数,具体步骤如下: + +1. 修改`conf/config.yaml`中 HTTP 端口监听的参数`node_listen`,示例: + + ``` + apisix: + node_listen: + - 9080 + - 9081 + - 9082 + ``` + + 处理 HTTPS 请求也类似,修改`conf/config.yaml`中 HTTPS 端口监听的参数``ssl.listen_port``,示例: + + ``` + apisix: + ssl: + listen_port: + - 9443 + - 9444 + - 9445 + ``` + +2. 重启 APISIX diff --git a/bin/apisix b/bin/apisix index 32519a6b099d..75c4184dc508 100755 --- a/bin/apisix +++ b/bin/apisix @@ -353,11 +353,14 @@ http { {% end %} server { - listen {* node_listen *} {% if enable_reuseport then %} reuseport {% end %}; + {% for _, port in ipairs(node_listen) do %} + listen {* port *} {% if enable_reuseport then %} reuseport {% end %}; + {% end %} {% if ssl.enable then %} - listen {* ssl.listen_port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %}; + {% for _, port in ipairs(ssl.listen_port) do %} + listen {* port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %}; + {% end %} {% end %} - {% if proxy_protocol and proxy_protocol.listen_http_port then %} listen {* proxy_protocol.listen_http_port *} proxy_protocol; {% end %} @@ -366,9 +369,13 @@ http { {% end %} {% if enable_ipv6 then %} - listen [::]:{* node_listen *} {% if enable_reuseport then %} reuseport {% end %}; + {% for _, port in ipairs(node_listen) do %} + listen [::]:{* port *} {% if enable_reuseport then %} reuseport {% end %}; + {% end %} {% if ssl.enable then %} - listen [::]:{* ssl.listen_port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %}; + {% for _, port in ipairs(ssl.listen_port) do %} + listen [::]:{* port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %}; + {% end %} {% end %} {% end %} {% -- if enable_ipv6 %} @@ -875,6 +882,18 @@ Please modify "admin_key" in conf/config.yaml . error("missing apisix.proxy_cache for plugin proxy-cache") end + --support multiple ports listen, compatible with the original style + if type(yaml_conf.apisix.node_listen) == "number" then + local node_listen = {yaml_conf.apisix.node_listen} + yaml_conf.apisix.node_listen = node_listen + end + + if type(yaml_conf.apisix.ssl.listen_port) == "number" then + local listen_port = {yaml_conf.apisix.ssl.listen_port} + yaml_conf.apisix.ssl.listen_port = listen_port + end + + -- Using template.render local sys_conf = { lua_path = pkg_path_org,