Replace nginx with Caddy for TLS termination and reverse proxying.

- Replace nginx with Caddy as the web server for both Docker and native
  (RPi/Debian) deployments. Caddy handles HTTPS termination, reverse
  proxying to all services (API, React app, Controller, Kiwix, Help, Map),
  and serves the media/download file browsers.
- Generate a Root CA and signed leaf certificates for HTTPS. The Root CA
  uses Name Constraints to restrict signing to local addresses only
  (.local, localhost, and RFC 1918 private IP ranges), mitigating MITM
  risk if the CA key is compromised.
- Add an HTTP landing page that guides users through one-time CA
  certificate installation on iOS, Android, macOS, Windows, Linux, and
  Firefox, with auto-redirect when the cert is already trusted.
- Add trailing-slash redirects for /controller, /docs, /media, and
  /download so bare paths don't fall through to the React catch-all.
- Add a Root CA download link on the Settings page under Configs.
- Update help.sh diagnostics for IPv6 listener detection and HTTP/2
  status codes.
- Remove all nginx config files and the https_proxy Docker container.
- Bump wrolpi-help submodule with certificate documentation.
This commit is contained in:
Roland 2026-03-07 12:08:27 -07:00 • committed by lrnselfreliance
parent dd270b37dc
commit fbb71e012d
43 changed files with 881 additions and 583 deletions

127
docker/README.md Normal file
View file

@ -0,0 +1,127 @@
# WROLPi Docker
Development and testing environment using Docker containers.
## Quick Start
```bash
git clone https://github.com/lrnselfreliance/wrolpi.git
cd wrolpi
git submodule update --init
docker-compose build --parallel
docker volume create --name=openstreetmap-data
docker volume create --name=openstreetmap-rendered-tiles
docker-compose up -d db
docker-compose run --rm api db upgrade
docker-compose up
```
Browse to https://localhost:8443
## Services
| Service | Port(s) | Description |
|----------------|--------------------------------------|-----------------------------|
| **web** | 8080 (HTTP), 8443 (HTTPS), 8084-8086 | Caddy reverse proxy |
| **api** | 8081 | Sanic API server |
| **app** | (via web) | React frontend |
| **controller** | 8087 | System management (FastAPI) |
| **db** | 5432 | PostgreSQL 12 |
| **archive** | 8083 | SingleFile/Readability |
| **zim** | (via web:8085) | Kiwix |
| **help** | (via web:8086) | MkDocs documentation |
| **map** | (via web:8084) | OpenStreetMap tile server |
## TLS Certificates
WROLPi uses a two-tier PKI for HTTPS. A **root CA** is generated once and persists
in the media directory. A **leaf certificate** is regenerated on every container start
to pick up hostname and IP changes.
### How it works
1. On first start, the `web` container generates a root CA at `/opt/media/config/ssl/`.
2. A leaf cert signed by the root CA is created at `/etc/ssl/caddy/` inside the container.
3. Caddy serves HTTPS on ports 443, 8084, 8085, and 8086 using the leaf cert.
4. Port 80 serves an HTTP landing page where users can download the root CA.
### Trusting the certificate
1. Browse to `http://<host-ip>:8080` to see the landing page with download link and
platform-specific trust instructions.
2. Download and install the root CA on your device.
3. All HTTPS ports will work without browser warnings.
The root CA only needs to be trusted once. Leaf certs can be regenerated (hostname
change, container rebuild) without needing to re-trust.
### Name Constraints
The root CA is restricted to local/private networks only via X.509 Name Constraints.
Even if the CA key were compromised, it cannot sign certificates for public domains.
Permitted:
- DNS: `*.local`, `localhost`
- IPv4: `127.0.0.0/8`, `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`
- IPv6: `::1`, `fc00::/7` (unique local), `fe80::/10` (link-local)
### EXTRA_SANS
The Docker container cannot discover the host's real LAN IP. Set `EXTRA_SANS` in a
`.env` file so the leaf certificate includes your host's IP and hostname:
```bash
# .env
EXTRA_SANS=IP:10.0.0.5,DNS:myhost.local
```
Each entry must have a type prefix (`IP:` or `DNS:`). Multiple entries are
comma-separated. After changing `EXTRA_SANS`, restart the web container:
```bash
docker compose up -d --build web
```
### Regenerating the root CA
If you need to regenerate the root CA (e.g. after updating the certificate
generation script), delete the old CA files and rebuild:
```bash
rm test/config/ssl/ca.crt test/config/ssl/ca.key
docker compose up -d --build web
```
All devices will need to re-download and re-trust the new CA from
`http://<host-ip>:8080/ca.crt`.
## Volumes
The `test/` directory is mounted to `/opt/media` inside containers, serving as the
media directory for development. Certificate files, configs, and downloaded content
are stored here.
Two external volumes are required for the map service:
```bash
docker volume create --name=openstreetmap-data
docker volume create --name=openstreetmap-rendered-tiles
```
## Environment Variables
| Variable | Default | Description |
|-------------------|----------------|----------------------------------------------|
| `MEDIA_DIRECTORY` | `/opt/media` | Media directory inside containers |
| `EXTRA_SANS` | (empty) | Additional SANs for the TLS leaf certificate |
| `WEB_HOST` | `0.0.0.0` | Host bind address for the web service |
| `WEB_PORT` | `8080` | HTTP port |
| `WEB_HTTPS_PORT` | `8443` | HTTPS port |
| `REACT_APP_API` | `0.0.0.0:8081` | API bind address |
| `UID` | `1000` | User ID for api/app containers |
| `GID` | `1000` | Group ID for api/app containers |
## Upgrading
See [UPGRADE.md](../UPGRADE.md) for upgrade instructions.

