From d4d2a62564a18b0e42d81ff42f77306f123d0825 Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 20 Nov 2020 18:05:42 +0800 Subject: [PATCH 1/4] feat: support include other nginx config Close #2565 Close #1620 --- .travis/apisix_cli_test.sh | 58 ++++++++++++++++++++++++++++++++++++++ apisix/cli/ngx_tpl.lua | 31 ++++++++++++++++++++ conf/config-default.yaml | 22 +++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh index a8e0389ed81c..6457b26d22ef 100755 --- a/.travis/apisix_cli_test.sh +++ b/.travis/apisix_cli_test.sh @@ -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: + 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 diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index 0af266d256e6..94dacdbfacad 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -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;]=] @@ -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 { @@ -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 { @@ -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 %} @@ -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; diff --git a/conf/config-default.yaml b/conf/config-default.yaml index 003f0aed03aa..3c5f47f100c7 100644 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -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" @@ -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. From 718ba719acc2844f678b6676306c743ade99e826 Mon Sep 17 00:00:00 2001 From: spacewander Date: Tue, 24 Nov 2020 09:37:04 +0800 Subject: [PATCH 2/4] change style --- .travis/apisix_cli_test.sh | 19 ++++++++----------- apisix/cli/ngx_tpl.lua | 16 ++++++++-------- conf/config-default.yaml | 26 ++++++++++++-------------- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh index 6457b26d22ef..3d2af340e6e9 100755 --- a/.travis/apisix_cli_test.sh +++ b/.travis/apisix_cli_test.sh @@ -471,17 +471,14 @@ apisix: nginx_config: 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; + 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; ' > conf/config.yaml make init diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index 94dacdbfacad..c7e8738185fd 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -69,8 +69,8 @@ stream { resolver_timeout {*resolver_timeout*}; # stream configuration snippet starts - {% if stream.configuration_snippet then %} - {* stream.configuration_snippet *} + {% if stream_configuration_snippet then %} + {* stream_configuration_snippet *} {% end %} # stream configuration snippet ends @@ -217,8 +217,8 @@ http { {% end %} # http configuration snippet starts - {% if http.configuration_snippet then %} - {* http.configuration_snippet *} + {% if http_configuration_snippet then %} + {* http_configuration_snippet *} {% end %} # http configuration snippet ends @@ -280,8 +280,8 @@ http { log_not_found off; # admin configuration snippet starts - {% if admin.configuration_snippet then %} - {* admin.configuration_snippet *} + {% if http_admin_configuration_snippet then %} + {* http_admin_configuration_snippet *} {% end %} # admin configuration snippet ends @@ -367,8 +367,8 @@ http { {% end %} # http server configuration snippet starts - {% if http.server_configuration_snippet then %} - {* http.server_configuration_snippet *} + {% if http_server_configuration_snippet then %} + {* http_server_configuration_snippet *} {% end %} # http server configuration snippet ends diff --git a/conf/config-default.yaml b/conf/config-default.yaml index 3c5f47f100c7..07ee871ca34d 100644 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -134,6 +134,18 @@ nginx_config: # config for render the template to genarate n main_configuration_snippet: | # Add custom Nginx main configuration to nginx.conf. # The configuration should be well indented! + http_configuration_snippet: | + # Add custom Nginx http configuration to nginx.conf. + # The configuration should be well indented! + http_server_configuration_snippet: | + # Add custom Nginx http server configuration to nginx.conf. + # The configuration should be well indented! + http_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! http: access_log: "logs/access.log" @@ -154,20 +166,6 @@ 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. From 5629076bdd98b45b6640d9966956199bc66c07e4 Mon Sep 17 00:00:00 2001 From: spacewander Date: Wed, 25 Nov 2020 15:35:01 +0800 Subject: [PATCH 3/4] doc --- doc/customize-nginx-configuration.md | 59 ++++++++++++++++++++++ doc/zh-cn/customize-nginx-configuration.md | 59 ++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 doc/customize-nginx-configuration.md create mode 100644 doc/zh-cn/customize-nginx-configuration.md diff --git a/doc/customize-nginx-configuration.md b/doc/customize-nginx-configuration.md new file mode 100644 index 000000000000..d65fd8e7b868 --- /dev/null +++ b/doc/customize-nginx-configuration.md @@ -0,0 +1,59 @@ + + +[中文](./zh-cn/customize-nginx-configuration.md) + +# Customize Nginx configuration + +The Nginx configuration used by APISIX is generated via the template file `apisix/ngx_tpl.lua` and the options from `conf/config-default.yaml` / `conf/config.yaml`. + +You can take a look at the generated Nginx configuration in `conf/nginx.conf` after running `./bin/apisix start`. + +If you want to customize the Nginx configuration, please read through the `nginx_config` in `conf/config-default.yaml`. You can override the default value in the `conf/config.yaml`. For instance, you can inject some snippets in the `conf/nginx.conf` via configuring the `xxx_snippet` entries: + +```yaml +... +# put this in config.yaml: +nginx_config: + main_configuration_snippet: | + daemon on; + http_configuration_snippet: | + server + { + listen 45651; + server_name _; + access_log off; + + location /ysec_status { + req_status_show; + allow 127.0.0.1; + deny all; + } + } + + 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; +... +``` diff --git a/doc/zh-cn/customize-nginx-configuration.md b/doc/zh-cn/customize-nginx-configuration.md new file mode 100644 index 000000000000..5b34f19bf520 --- /dev/null +++ b/doc/zh-cn/customize-nginx-configuration.md @@ -0,0 +1,59 @@ + + +[English](../customize-nginx-configuration.md) + +# 自定义 Nginx 配置 + +APISIX 会通过 `apisix/ngx_tpl.lua` 这个模板和 `conf/config-default.yaml` 加 `conf/config.yaml` 的配置生成 Nginx 配置文件。 + +在执行完 `./bin/apisix start`,你可以在 `conf/nginx.conf` 看到生成的 Nginx 配置文件。 + +在自定义 Nginx 配置文件之前,烦请仔细阅读 `conf/config-default.yaml`。你可以在 `conf/config.yaml` 里面覆盖掉默认值。举个例子,你可以通过 `xxx_snippet` 之类的配置,在 `conf/nginx.conf` 里面注入你的自定义配置: + +```yaml +... +# config.yaml 里面的内容 +nginx_config: + main_configuration_snippet: | + daemon on; + http_configuration_snippet: | + server + { + listen 45651; + server_name _; + access_log off; + + location /ysec_status { + req_status_show; + allow 127.0.0.1; + deny all; + } + } + + 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; +... +``` From d68c5a9456da1b869db417f9d5d91c4eb408e403 Mon Sep 17 00:00:00 2001 From: spacewander Date: Wed, 25 Nov 2020 15:57:28 +0800 Subject: [PATCH 4/4] link --- doc/README.md | 1 + doc/zh-cn/README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/README.md b/doc/README.md index 829dfaf50c99..f2ea095a451f 100644 --- a/doc/README.md +++ b/doc/README.md @@ -31,6 +31,7 @@ * [Stand Alone Model](stand-alone.md): Supports to load route rules from local yaml file, it is more friendly such as under the kubernetes(k8s). * [Stream Proxy](stream-proxy.md) * [gRPC Proxy](grpc-proxy.md) +* [Customize Nginx Configuration](./customize-nginx-configuration.md) * [Changelog](../CHANGELOG.md) * [Benchmark](benchmark.md) * [Code Style](../CODE_STYLE.md) diff --git a/doc/zh-cn/README.md b/doc/zh-cn/README.md index 6a2e7af7b351..5d83b0bdec5d 100644 --- a/doc/zh-cn/README.md +++ b/doc/zh-cn/README.md @@ -30,6 +30,7 @@ * [独立运行模型](stand-alone.md): 支持从本地 yaml 格式的配置文件启动,更适合 Kubernetes(k8s) 体系。 * [TCP/UDP 动态代理](stream-proxy.md) * [gRPC 代理](grpc-proxy.md) +* [自定义 Nginx 配置](./customize-nginx-configuration.md) * [变更日志](../../CHANGELOG_CN.md) * [压力测试](benchmark.md) * [代码风格](../../CODE_STYLE_CN.md)