From 06de81b61ba1175ea2e8d20e64bbbb62526ee273 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 30 Jun 2025 03:29:43 -0400 Subject: [PATCH] refactor: allow custom nginx command to be set as an nginx property This avoids adding a new environment variable in favor of a script that can be called both globally as well as on a per-app basis. --- docs/networking/proxies/nginx.md | 1 + plugins/nginx-vhosts/internal-functions | 20 ++++++++- plugins/nginx-vhosts/nginx_vhosts.go | 43 +++++++++++++------ .../src/nginx-property/nginx-property.go | 6 +++ plugins/nginx-vhosts/subcommands/set | 6 +-- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/docs/networking/proxies/nginx.md b/docs/networking/proxies/nginx.md index ed1d64d47..bd6b5d92a 100644 --- a/docs/networking/proxies/nginx.md +++ b/docs/networking/proxies/nginx.md @@ -332,6 +332,7 @@ Changing these value globally or on a per-app basis will require rebuilding the | keepalive-timeout | `75s` | string | Timeout (with units) during which a keep-alive client connection will stay open on the server side | | lingering-timeout | `5s` | string | Timeout (with units) is the maximum waiting time for more client data to arrive | | nginx-conf-sigil-path | `nginx.conf.sigil` | string | Path in the repository to the `nginx.conf.sigil` file | +| nginx-service-command | | string | Full path to an executable script or binary to invoke instead of the system's init process when interacting with nginx | | proxy-buffer-size | `8k` (# is os pagesize) | string | Size of the buffer used for reading the first part of proxied server response | | proxy-buffering | `on` | string | Enables or disables buffering of responses from the proxied server | | proxy-buffers | `8 8k` | string | Number and size of the buffers used for reading the proxied server response, for a single connection | diff --git a/plugins/nginx-vhosts/internal-functions b/plugins/nginx-vhosts/internal-functions index 5485c345a..1950e199e 100755 --- a/plugins/nginx-vhosts/internal-functions +++ b/plugins/nginx-vhosts/internal-functions @@ -327,6 +327,21 @@ fn-nginx-global-nginx-conf-sigil-path() { fn-get-property --global "nginx-conf-sigil-path" } +fn-nginx-nginx-service-command() { + declare APP="$1" + fn-get-property --app "$APP" "nginx-service-command" +} + +fn-nginx-computed-nginx-service-command() { + declare APP="$1" + fn-get-property --computed --app "$APP" "nginx-service-command" +} + +fn-nginx-global-nginx-service-command() { + declare APP="$1" + fn-get-property --global "nginx-service-command" +} + fn-nginx-proxy-buffer-size() { declare desc="get the configured proxy buffer size" declare APP="$1" @@ -730,8 +745,9 @@ fn-nginx-vhosts-nginx-init-cmd() { declare desc="start nginx for given distros" declare CMD="$1" - if [[ -n "$DOKKU_NGINX_CUSTOM_CMD" ]]; then - $DOKKU_NGINX_CUSTOM_CMD $CMD + local NGINX_SERVICE_COMMAND="$(fn-nginx-computed-nginx-service-command "$APP")" + if [[ -n "$NGINX_SERVICE_COMMAND" ]]; then + "$NGINX_SERVICE_COMMAND" $CMD return fi diff --git a/plugins/nginx-vhosts/nginx_vhosts.go b/plugins/nginx-vhosts/nginx_vhosts.go index 4d7d17ac6..8c25ac1a7 100644 --- a/plugins/nginx-vhosts/nginx_vhosts.go +++ b/plugins/nginx-vhosts/nginx_vhosts.go @@ -234,6 +234,36 @@ func AppNginxConfSigilPath(appName string) string { return common.PropertyGet("nginx", appName, "nginx-conf-sigil-path") } +func ComputedNginxConfSigilPath(appName string) string { + appValue := AppNginxConfSigilPath(appName) + if appValue != "" { + return appValue + } + + return GlobalNginxConfSigilPath() +} + +func GlobalNginxConfSigilPath() string { + return common.PropertyGetDefault("nginx", "--global", "nginx-conf-sigil-path", "nginx.conf.sigil") +} + +func AppNginxServiceCommand(appName string) string { + return common.PropertyGet("nginx", appName, "nginx-service-command") +} + +func ComputedNginxServiceCommand(appName string) string { + appValue := AppNginxServiceCommand(appName) + if appValue != "" { + return appValue + } + + return GlobalNginxServiceCommand() +} + +func GlobalNginxServiceCommand() string { + return common.PropertyGetDefault("nginx", "--global", "nginx-service-command", "") +} + func AppKeepaliveTimeout(appName string) string { return common.PropertyGet("nginx", appName, "keepalive-timeout") } @@ -268,19 +298,6 @@ func GlobalLingeringTimeout() string { return common.PropertyGetDefault("nginx", "--global", "lingering-timeout", "5s") } -func ComputedNginxConfSigilPath(appName string) string { - appValue := AppNginxConfSigilPath(appName) - if appValue != "" { - return appValue - } - - return GlobalNginxConfSigilPath() -} - -func GlobalNginxConfSigilPath() string { - return common.PropertyGetDefault("nginx", "--global", "nginx-conf-sigil-path", "nginx.conf.sigil") -} - func AppProxyBufferSize(appName string) string { return common.PropertyGet("nginx", appName, "proxy-buffer-size") } diff --git a/plugins/nginx-vhosts/src/nginx-property/nginx-property.go b/plugins/nginx-vhosts/src/nginx-property/nginx-property.go index 6270d97c2..47c3bbd49 100644 --- a/plugins/nginx-vhosts/src/nginx-property/nginx-property.go +++ b/plugins/nginx-vhosts/src/nginx-property/nginx-property.go @@ -67,6 +67,8 @@ func appValue(appName string, property string) string { value = nginx_vhosts.AppLingeringTimeout(appName) case "nginx-conf-sigil-path": value = nginx_vhosts.AppNginxConfSigilPath(appName) + case "nginx-service-command": + value = nginx_vhosts.AppNginxServiceCommand(appName) case "proxy-buffer-size": value = nginx_vhosts.AppProxyBufferSize(appName) case "proxy-buffering": @@ -133,6 +135,8 @@ func computedValue(appName string, property string) string { value = nginx_vhosts.ComputedLingeringTimeout(appName) case "nginx-conf-sigil-path": value = nginx_vhosts.ComputedNginxConfSigilPath(appName) + case "nginx-service-command": + value = nginx_vhosts.ComputedNginxServiceCommand(appName) case "proxy-buffer-size": value = nginx_vhosts.ComputedProxyBufferSize(appName) case "proxy-buffering": @@ -199,6 +203,8 @@ func globalValue(appName string, property string) string { value = nginx_vhosts.ComputedLingeringTimeout(appName) case "nginx-conf-sigil-path": value = nginx_vhosts.GlobalNginxConfSigilPath() + case "nginx-service-command": + value = nginx_vhosts.GlobalNginxServiceCommand() case "proxy-buffer-size": value = nginx_vhosts.GlobalProxyBufferSize() case "proxy-buffering": diff --git a/plugins/nginx-vhosts/subcommands/set b/plugins/nginx-vhosts/subcommands/set index 49e2e541b..12cfaaab5 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-format" "access-log-path" "bind-address-ipv4" "bind-address-ipv6" "client-body-timeout" "client-header-timeout" "client-max-body-size" "disable-custom-config" "error-log-path" "hsts" "hsts-include-subdomains" "hsts-preload" "hsts-max-age" "keepalive-timeout" "lingering-timeout" "nginx-conf-sigil-path" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size" "proxy-connect-timeout" "proxy-read-timeout" "proxy-send-timeout" "send-timeout" "underscore-in-headers" "x-forwarded-for-value" "x-forwarded-port-value" "x-forwarded-proto-value" "x-forwarded-ssl") - local GLOBAL_KEYS=("access-log-format" "access-log-path" "bind-address-ipv4" "bind-address-ipv6" "client-body-timeout" "client-header-timeout" "client-max-body-size" "disable-custom-config" "error-log-path" "hsts-include-subdomains" "hsts-max-age" "hsts-preload" "hsts" "keepalive-timeout" "lingering-timeout" "nginx-conf-sigil-path" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size" "proxy-connect-timeout" "proxy-read-timeout" "proxy-send-timeout" "send-timeout" "underscore-in-headers" "x-forwarded-for-value" "x-forwarded-port-value" "x-forwarded-proto-value" "x-forwarded-ssl") + local VALID_KEYS=("access-log-format" "access-log-path" "bind-address-ipv4" "bind-address-ipv6" "client-body-timeout" "client-header-timeout" "client-max-body-size" "disable-custom-config" "error-log-path" "hsts" "hsts-include-subdomains" "hsts-preload" "hsts-max-age" "keepalive-timeout" "lingering-timeout" "nginx-conf-sigil-path" "nginx-service-command" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size" "proxy-connect-timeout" "proxy-read-timeout" "proxy-send-timeout" "send-timeout" "underscore-in-headers" "x-forwarded-for-value" "x-forwarded-port-value" "x-forwarded-proto-value" "x-forwarded-ssl") + local GLOBAL_KEYS=("access-log-format" "access-log-path" "bind-address-ipv4" "bind-address-ipv6" "client-body-timeout" "client-header-timeout" "client-max-body-size" "disable-custom-config" "error-log-path" "hsts-include-subdomains" "hsts-max-age" "hsts-preload" "hsts" "keepalive-timeout" "lingering-timeout" "nginx-conf-sigil-path" "nginx-service-command" "proxy-buffer-size" "proxy-buffering" "proxy-buffers" "proxy-busy-buffers-size" "proxy-connect-timeout" "proxy-read-timeout" "proxy-send-timeout" "send-timeout" "underscore-in-headers" "x-forwarded-for-value" "x-forwarded-port-value" "x-forwarded-proto-value" "x-forwarded-ssl") [[ -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-format, access-log-path, bind-address-ipv4, bind-address-ipv6, client-body-timeout, client-header-timeout, client-max-body-size, disable-custom-config, error-log-path, hsts, hsts-include-subdomains, hsts-preload, hsts-max-age, keepalive-timeout, lingering-timeout, nginx-conf-sigil-path, proxy-buffer-size, proxy-buffering, proxy-buffers, proxy-busy-buffers-size, proxy-connect-timeout, proxy-read-timeout, proxy-send-timeout, send-timeout, underscore-in-headers, x-forwarded-for-value, x-forwarded-port-value, x-forwarded-proto-value, x-forwarded-ssl" + dokku_log_fail "Invalid key specified, valid keys include: access-log-format, access-log-path, bind-address-ipv4, bind-address-ipv6, client-body-timeout, client-header-timeout, client-max-body-size, disable-custom-config, error-log-path, hsts, hsts-include-subdomains, hsts-preload, hsts-max-age, keepalive-timeout, lingering-timeout, nginx-conf-sigil-path, nginx-service-command, proxy-buffer-size, proxy-buffering, proxy-buffers, proxy-busy-buffers-size, proxy-connect-timeout, proxy-read-timeout, proxy-send-timeout, send-timeout, underscore-in-headers, x-forwarded-for-value, x-forwarded-port-value, x-forwarded-proto-value, x-forwarded-ssl" fi if ! fn-in-array "$KEY" "${GLOBAL_KEYS[@]}"; then