#!/usr/bin/env bash set -euo pipefail source ./lib.sh log "Nginx konfigurieren …" # ── Flags/Umgebung (vom Bootstrap gesetzt; hier Fallbacks) ──────────────── DEV_MODE="${DEV_MODE:-0}" # 1 = DEV (Vite-Proxy aktiv), 0 = PROD PROXY_MODE="${PROXY_MODE:-0}" # 1 = NPM/Proxy davor, Backend spricht nur HTTP:80 NPM_IP="${NPM_IP:-}" # z.B. 10.10.20.20 # Erwartet vom Bootstrap/Installer exportiert: : "${UI_HOST:?UI_HOST fehlt}" : "${WEBMAIL_HOST:?WEBMAIL_HOST fehlt}" : "${APP_DIR:?APP_DIR fehlt}" ACME_ROOT="/var/www/letsencrypt" install -d -m 0755 "$ACME_ROOT" # Default-Sites entfernen (verhindert doppelten default_server) rm -f /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default || true # HTTP/2-Unterstützung erkennen NGINX_HTTP2_SUFFIX="" if nginx -V 2>&1 | grep -q http_v2; then NGINX_HTTP2_SUFFIX=" http2" fi # PHP-FPM Socket/TCP finden → fastcgi_pass bauen detect_php_fpm_sock(){ for v in 8.3 8.2 8.1 8.0 7.4; do s="/run/php/php${v}-fpm.sock" [[ -S "$s" ]] && { echo "unix:${s}"; return; } done [[ -S "/run/php/php-fpm.sock" ]] && { echo "unix:/run/php/php-fpm.sock"; return; } echo "127.0.0.1:9000" } PHP_FPM_TARGET="$(detect_php_fpm_sock)" if [[ "$PHP_FPM_TARGET" == unix:* ]]; then FASTCGI_PASS="fastcgi_pass ${PHP_FPM_TARGET};" else FASTCGI_PASS="fastcgi_pass ${PHP_FPM_TARGET};" fi # ── Builder 1: HTTP-only (Proxy-Mode: TLS endet im NPM) ─────────────────── ## $1=host, $2=outfile #build_site_http_only(){ # local host="$1" outfile="$2" # # local def="" # [[ "${DEV_MODE}" = "1" ]] && def=" default_server" # [[ -z "${host}" || "${host}" = "_" ]] && host="_" # # cat > "$outfile" <> "$outfile" <<'CONF' # # DEV: Vite-Proxy (HMR) # location ^~ /@vite/ { proxy_pass http://127.0.0.1:5173/@vite/; proxy_set_header Host $host; } # location ^~ /node_modules/ { proxy_pass http://127.0.0.1:5173/node_modules/; proxy_set_header Host $host; } # location ^~ /resources/ { proxy_pass http://127.0.0.1:5173/resources/; proxy_set_header Host $host; } #CONF # fi # # echo "}" >> "$outfile" #} build_site_http_only(){ local host="$1" outfile="$2" # DEV: IP-Zugriff ohne Hostname → default_server + server_name _ local def="" if [[ "${DEV_MODE}" = "1" ]]; then def=" default_server" host="_" fi [[ -z "${host}" || "${host}" = "_" ]] && host="_" cat > "$outfile" <> "$outfile" <<'CONF' # DEV: Vite-Proxy (HMR) location ^~ /@vite/ { proxy_pass http://127.0.0.1:5173/@vite/; proxy_set_header Host $host; } location ^~ /node_modules/ { proxy_pass http://127.0.0.1:5173/node_modules/; proxy_set_header Host $host; } location ^~ /resources/ { proxy_pass http://127.0.0.1:5173/resources/; proxy_set_header Host $host; } CONF fi echo "}" >> "$outfile" } # ── Builder 2: 80→443 Redirect + 443/TLS (Live-Server) ──────────────────── # $1=host, $2=cert_dir (/etc/ssl/ui | /etc/ssl/webmail), $3=outfile build_site_tls(){ local host="$1" cert_dir="$2" outfile="$3" local cert="${cert_dir}/fullchain.pem" local key="${cert_dir}/privkey.pem" cat > "$outfile" <> "$outfile" <<'CONF' # DEV: Vite-Proxy location ^~ /@vite/ { proxy_pass http://127.0.0.1:5173/@vite/; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto https; } location ^~ /node_modules/ { proxy_pass http://127.0.0.1:5173/node_modules/; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto https; } location ^~ /resources/ { proxy_pass http://127.0.0.1:5173/resources/; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto https; } CONF fi echo "}" >> "$outfile" } build_site_acme_only(){ local host="$1" outfile="$2" cat > "$outfile" < /etc/nginx/conf.d/realip.conf </dev/null 2>&1 || true systemctl reload nginx || true else die "nginx -t fehlgeschlagen – siehe /var/log/nginx/*.log" fi