Without a resolver directive, nginx resolves upstream hostnames once at
startup and caches the IP forever. When the API container restarts it gets
a new Docker-assigned IP, causing 502 Bad Gateway until nginx is reloaded.
Fix:
- Add 'resolver 127.0.0.11 valid=10s' (Docker embedded DNS)
- Use a variable in proxy_pass ('set \ api') so nginx
re-resolves on every request using the resolver above
- Variable proxy_pass passes the full request URI as-is, so /api/...
correctly maps to http://api:8000/api/... without double-pathing
65 lines
2.8 KiB
Plaintext
65 lines
2.8 KiB
Plaintext
server {
|
|
listen 80;
|
|
server_name _;
|
|
|
|
# Use Docker's embedded DNS resolver so nginx re-resolves upstream hostnames
|
|
# dynamically instead of caching IPs at startup (prevents 502 after container restarts).
|
|
resolver 127.0.0.11 valid=10s ipv6=off;
|
|
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
# Security headers (server-level -- inherited by all locations unless overridden)
|
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header X-XSS-Protection "0" always;
|
|
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
|
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
|
|
|
# CSP for React SPA with Tailwind CSS and Leaflet maps
|
|
# worker-src required for SRP key derivation Web Worker (Safari won't fall back to script-src)
|
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://*.tile.openstreetmap.org; font-src 'self'; connect-src 'self'; worker-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self';" always;
|
|
|
|
# Proxy API requests to the backend service
|
|
# The api container is reachable via Docker internal DNS as "api" on port 8000
|
|
location /api/ {
|
|
# Use a variable so nginx applies the resolver on every request.
|
|
# With a variable in proxy_pass, nginx passes the full request URI as-is
|
|
# (no path stripping), so the upstream receives /api/... correctly.
|
|
set $api_upstream api;
|
|
proxy_pass http://$api_upstream:8000;
|
|
proxy_http_version 1.1;
|
|
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;
|
|
# Disable buffering for streaming/SSE endpoints
|
|
proxy_buffering off;
|
|
proxy_read_timeout 300s;
|
|
# Hide nginx server-level CSP — backend sets its own CSP on API responses
|
|
proxy_hide_header Content-Security-Policy;
|
|
}
|
|
|
|
# Serve static assets with long cache headers
|
|
# Note: add_header in a location block clears parent-block headers,
|
|
# so we re-add the essential security header for static assets.
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
|
expires 1y;
|
|
add_header Cache-Control "public, immutable" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
try_files $uri =404;
|
|
}
|
|
|
|
# SPA routing: all other requests serve index.html
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
}
|
|
|
|
# Health check endpoint for container probes
|
|
location /nginx-health {
|
|
access_log off;
|
|
return 200 "OK\n";
|
|
add_header Content-Type text/plain;
|
|
}
|
|
}
|