From c0581a0e375a24f0fca77b9c01876573f2168bd7 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 30 Nov 2020 21:59:36 -0800 Subject: [PATCH 1/2] feat: add ability to change the access log format --- docs/configuration/nginx.md | 10 ++++ plugins/nginx-vhosts/command-functions | 1 + plugins/nginx-vhosts/functions | 9 ++++ plugins/nginx-vhosts/subcommands/set | 4 +- .../nginx-vhosts/templates/nginx.conf.sigil | 8 +-- tests/unit/nginx-vhosts_8.bats | 50 +++++++++++++++++++ 6 files changed, 76 insertions(+), 6 deletions(-) diff --git a/docs/configuration/nginx.md b/docs/configuration/nginx.md index 36ca66f07..706167bfb 100644 --- a/docs/configuration/nginx.md +++ b/docs/configuration/nginx.md @@ -114,6 +114,16 @@ dokku nginx:set node-js-app error-log-path In all cases, the nginx config must be regenerated after setting the above values. +### Changing log format + +> New as of 0.21.5 + +The format of the access log can be changed by calling the `nginx:set` command as follows: + +```shell +dokku nginx:set node-js-app access-log-format custom +``` + ### Specifying a read timeout > New as of 0.21.0 diff --git a/plugins/nginx-vhosts/command-functions b/plugins/nginx-vhosts/command-functions index b2ecf2dab..1fda16313 100755 --- a/plugins/nginx-vhosts/command-functions +++ b/plugins/nginx-vhosts/command-functions @@ -38,6 +38,7 @@ cmd-nginx-report-single() { fi verify_app_name "$APP" local flag_map=( + "--nginx-access-log-format: $(fn-nginx-access-log-format "$APP")" "--nginx-access-log-path: $(fn-nginx-access-log-path "$APP")" "--nginx-bind-address-ipv4: $(fn-plugin-property-get-default "nginx" "$APP" "bind-address-ipv4" "")" "--nginx-bind-address-ipv6: $(fn-plugin-property-get-default "nginx" "$APP" "bind-address-ipv6" "::")" diff --git a/plugins/nginx-vhosts/functions b/plugins/nginx-vhosts/functions index 1aeabc093..6af95592d 100755 --- a/plugins/nginx-vhosts/functions +++ b/plugins/nginx-vhosts/functions @@ -13,6 +13,13 @@ fn-nginx-log-root() { echo "$NGINX_LOG_ROOT" } +fn-nginx-access-log-format() { + declare desc="get the configured access log format" + declare APP="$1" + + fn-plugin-property-get-default "nginx" "$APP" "access-log-format" "" +} + fn-nginx-access-log-path() { declare desc="get the configured access log path" declare APP="$1" @@ -424,6 +431,7 @@ nginx_build_config() { PROXY_PORT_MAP=$(echo "$PROXY_PORT_MAP" | xargs) # trailing spaces mess up default template local NGINX_LOG_ROOT="$(fn-nginx-log-root)" + local NGINX_ACCESS_LOG_FORMAT="$(fn-nginx-access-log-format "$APP")" local NGINX_ACCESS_LOG_PATH="$(fn-nginx-access-log-path "$APP")" local NGINX_ERROR_LOG_PATH="$(fn-nginx-error-log-path "$APP")" local PROXY_READ_TIMEOUT="$(fn-nginx-proxy-read-timeout "$APP")" @@ -469,6 +477,7 @@ nginx_build_config() { TLS13_SUPPORTED="$TLS13_SUPPORTED" HTTP2_SUPPORTED="$HTTP2_SUPPORTED" NGINX_LOG_ROOT="$NGINX_LOG_ROOT" + NGINX_ACCESS_LOG_FORMAT="$NGINX_ACCESS_LOG_FORMAT" NGINX_ACCESS_LOG_PATH="$NGINX_ACCESS_LOG_PATH" NGINX_ERROR_LOG_PATH="$NGINX_ERROR_LOG_PATH" NGINX_BIND_ADDRESS_IP4="$NGINX_BIND_ADDRESS_IP4" diff --git a/plugins/nginx-vhosts/subcommands/set b/plugins/nginx-vhosts/subcommands/set index 0c7756713..c85f5ab22 100755 --- a/plugins/nginx-vhosts/subcommands/set +++ b/plugins/nginx-vhosts/subcommands/set @@ -9,13 +9,13 @@ cmd-nginx-set() { declare cmd="nginx:set" [[ "$1" == "$cmd" ]] && shift 1 declare APP="$1" KEY="$2" VALUE="$3" - local VALID_KEYS=("access-log-path" "bind-address-ipv4" "bind-address-ipv6" "disable-custom-config" "error-log-path" "hsts" "hsts-include-subdomains" "hsts-preload" "hsts-max-age" "proxy-read-timeout" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size") + local VALID_KEYS=("access-log-format" "access-log-path" "bind-address-ipv4" "bind-address-ipv6" "disable-custom-config" "error-log-path" "hsts" "hsts-include-subdomains" "hsts-preload" "hsts-max-age" "proxy-read-timeout" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size") [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on" [[ -z "$KEY" ]] && dokku_log_fail "No key specified" if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then - dokku_log_fail "Invalid key specified, valid keys include: access-log-path, bind-address-ipv4, bind-address-ipv6, disable-custom-config, error-log-path, hsts, hsts-include-subdomains, hsts-preload, hsts-max-age, proxy-read-timeout, proxy-buffer-size, proxy-buffering, proxy-buffers, proxy-busy-buffers-size" + dokku_log_fail "Invalid key specified, valid keys include: access-log-format, access-log-path, bind-address-ipv4, bind-address-ipv6, disable-custom-config, error-log-path, hsts, hsts-include-subdomains, hsts-preload, hsts-max-age, proxy-read-timeout, proxy-buffer-size, proxy-buffering, proxy-buffers, proxy-busy-buffers-size" fi if [[ -n "$VALUE" ]]; then diff --git a/plugins/nginx-vhosts/templates/nginx.conf.sigil b/plugins/nginx-vhosts/templates/nginx.conf.sigil index a79be1af0..a195a0a17 100644 --- a/plugins/nginx-vhosts/templates/nginx.conf.sigil +++ b/plugins/nginx-vhosts/templates/nginx.conf.sigil @@ -9,7 +9,7 @@ server { listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }}; listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }}; {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} - access_log {{ $.NGINX_ACCESS_LOG_PATH }}; + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; error_log {{ $.NGINX_ERROR_LOG_PATH }}; {{ if (and (eq $listen_port "80") ($.SSL_INUSE)) }} return 301 https://$host:{{ $.PROXY_SSL_PORT }}$request_uri; @@ -65,7 +65,7 @@ server { listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} ssl {{ if eq $.HTTP2_SUPPORTED "true" }}http2{{ else if eq $.SPDY_SUPPORTED "true" }}spdy{{ end }}; {{ if $.SSL_SERVER_NAME }}server_name {{ $.SSL_SERVER_NAME }}; {{ end }} {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} - access_log {{ $.NGINX_ACCESS_LOG_PATH }}; + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; error_log {{ $.NGINX_ERROR_LOG_PATH }}; ssl_certificate {{ $.APP_SSL_PATH }}/server.crt; @@ -133,7 +133,7 @@ server { listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }} http2; listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} http2; {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} - access_log {{ $.NGINX_ACCESS_LOG_PATH }}; + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; error_log {{ $.NGINX_ERROR_LOG_PATH }}; location / { grpc_pass grpc://{{ $.APP }}-{{ $upstream_port }}; @@ -147,7 +147,7 @@ server { listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }} ssl http2; listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} ssl http2; {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} - access_log {{ $.NGINX_ACCESS_LOG_PATH }}; + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; error_log {{ $.NGINX_ERROR_LOG_PATH }}; ssl_certificate {{ $.APP_SSL_PATH }}/server.crt; diff --git a/tests/unit/nginx-vhosts_8.bats b/tests/unit/nginx-vhosts_8.bats index 7b4f6accf..32a5b64ea 100644 --- a/tests/unit/nginx-vhosts_8.bats +++ b/tests/unit/nginx-vhosts_8.bats @@ -104,6 +104,56 @@ teardown() { assert_output_contains "off;" 0 } +@test "(nginx-vhosts) access-log-format" { + deploy_app + + run /bin/bash -c "dokku nginx:set $TEST_APP access-log-format combined" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:build-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:show-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "-access.log combined;" + + run /bin/bash -c "dokku nginx:set $TEST_APP access-log-path off" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:build-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:show-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "off;" + + run /bin/bash -c "dokku nginx:set $TEST_APP access-log-format" + run /bin/bash -c "dokku nginx:set $TEST_APP access-log-path" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:build-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:show-config $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "-access.log;" +} + @test "(nginx-vhosts) nginx:set proxy-read-timeout" { deploy_app From 889c11d84bd77edfca9b4df54ab9da1484f10b53 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 1 Dec 2020 11:08:21 -0500 Subject: [PATCH 2/2] docs: use correct version [ci skip] --- docs/configuration/nginx.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration/nginx.md b/docs/configuration/nginx.md index 706167bfb..328db0e51 100644 --- a/docs/configuration/nginx.md +++ b/docs/configuration/nginx.md @@ -116,7 +116,7 @@ In all cases, the nginx config must be regenerated after setting the above value ### Changing log format -> New as of 0.21.5 +> New as of 0.22.0 The format of the access log can be changed by calling the `nginx:set` command as follows: