events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log error; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; gzip on; gzip_vary on; gzip_min_length 1024; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/atom+xml image/svg+xml; # Rate limiting limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s; limit_req_zone $binary_remote_addr zone=static:10m rate=30r/s; # Use resolver to dynamically resolve backend hostname # This allows backend container to start after frontend resolver 127.0.0.11 valid=10s; server { listen 80; server_name _; root /usr/share/nginx/html; index index.html; # Set backend upstream address (resolved dynamically) set $backend_backend "backend:3000"; # Rate limited API proxy location /api/ { limit_req zone=api burst=20 nodelay; proxy_pass http://$backend_backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; 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; proxy_cache_bypass $http_upgrade; proxy_read_timeout 86400; } # Hashed CSS files (cache-busted) location ~* ^/css/.*\.css$ { limit_req zone=static burst=50 nodelay; expires 1y; add_header Cache-Control "public, immutable"; } # Hashed JS files (cache-busted) location ~* ^/js/.*\.(js|mjs)$ { limit_req zone=static burst=50 nodelay; expires 1d; add_header Cache-Control "public, immutable"; } # Regular static files (non-hashed) location ~* \.(png|jpg|jpeg|gif|ico|svg|webp|woff|woff2)$ { limit_req zone=static burst=50 nodelay; expires 1y; add_header Cache-Control "public, immutable"; } # Version file endpoint location = /version.txt { access_log off; add_header Cache-Control "no-store, no-cache, must-revalidate"; add_header Pragma "no-cache"; add_header Expires "0"; } # Explicitly handle HTML files - don't fallback to index.html location ~* \.html$ { limit_req zone=static burst=10 nodelay; try_files $uri =404; } # Main application routes location / { limit_req zone=static burst=10 nodelay; try_files $uri $uri/ /index.html; } # Health check endpoint location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } # Error pages error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /404.html { root /usr/share/nginx/html; internal; } location = /50x.html { root /usr/share/nginx/html; internal; } } }