94 lines
3.9 KiB
Nginx Configuration File
94 lines
3.9 KiB
Nginx Configuration File
# -----------------------------------------------------------------------------
|
|
# Serves the SPA and proxies /api to the Django container.
|
|
#
|
|
# Putting both behind one origin means the browser never issues a cross-origin
|
|
# request, so no CORS pre-flight, no third-party cookie rules, and the API can
|
|
# keep its allow-list empty in production.
|
|
#
|
|
# Every location includes security-headers.conf rather than relying on a
|
|
# server-level declaration - see the comment in that file for why inheritance
|
|
# cannot be trusted here. Proxied locations additionally include
|
|
# proxy-backend.conf, which forwards the client details and drops the
|
|
# duplicate security headers Django emits.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# The SPA's policy: no inline script, nothing loaded off-origin. The bundle is
|
|
# fully self-hosted, so this can stay strict. ('unsafe-inline' for styles is
|
|
# still required by the inline style attributes React emits.)
|
|
map $host $spa_csp {
|
|
default "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'";
|
|
}
|
|
|
|
server {
|
|
listen 8080;
|
|
server_name _;
|
|
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
server_tokens off;
|
|
client_max_body_size 2m;
|
|
|
|
gzip on;
|
|
gzip_types text/css application/javascript application/json image/svg+xml;
|
|
gzip_min_length 1024;
|
|
|
|
location = /healthz {
|
|
access_log off;
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Type text/plain;
|
|
return 200 'ok';
|
|
}
|
|
|
|
# Hashed assets never change under the same name - cache them hard.
|
|
location /assets/ {
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Security-Policy $spa_csp always;
|
|
expires 1y;
|
|
add_header Cache-Control "public, immutable";
|
|
try_files $uri =404;
|
|
}
|
|
|
|
# --- API documentation ---------------------------------------------------
|
|
# Swagger UI and ReDoc bootstrap themselves from an inline <script> block
|
|
# that drf-spectacular generates, which the SPA's "script-src 'self'"
|
|
# blocks - the page loads but renders blank. Rather than punching
|
|
# 'unsafe-inline' into the policy the SPA runs under, the exemption is
|
|
# confined to these three documentation paths: they render no user data and
|
|
# no user input, they load nothing off-origin (drf-spectacular-sidecar
|
|
# serves the bundles from /static/, not a CDN), and in production they are
|
|
# not routed at all - SERVE_API_DOCS defaults to false there.
|
|
location ~ ^/api/(docs|redoc|schema)/ {
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" always;
|
|
|
|
include /etc/nginx/conf.d/proxy-backend.conf;
|
|
proxy_pass http://backend:8000;
|
|
}
|
|
|
|
location /api/ {
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Security-Policy $spa_csp always;
|
|
|
|
include /etc/nginx/conf.d/proxy-backend.conf;
|
|
proxy_pass http://backend:8000;
|
|
}
|
|
|
|
# Django admin and static files share the backend origin.
|
|
location ~ ^/(admin|static)/ {
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Security-Policy $spa_csp always;
|
|
|
|
include /etc/nginx/conf.d/proxy-backend.conf;
|
|
proxy_pass http://backend:8000;
|
|
}
|
|
|
|
# Client-side routing: unknown paths fall back to the SPA shell.
|
|
location / {
|
|
include /etc/nginx/conf.d/security-headers.conf;
|
|
add_header Content-Security-Policy $spa_csp always;
|
|
add_header Cache-Control "no-cache";
|
|
try_files $uri $uri/ /index.html;
|
|
}
|
|
}
|