Skip to content

Commit

Permalink
fix: use the builtin server by default
Browse files Browse the repository at this point in the history
Fix #3906

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Mar 25, 2021
1 parent 654b1a9 commit ede6b0e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 75 deletions.
14 changes: 8 additions & 6 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,33 @@ http {
server {
{% for _, item in ipairs(node_listen) do %}
listen {* item.port *} {% if enable_reuseport then %} reuseport {% end %} {% if item.enable_http2 then %} http2 {% end %};
listen {* item.port *} default_server {% if enable_reuseport then %} reuseport {% end %} {% if item.enable_http2 then %} http2 {% end %};
{% end %}
{% if ssl.enable then %}
{% for _, port in ipairs(ssl.listen_port) do %}
listen {* port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %};
listen {* port *} ssl default_server {% 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;
listen {* proxy_protocol.listen_http_port *} default_server proxy_protocol;
{% end %}
{% if proxy_protocol and proxy_protocol.listen_https_port then %}
listen {* proxy_protocol.listen_https_port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} proxy_protocol;
listen {* proxy_protocol.listen_https_port *} ssl default_server {% if ssl.enable_http2 then %} http2 {% end %} proxy_protocol;
{% end %}
{% if enable_ipv6 then %}
{% for _, item in ipairs(node_listen) do %}
listen [::]:{* item.port *} {% if enable_reuseport then %} reuseport {% end %} {% if item.enable_http2 then %} http2 {% end %};
listen [::]:{* item.port *} default_server {% if enable_reuseport then %} reuseport {% end %} {% if item.enable_http2 then %} http2 {% end %};
{% end %}
{% if ssl.enable then %}
{% for _, port in ipairs(ssl.listen_port) do %}
listen [::]:{* port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %};
listen [::]:{* port *} ssl default_server {% if ssl.enable_http2 then %} http2 {% end %} {% if enable_reuseport then %} reuseport {% end %};
{% end %}
{% end %}
{% end %} {% -- if enable_ipv6 %}
server_name _;
{% if ssl.ssl_trusted_certificate ~= nil then %}
lua_ssl_trusted_certificate {* ssl.ssl_trusted_certificate *};
{% end %}
Expand Down
69 changes: 0 additions & 69 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -481,75 +481,6 @@ fi

echo "passed: found 'my_dict' in nginx.conf"

# allow injecting configuration snippets

echo '
apisix:
node_listen: 9080
enable_admin: true
port_admin: 9180
stream_proxy:
tcp:
- 9100
nginx_config:
main_configuration_snippet: |
daemon on;
http_configuration_snippet: |
chunked_transfer_encoding on;
http_server_configuration_snippet: |
set $my "var";
http_admin_configuration_snippet: |
log_format admin "$request_time $pipe";
http_end_configuration_snippet: |
server_names_hash_bucket_size 128;
stream_configuration_snippet: |
tcp_nodelay off;
' > conf/config.yaml

make init

grep "daemon on;" -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject main configuration"
exit 1
fi

grep "chunked_transfer_encoding on;" -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http configuration"
exit 1
fi

grep 'set $my "var";' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http server configuration"
exit 1
fi

grep 'log_format admin "$request_time $pipe";' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject admin server configuration"
exit 1
fi

grep 'server_names_hash_bucket_size 128;' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http end configuration"
exit 1
fi

grep 'server_names_hash_bucket_size 128;' -A 3 conf/nginx.conf | grep "}" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http end configuration"
exit 1
fi

grep 'tcp_nodelay off;' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject stream configuration"
exit 1
fi

# check disable cpu affinity
git checkout conf/config.yaml

Expand Down
123 changes: 123 additions & 0 deletions t/cli/test_snippet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env bash

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

. ./t/cli/common.sh

# allow injecting configuration snippets

echo '
apisix:
node_listen: 9080
enable_admin: true
port_admin: 9180
stream_proxy:
tcp:
- 9100
nginx_config:
main_configuration_snippet: |
daemon on;
http_configuration_snippet: |
chunked_transfer_encoding on;
http_server_configuration_snippet: |
set $my "var";
http_admin_configuration_snippet: |
log_format admin "$request_time $pipe";
http_end_configuration_snippet: |
server_names_hash_bucket_size 128;
stream_configuration_snippet: |
tcp_nodelay off;
' > conf/config.yaml

make init

grep "daemon on;" -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject main configuration"
exit 1
fi

grep "chunked_transfer_encoding on;" -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http configuration"
exit 1
fi

grep 'set $my "var";' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http server configuration"
exit 1
fi

grep 'log_format admin "$request_time $pipe";' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject admin server configuration"
exit 1
fi

grep 'server_names_hash_bucket_size 128;' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http end configuration"
exit 1
fi

grep 'server_names_hash_bucket_size 128;' -A 3 conf/nginx.conf | grep "}" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject http end configuration"
exit 1
fi

grep 'tcp_nodelay off;' -A 2 conf/nginx.conf | grep "configuration snippet ends" > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: can't inject stream configuration"
exit 1
fi

# use the builtin server by default

echo '
apisix:
node_listen: 9080
nginx_config:
http_configuration_snippet: |
server {
listen 9080;
server_name qa.com www.qa.com;
location / {
return 503 "ouch";
}
}
' > conf/config.yaml

make run

sleep 1
code=$(curl -k -i -o /dev/null -s -w %{http_code} http://127.0.0.1:9080 -H 'Host: m.qa.com')
if [ ! $code -eq 404 ]; then
echo "failed: use the builtin server by default"
exit 1
fi
code=$(curl -k -i -o /dev/null -s -w %{http_code} http://127.0.0.1:9080 -H 'Host: www.qa.com')
if [ ! $code -eq 503 ]; then
echo "failed: use the builtin server by default"
exit 1
fi

make stop

echo "passed: use the builtin server by default"

0 comments on commit ede6b0e

Please sign in to comment.