Set backend host header for frontend proxy
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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" <<EOF
|
||||
BACKEND_UPSTREAM=$DEFAULT_BACKEND_UPSTREAM
|
||||
BACKEND_HOST_HEADER=$DEFAULT_BACKEND_HOST_HEADER
|
||||
FRONTEND_BIND_HOST=10.10.0.124
|
||||
FRONTEND_PORT=18011
|
||||
EOF
|
||||
@@ -33,21 +35,37 @@ else
|
||||
if [[ -z "$desired_backend_upstream" || "$desired_backend_upstream" == "https://anal-back.dev.nii-ecos.ru" ]]; then
|
||||
desired_backend_upstream="$DEFAULT_BACKEND_UPSTREAM"
|
||||
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)"
|
||||
backend_line_written=0
|
||||
backend_host_header_written=0
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
if [[ "$line" == BACKEND_UPSTREAM=* ]]; then
|
||||
printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user