View file

@ -1,21 +0,0 @@
events {}
http {
server {
listen 443 ssl;
ssl_certificate /cert.crt;
ssl_certificate_key /cert.key;
add_header 'Access-Control-Allow-Origin' '*';
location / {
proxy_pass http://help:80;
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 'Access-Control-Allow-Origin' '*';
}
}
}

@ -1 +1 @@
Subproject commit 0bc20aa33e03f35f2da62ecd9460636fc8398b1c
Subproject commit f8164d905310670b95f63d08bb9254f7d4359f7d

View file

@ -1,16 +0,0 @@
# This Dockerfile is used as a reverse proxy to serve a HTTPS nieve service with HTTPS.
FROM nginx:latest AS https_proxy
# Create a self-signed cert and key pair for HTTPS.
RUN openssl genrsa -out /cert.key 2048
RUN openssl req -new -x509 -nodes -key /cert.key -out /cert.crt -days 3650 \
-subj "/C=US/ST=State/L=City/O=Org/OU=WROLPi/CN=wrolpi.local"
RUN chmod 777 /cert.key /cert.crt
# Apply docker-compose environment variables to config
ENV UPSTREAM_HOST=you-must-change-this-in-docker-compose
ENV UPSTREAM_PORT=80
COPY docker/https_proxy/nginx.proxy.conf.template /etc/nginx/nginx.conf
RUN envsubst < /etc/nginx/nginx.conf > /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]

View file

@ -1,18 +0,0 @@
events {}
http {
server {
listen 443 ssl;
ssl_certificate /cert.crt;
ssl_certificate_key /cert.key;
location / {
proxy_pass http://${UPSTREAM_HOST}:${UPSTREAM_PORT};
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;
}
}
}

View file

@ -1,21 +0,0 @@
events {}
http {
server {
listen 443 ssl;
ssl_certificate /cert.crt;
ssl_certificate_key /cert.key;
add_header 'Access-Control-Allow-Origin' '*';
location / {
proxy_pass http://map:80;
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 'Access-Control-Allow-Origin' '*';
}
}
}

119
docker/web/Caddyfile Normal file
View file

