96 lines
4.4 KiB
Bash
96 lines
4.4 KiB
Bash
#!/bin/sh
|
||
# Entrypoint-скрипт контейнера (кладётся в /docker-entrypoint.d/ образа nginx,
|
||
# выполняется официозным entrypoint до старта nginx).
|
||
#
|
||
# Генерирует из переменных окружения:
|
||
# /usr/share/nginx/html/config.js — runtime-конфигурацию SPA (см. src/env.ts)
|
||
# /etc/nginx/conf.d/default.conf — SPA-роутинг + проксирование /api и /oauth2
|
||
#
|
||
# Переменные окружения:
|
||
# BACKEND_URL URL бэкенда (API + авторизация email-кодом) (по умолч. http://localhost:8080)
|
||
# API_BASE_URL база API в SPA (по умолчанию /api — через nginx)
|
||
# BASE_PATH базовый путь SPA (по умолчанию /admin/; должен
|
||
# совпадать со сборкой образа)
|
||
set -e
|
||
|
||
: "${BACKEND_URL:=http://app:8080}"
|
||
: "${API_BASE_URL:=/api}"
|
||
: "${BASE_PATH:=/admin/}"
|
||
|
||
# Схема и host:port из BACKEND_URL: nginx резолвит апстрим в РАНТАЙМЕ (через
|
||
# переменную + docker DNS), иначе отсутствие хоста на старте роняет nginx
|
||
# («host not found in upstream»), например при автономном запуске без стека back.
|
||
BACKEND_SCHEME=$(printf '%s' "$BACKEND_URL" | sed -nE 's#^(https?)://.*#\1#p')
|
||
BACKEND_HOST_PORT=$(printf '%s' "$BACKEND_URL" | sed -E 's#^[a-z]+://##; s#/$##')
|
||
case "$BACKEND_SCHEME" in http|https) ;; *) BACKEND_SCHEME=http; BACKEND_HOST_PORT="$BACKEND_URL";; esac
|
||
|
||
echo "app-config: backend=$BACKEND_URL apiBase=$API_BASE_URL base=$BASE_PATH"
|
||
|
||
cat > /usr/share/nginx/html/config.js <<EOF
|
||
window.__APP_CONFIG__ = {
|
||
apiBaseUrl: "${API_BASE_URL}"
|
||
};
|
||
EOF
|
||
|
||
# В nginx.conf экранируем $-переменные nginx от подстановки шеллом.
|
||
# BASE_PATH должен совпадать с базой, использованной при сборке образа
|
||
# (ARG BASE_PATH в Dockerfile): ассеты SPA запекаются с этим префиксом.
|
||
cat > /etc/nginx/conf.d/default.conf <<EOF
|
||
server {
|
||
listen 80;
|
||
server_name _;
|
||
absolute_redirect off;
|
||
|
||
# Docker embedded DNS: резолвим апстрим-бэкенд в рантайме (см. \$gorodar_backend)
|
||
resolver 127.0.0.11 valid=10s ipv6=off;
|
||
|
||
root /usr/share/nginx/html;
|
||
index index.html;
|
||
|
||
# Корень → редирект в приложение.
|
||
location = / {
|
||
return 302 ${BASE_PATH};
|
||
}
|
||
|
||
# Runtime-конфиг не кэшировать — он зависит от окружения контейнера.
|
||
location = ${BASE_PATH}config.js {
|
||
alias /usr/share/nginx/html/config.js;
|
||
add_header Cache-Control "no-store";
|
||
}
|
||
|
||
# SPA под базовым путём (/admin/): статика + fallback на index.html.
|
||
location ${BASE_PATH} {
|
||
alias /usr/share/nginx/html/;
|
||
try_files \$uri ${BASE_PATH}index.html;
|
||
}
|
||
|
||
# Бэкенд: API + собственная авторизация (/oauth2/*, /.well-known/*).
|
||
# Same-origin — CORS не нужен. Лимит 25m — чуть выше multipart-лимита бэкенда (22MB).
|
||
location /api/ {
|
||
set \$gorodar_backend "${BACKEND_SCHEME}://${BACKEND_HOST_PORT}";
|
||
proxy_pass \$gorodar_backend;
|
||
proxy_set_header Host \$host;
|
||
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;
|
||
client_max_body_size 25m;
|
||
}
|
||
location /oauth2/ {
|
||
set \$gorodar_backend "${BACKEND_SCHEME}://${BACKEND_HOST_PORT}";
|
||
proxy_pass \$gorodar_backend;
|
||
proxy_set_header Host \$host;
|
||
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;
|
||
}
|
||
location /.well-known/ {
|
||
set \$gorodar_backend "${BACKEND_SCHEME}://${BACKEND_HOST_PORT}";
|
||
proxy_pass \$gorodar_backend;
|
||
proxy_set_header Host \$host;
|
||
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;
|
||
}
|
||
}
|
||
EOF
|