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

feature: support multiple ports like stream listen in http and https … #2409

Merged
merged 7 commits into from
Oct 17, 2020
43 changes: 43 additions & 0 deletions .travis/apisix_cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions FAQ_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 24 additions & 5 deletions bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}

Expand Down Expand Up @@ -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,
Expand Down