Set backend host header for frontend proxy
Some checks failed
CI/CD Pipeline / Quality Gate (push) Failing after 8m28s
CI/CD Pipeline / Build and Deploy Dev via Compose (push) Has been skipped

This commit is contained in:
2026-06-21 11:08:47 +02:00
parent 77441df0cb
commit f43f2055e3
5 changed files with 27 additions and 5 deletions

View File

@@ -6,5 +6,6 @@ services:
restart: unless-stopped restart: unless-stopped
environment: environment:
BACKEND_UPSTREAM: ${BACKEND_UPSTREAM:?BACKEND_UPSTREAM is required} BACKEND_UPSTREAM: ${BACKEND_UPSTREAM:?BACKEND_UPSTREAM is required}
BACKEND_HOST_HEADER: ${BACKEND_HOST_HEADER:?BACKEND_HOST_HEADER is required}
ports: ports:
- "${FRONTEND_BIND_HOST:-10.10.0.124}:${FRONTEND_PORT:-18011}:80" - "${FRONTEND_BIND_HOST:-10.10.0.124}:${FRONTEND_PORT:-18011}:80"

View File

@@ -7,6 +7,7 @@ ENV_FILE="${ENV_FILE:-$APP_DIR/.env}"
IMAGE_ENV_FILE="${IMAGE_ENV_FILE:-$APP_DIR/releases/current.env}" IMAGE_ENV_FILE="${IMAGE_ENV_FILE:-$APP_DIR/releases/current.env}"
HEALTH_URL="${HEALTH_URL:-http://10.10.0.124:18011/}" HEALTH_URL="${HEALTH_URL:-http://10.10.0.124:18011/}"
DEFAULT_BACKEND_UPSTREAM="${DEFAULT_BACKEND_UPSTREAM:-http://10.10.0.124:18010}" 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:-}}" ANAL_FRONT_WEB_IMAGE="${1:-${ANAL_FRONT_WEB_IMAGE:-}}"
if [[ -z "$ANAL_FRONT_WEB_IMAGE" ]]; then if [[ -z "$ANAL_FRONT_WEB_IMAGE" ]]; then
@@ -20,6 +21,7 @@ if [[ ! -f "$ENV_FILE" ]]; then
umask 077 umask 077
cat >"$ENV_FILE" <<EOF cat >"$ENV_FILE" <<EOF
BACKEND_UPSTREAM=$DEFAULT_BACKEND_UPSTREAM BACKEND_UPSTREAM=$DEFAULT_BACKEND_UPSTREAM
BACKEND_HOST_HEADER=$DEFAULT_BACKEND_HOST_HEADER
FRONTEND_BIND_HOST=10.10.0.124 FRONTEND_BIND_HOST=10.10.0.124
FRONTEND_PORT=18011 FRONTEND_PORT=18011
EOF EOF
@@ -33,21 +35,37 @@ else
if [[ -z "$desired_backend_upstream" || "$desired_backend_upstream" == "https://anal-back.dev.nii-ecos.ru" ]]; then if [[ -z "$desired_backend_upstream" || "$desired_backend_upstream" == "https://anal-back.dev.nii-ecos.ru" ]]; then
desired_backend_upstream="$DEFAULT_BACKEND_UPSTREAM" desired_backend_upstream="$DEFAULT_BACKEND_UPSTREAM"
fi fi
current_backend_host_header="$(
awk -F= '/^BACKEND_HOST_HEADER=/{sub(/^BACKEND_HOST_HEADER=/, ""); print; exit}' "$ENV_FILE"
)"
desired_backend_host_header="$current_backend_host_header"
if [[ -z "$desired_backend_host_header" || "$desired_backend_host_header" == "anal-back.dev.nii-ecos.ru" ]]; then
desired_backend_host_header="$DEFAULT_BACKEND_HOST_HEADER"
fi
tmp_env_file="$(mktemp)" tmp_env_file="$(mktemp)"
backend_line_written=0 backend_line_written=0
backend_host_header_written=0
while IFS= read -r line || [[ -n "$line" ]]; do while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == BACKEND_UPSTREAM=* ]]; then if [[ "$line" == BACKEND_UPSTREAM=* ]]; then
printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file" printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file"
backend_line_written=1 backend_line_written=1
continue continue
fi 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" printf '%s\n' "$line" >>"$tmp_env_file"
done <"$ENV_FILE" done <"$ENV_FILE"
if [[ "$backend_line_written" -eq 0 ]]; then if [[ "$backend_line_written" -eq 0 ]]; then
printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file" printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file"
fi 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" mv "$tmp_env_file" "$ENV_FILE"
chmod 0600 "$ENV_FILE" chmod 0600 "$ENV_FILE"

View File

@@ -6,6 +6,7 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
environment: environment:
BACKEND_UPSTREAM: '${BACKEND_UPSTREAM:-https://anal-back.dev.nii-ecos.ru}' BACKEND_UPSTREAM: '${BACKEND_UPSTREAM:-https://anal-back.dev.nii-ecos.ru}'
BACKEND_HOST_HEADER: '${BACKEND_HOST_HEADER:-anal-back.dev.nii-ecos.ru}'
ports: ports:
- '${FRONTEND_PORT:-8080}:80' - '${FRONTEND_PORT:-8080}:80'
restart: unless-stopped restart: unless-stopped

View File

@@ -2,11 +2,13 @@
set -eu set -eu
: "${BACKEND_UPSTREAM:?BACKEND_UPSTREAM is required (example: http://analytical-panel-backend:8000)}" : "${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%/}" BACKEND_UPSTREAM="${BACKEND_UPSTREAM%/}"
export 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/templates/default.conf.template \
> /etc/nginx/conf.d/default.conf > /etc/nginx/conf.d/default.conf

View File

@@ -25,7 +25,7 @@ server {
location = /api { location = /api {
proxy_pass ${BACKEND_UPSTREAM}/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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
@@ -33,7 +33,7 @@ server {
location /api/ { location /api/ {
proxy_pass ${BACKEND_UPSTREAM}/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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
@@ -41,7 +41,7 @@ server {
location = /health { location = /health {
proxy_pass ${BACKEND_UPSTREAM}/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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
@@ -49,7 +49,7 @@ server {
location /health/ { location /health/ {
proxy_pass ${BACKEND_UPSTREAM}/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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;