start project
This commit is contained in:
+48
-23
@@ -4,7 +4,21 @@
|
||||
# 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 _;
|
||||
@@ -12,14 +26,6 @@ server {
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
# --- Security headers ----------------------------------------------------
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), interest-cohort=()" always;
|
||||
# The bundle is fully self-hosted, so the policy can stay strict.
|
||||
add_header Content-Security-Policy "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'" always;
|
||||
|
||||
server_tokens off;
|
||||
client_max_body_size 2m;
|
||||
|
||||
@@ -29,40 +35,59 @@ server {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
# --- 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;
|
||||
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;
|
||||
proxy_set_header X-Request-ID $request_id;
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_read_timeout 30s;
|
||||
}
|
||||
|
||||
# Django admin and API documentation share the backend origin.
|
||||
location ~ ^/(admin|static)/ {
|
||||
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;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
# Client-side routing: unknown paths fall back to the SPA shell.
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user