csrf fehler ausgebessert
This commit is contained in:
@@ -384,28 +384,117 @@ docker compose logs -f web
|
||||
docker compose exec web python manage.py createsuperuser
|
||||
```
|
||||
|
||||
### Externen Proxy konfigurieren (Beispiel)
|
||||
### Externen Reverse-Proxy konfigurieren
|
||||
|
||||
Der Stack liefert intern HTTP auf `${APP_BIND_IP}:${APP_BIND_PORT}` aus (Default `127.0.0.1:8080`). Der externe Proxy übernimmt TLS-Terminierung und die nach außen sichtbare Domain.
|
||||
|
||||
#### Voraussetzungen für externen Zugriff
|
||||
|
||||
Wenn der Proxy auf einem **anderen Host** läuft (typisch bei Nginx Proxy Manager als eigener Container/VM), muss `APP_BIND_IP` von `127.0.0.1` auf eine LAN-erreichbare Adresse umgestellt werden:
|
||||
|
||||
```bash
|
||||
# LAN-IP des Docker-Hosts ermitteln
|
||||
ip -4 -br a | grep -v lo
|
||||
|
||||
# In .env eintragen (Beispiel)
|
||||
sed -i 's/^APP_BIND_IP=.*/APP_BIND_IP=192.168.10.42/' .env
|
||||
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Alternativ `0.0.0.0` für alle Interfaces — die explizite IP ist sicherheitstechnisch sauberer.
|
||||
|
||||
Firewall absichern, damit nur der Proxy-Host auf 8080 zugreifen darf:
|
||||
|
||||
```bash
|
||||
sudo ufw allow from <proxy-host-ip> to any port 8080 proto tcp comment 'reverse proxy'
|
||||
sudo ufw deny 8080
|
||||
```
|
||||
|
||||
#### Django-Seite anpassen
|
||||
|
||||
Die externe Domain muss in `.env` eingetragen sein, sonst lehnt Django die Requests ab (400 Bad Request bzw. 403 CSRF):
|
||||
|
||||
```env
|
||||
DJANGO_ALLOWED_HOSTS=serienbrief.example.lan,localhost,127.0.0.1
|
||||
CSRF_TRUSTED_ORIGINS=https://serienbrief.example.lan,http://localhost:8080,http://127.0.0.1:8080
|
||||
```
|
||||
|
||||
Nach Änderung `docker compose restart web`.
|
||||
|
||||
#### Variante A: Nginx Proxy Manager (NPM)
|
||||
|
||||
Im NPM-Webinterface unter **Proxy Hosts → Add Proxy Host**:
|
||||
|
||||
| Feld | Wert |
|
||||
|---|---|
|
||||
| Domain Names | `serienbrief.example.lan` |
|
||||
| Scheme | `http` |
|
||||
| Forward Hostname / IP | LAN-IP des Docker-Hosts (z.B. `192.168.10.42`) |
|
||||
| Forward Port | `8080` |
|
||||
| Cache Assets | aus |
|
||||
| Block Common Exploits | an |
|
||||
| Websockets Support | an (HTMX nutzt nur AJAX, schadet aber nicht) |
|
||||
|
||||
Im Tab **Custom Locations** oder **Advanced** zusätzlich:
|
||||
|
||||
```nginx
|
||||
client_max_body_size 50M;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_read_timeout 300s;
|
||||
proxy_send_timeout 300s;
|
||||
```
|
||||
|
||||
NPM setzt `Host`, `X-Forwarded-For` und Standard-Header bereits selbst. **Pflicht** ist nur `X-Forwarded-Proto $scheme`, weil Djangos `SECURE_PROXY_SSL_HEADER` darauf basiert.
|
||||
|
||||
TLS-Zertifikat (Let's Encrypt für interne Domains via DNS-Challenge, oder eigenes CA-Cert) im Tab **SSL** zuweisen.
|
||||
|
||||
#### Variante B: Generisches Nginx / OpenResty
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name serienbrief.lan;
|
||||
server_name serienbrief.example.lan;
|
||||
|
||||
ssl_certificate /etc/ssl/lan/serienbrief.crt;
|
||||
ssl_certificate_key /etc/ssl/lan/serienbrief.key;
|
||||
|
||||
client_max_body_size 50M; # für CSV/DOCX-Uploads
|
||||
|
||||
location / {
|
||||
proxy_pass http://<docker-host-ip>:8080;
|
||||
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 https;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_read_timeout 300s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_redirect off;
|
||||
client_max_body_size 50M; # für CSV/DOCX‑Uploads
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Diagnose bei 502 Bad Gateway
|
||||
|
||||
Vom Proxy-Host aus testen:
|
||||
|
||||
```bash
|
||||
# TCP-Connect — "succeeded" erwartet
|
||||
nc -zv <docker-host-ip> 8080
|
||||
|
||||
# HTTP-Request — 200/302 erwartet
|
||||
curl -v -H "Host: serienbrief.example.lan" http://<docker-host-ip>:8080/
|
||||
```
|
||||
|
||||
Typische Ursachen:
|
||||
- `Connection refused` → `APP_BIND_IP` steht noch auf `127.0.0.1`
|
||||
- `Timeout` → Firewall blockt
|
||||
- 502 ohne Connect-Fehler → falsche Upstream-IP im Proxy-Eintrag
|
||||
|
||||
---
|
||||
|
||||
## Entwicklung in VS Code
|
||||
|
||||
Reference in New Issue
Block a user