# DoS backstop only — real per-client limiting happens in the backend (app/rate_limit.py), which # resolves the true client IP behind proxies. Behind a fronting proxy (e.g. NPM) every request # arrives from one address, so this zone is shared by ALL clients; keep it loose enough that a # handful of legitimate users entering gallery passwords can't lock the whole instance out. limit_req_zone $binary_remote_addr zone=auth:10m rate=60r/m; resolver 127.0.0.11 valid=30s ipv6=off; # The scheme the *client* used. This nginx only ever listens on :80, so `$scheme` is always "http" # here — but when TLS terminates one hop up (Nginx Proxy Manager, Caddy, Traefik) that proxy tells # us via X-Forwarded-Proto. Forwarding `$scheme` would overwrite that with "http" and the backend # would never flag the admin session cookie `Secure`. Trust the upstream header when it says https. map $http_x_forwarded_proto $fwd_proto { default $scheme; https https; } # Access log WITHOUT the query string: gallery JWTs travel as `?token=` on the media proxy, the # ZIP stream and the public WebSocket — the stock format's `$request` would write 12-hour bearer # tokens into the container log / any log shipper. log_format noquery '$remote_addr - $remote_user [$time_local] "$request_method $uri $server_protocol" ' '$status $body_bytes_sent "$http_referer" "$http_user_agent"'; server { listen 80; client_max_body_size 1m; access_log /var/log/nginx/access.log noquery; # `always` so the headers also ride error responses (404/413/5xx). NOTE: any location that # declares its own add_header drops ALL inherited ones (nginx inheritance is all-or-nothing) — # such locations below must repeat this trio. add_header X-Frame-Options DENY always; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy strict-origin-when-cross-origin always; # The app's Content-Security-Policy is set per-request (with a fresh nonce) by the Next.js proxy # (frontend/src/proxy.ts) and passed through on proxied responses — it can't live here because # nginx can't mint a nonce the app also knows. The /branding and /uploads static mounts below # keep their own stricter CSPs. (Tip: terminate TLS and add HSTS at your edge proxy — see README.) location /uploads/ { alias /data/uploads/; # max-age rides this add_header instead of `expires 30d`, which would emit a second, # competing Cache-Control line. No `always` on it: a 404 must not inherit a 30-day cache. add_header Cache-Control "public, max-age=2592000, immutable"; # Declaring add_header here drops the server-level headers — repeat the full trio. add_header X-Frame-Options DENY always; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy strict-origin-when-cross-origin always; # Originals of any accepted type (TIFF/PSD/RAW/video) are served from here: never let one # execute as a document, whatever a browser sniffs it as (same CSP as /branding). add_header Content-Security-Policy "script-src 'none'" always; } location /branding/ { alias /data/branding/; # Same as /uploads/ above — one Cache-Control, no `expires`. Not immutable: branding is # replaced in place, so a 7-day max-age is the whole staleness budget. add_header Cache-Control "public, max-age=604800"; # Declaring add_header here drops the server-level headers — repeat the full trio. add_header X-Frame-Options DENY always; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy strict-origin-when-cross-origin always; # `always` like the trio above: a branding 404 should carry the same guarantees as a hit. add_header Content-Security-Policy "script-src 'none'" always; } # Both the admin upload (/api/galleries/{id}/images) and the public client upload # (/api/public/g/{token}/images). Without the public path here it would fall through to the # `location /api/` block and inherit the 1 MB server default — 413-ing every real photo a # visitor uploads. The backend enforces the real per-tier size caps (300 MB images, 8 GB # PSD/PSB/TIFF, 2 GB video); 8g here is just the ceiling. A proxy in front (e.g. NPM) must allow # at least as much, or it 413s first. location ~ ^/api/(galleries/[^/]+|public/g/[^/]+)/images$ { client_max_body_size 8g; proxy_request_buffering off; set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; proxy_read_timeout 1800s; proxy_send_timeout 1800s; } # Full-instance backup & restore. The restore upload (POST .../restore) carries an entire # backup archive, and the download (GET .../backup/{id}/download) streams one back — both # would otherwise fall through to `location /api/` and inherit the 1 MB cap + 120s timeout, # 413-ing or truncating any real backup. 2g is just a ceiling; the work is also slow (DB # snapshot/swap + migration), so the timeouts are generous. See docs/backup-and-restore.md. location ~ ^/api/admin/settings/(backup|restore) { client_max_body_size 2g; proxy_request_buffering off; set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; proxy_read_timeout 1800s; proxy_send_timeout 1800s; } # Admin image uploads that are NOT photo originals: gallery header/cover, instance logo, and # watermark. They'd otherwise fall through to `location /api/` and inherit the 1 MB server # default — 413-ing any header/cover/logo over 1 MB before it reaches the backend (which allows # 100 MB for header/cover so photographers can drop full-res developed JPEGs; the server bounds # them to 3840 px on store). 110m is just a ceiling above that backend cap. See # docs/architecture/header-cover-uploads-and-og-image-sizing.md. location ~ ^/api/(galleries/[^/]+/(header-image|cover-image|watermark)|admin/settings/logo) { client_max_body_size 110m; set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; } location ~ ^/api/(auth/login|public/g/.+/auth)$ { limit_req zone=auth burst=30 nodelay; set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; } # Archive downloads: the public streaming ZIP (built on the fly, exact Content-Length) and the # admin/public job downloads. Default proxy buffering would spool each multi-GB archive into # nginx's temp dir on the container's overlay disk before the first byte reaches the client; # stream it through instead, and give a big archive time to finish. location ~ ^/api/(public/g/[^/]+/zip/(stream|[^/]+/download)|galleries/[^/]+/export/zip/[^/]+/download)$ { set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; proxy_buffering off; proxy_read_timeout 1800s; proxy_send_timeout 1800s; } location ^~ /api/ws/ { set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; proxy_buffering off; proxy_read_timeout 3600s; proxy_send_timeout 3600s; } location /api/ { set $backend "http://backend:8000"; proxy_pass $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 $fwd_proto; proxy_read_timeout 120s; proxy_send_timeout 120s; } location / { set $frontend "http://frontend:3000"; proxy_pass $frontend; 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_cache_bypass $http_upgrade; } }