From f43f2055e3a0a461eb0b31fce3e4fe02e2a69fb0 Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchriakov Date: Sun, 21 Jun 2026 11:08:47 +0200 Subject: [PATCH] Set backend host header for frontend proxy --- deploy/anal-front/compose.yml | 1 + deploy/anal-front/deploy.sh | 18 ++++++++++++++++++ docker-compose.yml | 1 + docker/docker-entrypoint.sh | 4 +++- docker/nginx.conf.template | 8 ++++---- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/deploy/anal-front/compose.yml b/deploy/anal-front/compose.yml index b087a81..ba02802 100644 --- a/deploy/anal-front/compose.yml +++ b/deploy/anal-front/compose.yml @@ -6,5 +6,6 @@ services: restart: unless-stopped environment: BACKEND_UPSTREAM: ${BACKEND_UPSTREAM:?BACKEND_UPSTREAM is required} + BACKEND_HOST_HEADER: ${BACKEND_HOST_HEADER:?BACKEND_HOST_HEADER is required} ports: - "${FRONTEND_BIND_HOST:-10.10.0.124}:${FRONTEND_PORT:-18011}:80" diff --git a/deploy/anal-front/deploy.sh b/deploy/anal-front/deploy.sh index 36f0bef..1542566 100755 --- a/deploy/anal-front/deploy.sh +++ b/deploy/anal-front/deploy.sh @@ -7,6 +7,7 @@ ENV_FILE="${ENV_FILE:-$APP_DIR/.env}" IMAGE_ENV_FILE="${IMAGE_ENV_FILE:-$APP_DIR/releases/current.env}" HEALTH_URL="${HEALTH_URL:-http://10.10.0.124:18011/}" DEFAULT_BACKEND_UPSTREAM="${DEFAULT_BACKEND_UPSTREAM:-http://10.10.0.124:18010}" +DEFAULT_BACKEND_HOST_HEADER="${DEFAULT_BACKEND_HOST_HEADER:-10.10.0.124}" ANAL_FRONT_WEB_IMAGE="${1:-${ANAL_FRONT_WEB_IMAGE:-}}" if [[ -z "$ANAL_FRONT_WEB_IMAGE" ]]; then @@ -20,6 +21,7 @@ if [[ ! -f "$ENV_FILE" ]]; then umask 077 cat >"$ENV_FILE" <>"$tmp_env_file" backend_line_written=1 continue fi + if [[ "$line" == BACKEND_HOST_HEADER=* ]]; then + printf 'BACKEND_HOST_HEADER=%s\n' "$desired_backend_host_header" >>"$tmp_env_file" + backend_host_header_written=1 + continue + fi printf '%s\n' "$line" >>"$tmp_env_file" done <"$ENV_FILE" if [[ "$backend_line_written" -eq 0 ]]; then printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file" fi + if [[ "$backend_host_header_written" -eq 0 ]]; then + printf 'BACKEND_HOST_HEADER=%s\n' "$desired_backend_host_header" >>"$tmp_env_file" + fi mv "$tmp_env_file" "$ENV_FILE" chmod 0600 "$ENV_FILE" diff --git a/docker-compose.yml b/docker-compose.yml index 9905356..d6c15e3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: dockerfile: Dockerfile environment: BACKEND_UPSTREAM: '${BACKEND_UPSTREAM:-https://anal-back.dev.nii-ecos.ru}' + BACKEND_HOST_HEADER: '${BACKEND_HOST_HEADER:-anal-back.dev.nii-ecos.ru}' ports: - '${FRONTEND_PORT:-8080}:80' restart: unless-stopped diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index a6b4a24..d246b86 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -2,11 +2,13 @@ set -eu : "${BACKEND_UPSTREAM:?BACKEND_UPSTREAM is required (example: http://analytical-panel-backend:8000)}" +: "${BACKEND_HOST_HEADER:?BACKEND_HOST_HEADER is required (example: analytical-panel-backend)}" BACKEND_UPSTREAM="${BACKEND_UPSTREAM%/}" export BACKEND_UPSTREAM +export BACKEND_HOST_HEADER -envsubst '${BACKEND_UPSTREAM}' \ +envsubst '${BACKEND_UPSTREAM} ${BACKEND_HOST_HEADER}' \ < /etc/nginx/templates/default.conf.template \ > /etc/nginx/conf.d/default.conf diff --git a/docker/nginx.conf.template b/docker/nginx.conf.template index c842baf..869c68a 100644 --- a/docker/nginx.conf.template +++ b/docker/nginx.conf.template @@ -25,7 +25,7 @@ server { location = /api { proxy_pass ${BACKEND_UPSTREAM}/api; - proxy_set_header Host $host; + proxy_set_header Host ${BACKEND_HOST_HEADER}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; @@ -33,7 +33,7 @@ server { location /api/ { proxy_pass ${BACKEND_UPSTREAM}/api/; - proxy_set_header Host $host; + proxy_set_header Host ${BACKEND_HOST_HEADER}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; @@ -41,7 +41,7 @@ server { location = /health { proxy_pass ${BACKEND_UPSTREAM}/health/; - proxy_set_header Host $host; + proxy_set_header Host ${BACKEND_HOST_HEADER}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; @@ -49,7 +49,7 @@ server { location /health/ { proxy_pass ${BACKEND_UPSTREAM}/health/; - proxy_set_header Host $host; + proxy_set_header Host ${BACKEND_HOST_HEADER}; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;