049837f9d6
- Add resolver 127.0.0.11 directive for Docker's internal DNS - Use variable-based proxy_pass to force per-request DNS resolution - Fix 502 Bad Gateway error on root path in Docker Swarm deployments The issue was that nginx caches DNS lookups at startup, but in Docker Swarm where service IPs can change dynamically, this caused stale DNS entries leading to 502 errors for proxied requests. Bumps version to 2.2.3
139 lines
4.5 KiB
Nginx Configuration File
139 lines
4.5 KiB
Nginx Configuration File
server {
|
|
listen 80;
|
|
server_name localhost;
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
# Docker DNS resolver for dynamic service discovery (required for Swarm/Compose)
|
|
resolver 127.0.0.11 valid=10s ipv6=off;
|
|
resolver_timeout 5s;
|
|
|
|
# Allow larger file uploads (up to 100MB)
|
|
client_max_body_size 100M;
|
|
client_body_timeout 300s;
|
|
|
|
# Gzip compression
|
|
gzip on;
|
|
gzip_vary on;
|
|
gzip_min_length 1024;
|
|
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
|
|
|
# Security headers
|
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header X-XSS-Protection "1; mode=block" always;
|
|
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
|
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline' 'unsafe-eval'" always;
|
|
|
|
# Health check endpoint
|
|
location /health {
|
|
access_log off;
|
|
return 200 "healthy\n";
|
|
add_header Content-Type text/plain;
|
|
}
|
|
|
|
# Cache static assets
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
|
expires 1y;
|
|
add_header Cache-Control "public, immutable";
|
|
}
|
|
|
|
# Cache index.html with revalidation
|
|
location = /index.html {
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
|
add_header Pragma "no-cache";
|
|
add_header Expires "0";
|
|
}
|
|
|
|
# API proxy
|
|
location /api {
|
|
# Use variable to force DNS resolution per request (required for Docker Swarm)
|
|
set $backend_upstream backend;
|
|
proxy_pass http://$backend_upstream:3000;
|
|
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;
|
|
|
|
# Allow larger uploads for API endpoints
|
|
client_max_body_size 100M;
|
|
client_body_timeout 300s;
|
|
}
|
|
|
|
# Photo serving proxy
|
|
location /photos {
|
|
set $backend_upstream backend;
|
|
proxy_pass http://$backend_upstream:3000;
|
|
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;
|
|
|
|
# Cache photos
|
|
proxy_cache_valid 200 302 1d;
|
|
proxy_cache_valid 404 1m;
|
|
}
|
|
|
|
# Thumbnail serving proxy
|
|
location /thumbnails {
|
|
set $backend_upstream backend;
|
|
proxy_pass http://$backend_upstream:3000;
|
|
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;
|
|
|
|
# Cache thumbnails
|
|
proxy_cache_valid 200 302 7d;
|
|
proxy_cache_valid 404 1m;
|
|
}
|
|
|
|
# Uploads serving proxy (logos, favicons, watermarks)
|
|
# ^~ modifier stops regex matching, ensuring uploads are proxied not served locally
|
|
location ^~ /uploads {
|
|
set $backend_upstream backend;
|
|
proxy_pass http://$backend_upstream:3000;
|
|
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;
|
|
|
|
# Cache uploads
|
|
proxy_cache_valid 200 302 7d;
|
|
proxy_cache_valid 404 1m;
|
|
}
|
|
|
|
# Delegate root requests to backend for public landing page handling
|
|
location = / {
|
|
# Use variable to force DNS resolution per request (required for Docker Swarm)
|
|
set $backend_upstream backend;
|
|
proxy_pass http://$backend_upstream:3000/;
|
|
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_read_timeout 60s;
|
|
}
|
|
|
|
# SPA fallback
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
}
|
|
|
|
# Deny access to hidden files
|
|
location ~ /\. {
|
|
deny all;
|
|
}
|
|
}
|