Files
Evection/config/settings/prod.py
T
ksmithandClaude Sonnet 5 21c33e8188 Make the reverse proxy a required, pluggable part of every deployment
gunicorn previously only ever bound to a Unix socket, which assumes the
reverse proxy is nginx running on this same host — it can't be reached by
a proxy on a different machine at all (e.g. Nginx Proxy Manager on its own
box), which is this project's actual deployment target. Switch gunicorn to
listening on a plain TCP address instead, controlled by a new
EVECTION_GUNICORN_BIND setting in .env.

install.sh now asks whether the proxy is local or remote and picks a
sensible bind default either way (127.0.0.1:8000 vs 0.0.0.0:8000, with a
reminder to firewall the port in the remote case), and no longer tries to
automate nginx/certbot itself — docs/install.md's new "Reverse proxy"
section covers both shapes instead, since which proxy and where it runs is
the operator's choice, not something this script should assume or set up.
upgrade.sh's health check now curls the TCP bind instead of the old socket
path.

Also add WhiteNoise so gunicorn serves /static/ itself: the old approach
(nginx aliasing the staticfiles directory on disk) silently breaks the
moment the proxy isn't on this host and can't see this filesystem.
nginx.conf.example's own /static/ alias becomes an optional same-host perf
optimization rather than a requirement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 09:01:46 -06:00

68 lines
2.8 KiB
Python

from .base import * # noqa: F403
from .base import env
DEBUG = False
# A reverse proxy (nginx, Caddy, Traefik, Nginx Proxy Manager on another
# host, ...) is required in front of gunicorn in every deployment — see
# docs/install.md — and it's always the one terminating TLS. This tells
# Django's request.is_secure() to trust the X-Forwarded-Proto header it
# sets, and the redirect/cookie/HSTS settings below assume it's set.
SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", default=True)
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
# WhiteNoise serves /static/ directly from gunicorn instead of relying on
# the reverse proxy to alias the staticfiles directory on disk — the proxy
# may not even be on this host (see docs/install.md), so it can't be
# trusted to have filesystem access to $APP_DIR/staticfiles.
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
*MIDDLEWARE[1:], # noqa: F405
]
STORAGES = {
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = "Lax"
CSRF_COOKIE_SAMESITE = "Lax"
SECURE_HSTS_SECONDS = env.int("SECURE_HSTS_SECONDS", default=60 * 60 * 24 * 7)
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_REFERRER_POLICY = "same-origin"
X_FRAME_OPTIONS = "DENY"
INSTALLED_APPS = [*INSTALLED_APPS, "csp"] # noqa: F405
MIDDLEWARE = ["csp.middleware.CSPMiddleware", *MIDDLEWARE] # noqa: F405
# nh3 already strips <script>/<style>/on* attrs/<iframe> from stored article
# HTML at ingest (see apps.feeds.services.fetcher.sanitize_html); this is a
# second, independent layer against any sanitizer gap, and against any
# future template that forgets to escape feed-supplied text. Remote article
# images are rendered directly (not proxied, see the PWA offline-caching
# limitation in the plan), so img-src has to allow any https host.
CONTENT_SECURITY_POLICY = {
"DIRECTIVES": {
"default-src": ["'self'"],
"img-src": ["'self'", "https:", "data:"],
# Tag pills render a per-tag color via a dynamic inline style="--tag-
# color: ..." attribute (templates/articles/_article_tags.html etc.),
# so style-src needs 'unsafe-inline'. script-src stays strict with no
# exception — every <script> in the app is loaded from a static file
# (see templates/base.html), none inline.
"style-src": ["'self'", "'unsafe-inline'"],
"script-src": ["'self'"],
"frame-ancestors": ["'none'"],
"base-uri": ["'self'"],
"form-action": ["'self'"],
}
}