From 26cfbd07cbd66c1d0cfa8d12ca94d61572cdadea Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 12 Aug 2025 17:39:47 +0100 Subject: [PATCH] feat: Add server names hash tuning options to nginx configuration --- HelmChart/Public/oneuptime/templates/nginx.yaml | 8 ++++++++ HelmChart/Public/oneuptime/values.yaml | 3 +++ Nginx/default.conf.template | 5 +++++ Nginx/envsubst-on-templates.sh | 16 +++++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/HelmChart/Public/oneuptime/templates/nginx.yaml b/HelmChart/Public/oneuptime/templates/nginx.yaml index 3634a8e7ec..0ba8ba5f40 100644 --- a/HelmChart/Public/oneuptime/templates/nginx.yaml +++ b/HelmChart/Public/oneuptime/templates/nginx.yaml @@ -111,6 +111,14 @@ spec: value: {{ $.Values.nginx.listenAddress | quote }} - name: NGINX_LISTEN_OPTIONS value: {{ $.Values.nginx.listenOptions | quote }} + {{- if $.Values.nginx.serverNamesHash.bucketSize }} + - name: SERVER_NAMES_HASH_BUCKET_SIZE + value: {{ $.Values.nginx.serverNamesHash.bucketSize | quote }} + {{- end }} + {{- if $.Values.nginx.serverNamesHash.maxSize }} + - name: SERVER_NAMES_HASH_MAX_SIZE + value: {{ $.Values.nginx.serverNamesHash.maxSize | quote }} + {{- end }} - name: ONEUPTIME_HTTP_PORT value: {{ $.Values.nginx.ports.http | quote }} - name: PORT diff --git a/HelmChart/Public/oneuptime/values.yaml b/HelmChart/Public/oneuptime/values.yaml index 94588aa3fe..930ad0131d 100644 --- a/HelmChart/Public/oneuptime/values.yaml +++ b/HelmChart/Public/oneuptime/values.yaml @@ -43,6 +43,9 @@ nginx: disableAutoscaler: false listenAddress: "" listenOptions: "" + serverNamesHash: + bucketSize: "" + maxSize: "" ports: http: 80 https: 443 diff --git a/Nginx/default.conf.template b/Nginx/default.conf.template index 42166bcbf4..170b97e76a 100644 --- a/Nginx/default.conf.template +++ b/Nginx/default.conf.template @@ -1,3 +1,8 @@ +## Hash tuning for many/long server names. Can be overridden via env vars. +## Defaults are provided by envsubst-on-templates.sh if not set. +server_names_hash_bucket_size ${SERVER_NAMES_HASH_BUCKET_SIZE}; +server_names_hash_max_size ${SERVER_NAMES_HASH_MAX_SIZE}; + upstream accounts { server ${SERVER_ACCOUNTS_HOSTNAME}:${ACCOUNTS_PORT} weight=10 max_fails=3 fail_timeout=30s; } diff --git a/Nginx/envsubst-on-templates.sh b/Nginx/envsubst-on-templates.sh index 3b6bb54479..efd884070b 100644 --- a/Nginx/envsubst-on-templates.sh +++ b/Nginx/envsubst-on-templates.sh @@ -25,8 +25,22 @@ auto_envsubst() { subdir=$(dirname "$relative_path") # create a subdirectory where the template file exists mkdir -p "$output_dir/$subdir" + + # Make a temp copy to allow conditional line removal + tmpfile=$(mktemp) + cp "$template" "$tmpfile" + + # If hash tuning envs are not set, remove their lines from the template + if [ -z "${SERVER_NAMES_HASH_BUCKET_SIZE}" ]; then + sed -i '/^[[:space:]]*server_names_hash_bucket_size[[:space:]]/d' "$tmpfile" + fi + if [ -z "${SERVER_NAMES_HASH_MAX_SIZE}" ]; then + sed -i '/^[[:space:]]*server_names_hash_max_size[[:space:]]/d' "$tmpfile" + fi + echo "$ME: Running envsubst on $template to $output_path" - envsubst "$defined_envs" < "$template" > "$output_path" + envsubst "$defined_envs" < "$tmpfile" > "$output_path" + rm -f "$tmpfile" done }