@ -0,0 +1,119 @@
# Caddy config for WROLPi services (Docker).
{
# No ACME - WROLPi is designed for offline use.
auto_https disable_redirects
}
:80 {
handle / {
root * /var/www
rewrite * /landing.html
file_server
}
handle /ca.crt {
root * /opt/media/config/ssl
header Content-Type "application/x-x509-ca-cert"
header Content-Disposition "attachment; filename=\"wrolpi-ca.crt\""
file_server
}
handle {
redir https://{host}:8443{uri} permanent
}
}
:443 {
tls /etc/ssl/caddy/cert.crt /etc/ssl/caddy/cert.key
handle /favicon.ico {
root * /etc/caddy
rewrite * /favicon.ico
file_server
}
handle /apple-touch-icon.png {
root * /etc/caddy
rewrite * /icon.png
file_server
}
# WebSocket for React dev server.
handle /ws {
reverse_proxy app:3000
}
# Controller - system management API and UI.
handle_path /controller/* {
reverse_proxy controller:8087
}
handle /controller {
redir * /controller/ 301
}
# Main API.
handle /api/* {
reverse_proxy api:8081
}
# API docs.
handle /docs/* {
reverse_proxy api:8081
}
handle /docs {
redir * /docs/ 301
}
# Static media files with directory browsing.
handle_path /media/* {
root * /opt/media
file_server browse
}
handle /media {
redir * /media/ 301
}
# Downloads with Content-Disposition header.
handle_path /download/* {
root * /opt/media
header Content-Disposition "attachment"
file_server browse
}
handle /download {
redir * /download/ 301
}
# React app with fallback to Controller.
handle {
reverse_proxy app:3000 {
@bad status 502 503 504
handle_response @bad {
header Location /controller/?fallback=true
respond "" 302
}
}
}
}
# Kiwix HTTPS proxy (replaces zim_https container).
:8085 {
tls /etc/ssl/caddy/cert.crt /etc/ssl/caddy/cert.key
header ?Access-Control-Allow-Origin "*"
header ?Access-Control-Allow-Methods "GET, OPTIONS"
reverse_proxy zim:80
}
# Help HTTPS proxy (replaces help_https container).
:8086 {
tls /etc/ssl/caddy/cert.crt /etc/ssl/caddy/cert.key
header ?Access-Control-Allow-Origin "*"
header ?Access-Control-Allow-Methods "GET, OPTIONS"
reverse_proxy help:80
}
# Map HTTPS proxy (replaces map_https container).
:8084 {
tls /etc/ssl/caddy/cert.crt /etc/ssl/caddy/cert.key
header ?Access-Control-Allow-Origin "*"
header ?Access-Control-Allow-Methods "GET, OPTIONS"
reverse_proxy map:80
}

View file

@ -1,11 +1,13 @@
FROM nginx:latest
FROM caddy:latest
# Create a self-signed cert and key pair for HTTPS.
RUN openssl genrsa -out /cert.key 2048
RUN openssl req -new -x509 -nodes -key /cert.key -out /cert.crt -days 3650 \
-subj "/C=US/ST=State/L=City/O=Org/OU=WROLPi/CN=wrolpi.local"
RUN chmod 777 /cert.key /cert.crt
RUN apk add --no-cache openssl bash
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/web/nginx.dev.conf /etc/nginx/nginx.conf
COPY docker/web/wrolpi.dev.conf /etc/nginx/conf.d/wrolpi.conf
# Copy scripts and static files.
COPY docker/web/Caddyfile /etc/caddy/Caddyfile
COPY docker/web/entrypoint.sh /entrypoint.sh
COPY scripts/generate_certificates.sh /opt/wrolpi/scripts/generate_certificates.sh
COPY etc/raspberrypios/landing.html /var/www/landing.html
RUN chmod +x /entrypoint.sh /opt/wrolpi/scripts/generate_certificates.sh
ENTRYPOINT ["/entrypoint.sh"]

11
docker/web/entrypoint.sh Normal file
View file

@ -0,0 +1,11 @@
#!/bin/sh
export MEDIA_DIRECTORY="${MEDIA_DIRECTORY:-/opt/media}"
export CERT_DIR="/etc/ssl/caddy"
# EXTRA_SANS can be set to include additional SANs in the leaf certificate.
# This is needed in Docker because the container cannot discover the host's
# real LAN IP. Example: EXTRA_SANS=IP:10.0.0.5,DNS:myhost.local
export EXTRA_SANS="${EXTRA_SANS:-}"
/opt/wrolpi/scripts/generate_certificates.sh
exec caddy run --config /etc/caddy/Caddyfile --adapter caddyfile

View file

@ -1,46 +0,0 @@
events {
}
http {
client_max_body_size 0;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
charset utf-8;
include mime.types;
types {
# Custom types for ebooks.
application/x-mobipocket-ebook mobi;
application/epub+zip epub;
# Configs
text/yaml yaml yml;
}
server {
listen 80;
listen [::]:80;
server_name _; # Default server for requests without "Host" header
location / {
return 301 https://$host:8443$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
proxy_pass_header Host;
# Self-signed cert from Dockerfile build.
ssl_certificate /cert.crt;
ssl_certificate_key /cert.key;
include /etc/nginx/conf.d/*;
}
}

View file

@ -1,91 +0,0 @@
# Nginx config for the WROLPi services.
#
# App
#
location /favicon.ico {
alias /etc/nginx/favicon.ico;
}
location /apple-touch-icon.png {
alias /etc/nginx/icon.png;
}
location / {
# React APP with Controller fallback
proxy_pass http://app:3000;
proxy_intercept_errors on;
error_page 502 503 504 = @react_fallback;
rewrite $1 break;
}
location @react_fallback {
return 302 /controller/?fallback=true;
}
location /ws {
proxy_pass http://app:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
#
# Controller - system management API and UI
# All Controller endpoints are under /controller/*
#
location /controller {
rewrite ^/controller(.*)$ $1 break;
proxy_pass http://controller:8087;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
#
# Main API - handled by the API service (port 8081)
#
location /api {
proxy_pass http://api:8081;
rewrite /^api(.*)$ $1 break;
}
location /docs {
proxy_pass http://api:8081;
rewrite /^docs(.*)$ $1 break;
}
location /media {
# The media files mounted in the docker volume
sendfile on;
sendfile_max_chunk 1m;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
autoindex on;
autoindex_exact_size off;
alias /opt/media;
}
location /download {
# Assume any request here is to download a file from the media directory.
if ( $request_filename ~ '^.*/(.+)$' ) {
# Always include the filename for downloads.
add_header Content-Disposition 'attachment; filename="$1"';
}
sendfile on;
sendfile_max_chunk 1m;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
autoindex on;
autoindex_exact_size off;
alias /opt/media;
}

View file

@ -1,19 +0,0 @@
events {}
http {
server {
listen 443 ssl;
ssl_certificate /cert.crt;
ssl_certificate_key /cert.key;
location / {
proxy_pass http://zim:80;
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 'Access-Control-Allow-Origin' '*';
}
}
}