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

feat: support include other nginx config #2803

Merged
merged 4 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions .travis/apisix_cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,64 @@ 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this style? I think one level is enough and it looks simpler.

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";
    stream_configuration_snippet: |
        tcp_nodelay off;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@membphis
Changed.

main_configuration_snippet: |
daemon on;
http:
configuration_snippet: |
chunked_transfer_encoding on;
server_configuration_snippet: |
set $my "var";
admin:
configuration_snippet: |
log_format admin "$request_time $pipe";
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 '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
31 changes: 31 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ env {*name*};
{% end %}
{% end %}

# main configuration snippet starts
{% if main_configuration_snippet then %}
{* main_configuration_snippet *}
{% end %}
# main configuration snippet ends

{% if stream_proxy then %}
stream {
lua_package_path "$prefix/deps/share/lua/5.1/?.lua;$prefix/deps/share/lua/5.1/?/init.lua;]=]
Expand All @@ -62,6 +68,12 @@ stream {
resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} valid={*dns_resolver_valid*};
resolver_timeout {*resolver_timeout*};

# stream configuration snippet starts
{% if stream.configuration_snippet then %}
{* stream.configuration_snippet *}
{% end %}
# stream configuration snippet ends

upstream apisix_backend {
server 127.0.0.1:80;
balancer_by_lua_block {
Expand Down Expand Up @@ -204,6 +216,12 @@ http {
{% end %}
{% end %}

# http configuration snippet starts
{% if http.configuration_snippet then %}
{* http.configuration_snippet *}
{% end %}
# http configuration snippet ends

upstream apisix_backend {
server 0.0.0.1;
balancer_by_lua_block {
Expand Down Expand Up @@ -260,6 +278,13 @@ http {
listen {* port_admin *};
{%end%}
log_not_found off;

# admin configuration snippet starts
{% if admin.configuration_snippet then %}
{* admin.configuration_snippet *}
{% end %}
# admin configuration snippet ends

location /apisix/admin {
{%if allow_admin then%}
{% for _, allow_ip in ipairs(allow_admin) do %}
Expand Down Expand Up @@ -341,6 +366,12 @@ http {
ssl_session_tickets off;
{% end %}

# http server configuration snippet starts
{% if http.server_configuration_snippet then %}
{* http.server_configuration_snippet *}
{% end %}
# http server configuration snippet ends

{% if with_module_status then %}
location = /apisix/nginx_status {
allow 127.0.0.0/24;
Expand Down
22 changes: 22 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ nginx_config: # config for render the template to genarate n
worker_connections: 10620
#envs: # allow to get a list of environment variables
# - TEST_ENV

# As user can add arbitrary configurations in the snippet,
# it is user's responsibility to check the configurations
# don't conflict with APISIX.
main_configuration_snippet: |
# Add custom Nginx main configuration to nginx.conf.
# The configuration should be well indented!

http:
access_log: "logs/access.log"
access_log_format: "$remote_addr - $remote_user [$time_local] $http_host \"$request\" $status $body_bytes_sent $request_time \"$http_referer\" \"$http_user_agent\" $upstream_addr $upstream_status $upstream_response_time"
Expand All @@ -146,6 +154,20 @@ nginx_config: # config for render the template to genarate n
- 'unix:'
#lua_shared_dicts: # add custom shared cache to nginx.conf
# ipc_shared_dict: 100m # custom shared cache, format: `cache-key: cache-size`
configuration_snippet: |
# Add custom Nginx http configuration to nginx.conf.
# The configuration should be well indented!
server_configuration_snippet: |
# Add custom Nginx http server configuration to nginx.conf.
# The configuration should be well indented!
admin:
configuration_snippet: |
# Add custom Nginx admin server configuration to nginx.conf.
# The configuration should be well indented!
stream:
configuration_snippet: |
# Add custom Nginx stream configuration to nginx.conf.
# The configuration should be well indented!

etcd:
host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
Expand Down