Reverse proxy and TLS
Put Caddy, nginx or Traefik in front of port 8080: TLS, upload size, client IP headers, PUBLIC_URL and the single sign-on callback.
The app container serves plain HTTP/1.1 on port 8080 and has no TLS of its
own. Terminate TLS in a reverse proxy, which also speaks HTTP/2 to browsers. The
basic Caddy and nginx blocks are in Installation;
this page covers the production details.
What the proxy must do
| Requirement | Why |
|---|---|
Serve the app at the root of its own hostname (https://tprm.example.com/). | Pages, redirects and the API use absolute paths (/auth/login, /d/, /api/v1/...). A sub-path such as /tprm/ is not supported. |
Accept request bodies up to S3_MAX_UPLOAD_MB (2048 MB by default). | Contract documents are streamed through the app to S3. A smaller limit gives 413. |
Set X-Forwarded-For to the client address, and drop any Forwarded header from the client. | The app takes the client IP from Forwarded, then X-Forwarded-For (first address), then the TCP peer. That IP goes into the request logs and keys the login limit: 10 failed attempts per IP and per email in 15 minutes. Without the header every user shares the proxy's IP and one budget. |
Pass the Host header and the path unchanged. | The SSO callback path must match the redirect URI exactly. |
| Be the only way in. | Port 8080 trusts forwarded headers from anyone: reached directly, a client can choose the IP that is logged and counted. See Hardening. |
Settings in .env
Once TLS is in front, set:
PUBLIC_URL=https://tprm.example.com
COOKIE_SECURE=true| Variable | Effect |
|---|---|
PUBLIC_URL | The address users reach, without a trailing slash. Single sign-on builds its redirect URI from it: <PUBLIC_URL>/api/v1/auth/sso/callback. It defaults to http://localhost:8080, which no identity provider will accept for a real deployment. |
COOKIE_SECURE | true (and when unset) marks the mlab_session cookie Secure and adds Strict-Transport-Security: max-age=31536000; includeSubDomains to every response. Set it to false only on plain HTTP, or the browser drops the cookie and login fails without an error. DEV=true also turns it off. |
CORS_ORIGIN | Leave at same-origin unless a browser application on another origin calls the API with credentials. |
The app also sends X-Frame-Options: DENY, X-Content-Type-Options: nosniff and
Referrer-Policy: strict-origin-when-cross-origin. Don't strip them.
includeSubDomains. With COOKIE_SECURE=true, browsers apply HSTS for a year
to the app's hostname and every name below it. Host the app on a name whose
subdomains all serve HTTPS.
Bind the app to localhost
When the proxy runs on the same host outside Docker, publish port 8080 on the
loopback only, in docker-compose.yml:
app:
ports:
- "127.0.0.1:8080:8080"The downloadable compose file reads the host side from APP_PORT
("${APP_PORT:-8080}:8080"), so APP_PORT=127.0.0.1:8080 in .env does the same.
When the proxy is a container on the same Docker network (Traefik below), remove
the ports mapping altogether.
Caddy
Caddy obtains and renews certificates itself, accepts bodies of any size and sets
X-Forwarded-For from the real client unless you declare trusted proxies.
tprm.example.com {
encode gzip
reverse_proxy 127.0.0.1:8080 {
header_up -Forwarded
}
}nginx
server {
listen 80;
server_name tprm.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name tprm.example.com;
ssl_certificate /etc/letsencrypt/live/tprm.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/tprm.example.com/privkey.pem;
# Contract documents: as large as S3_MAX_UPLOAD_MB (2048 by default)
client_max_body_size 2048m;
proxy_request_buffering off;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
# Overwrite, don't append: the app reads the first address
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Forwarded "";
}
}proxy_request_buffering off streams uploads to the app instead of spooling them
to the proxy's disk first.
Traefik
An example with Traefik v2/v3 as a container on the same compose network, using
Docker labels and a certificate resolver named le defined in your Traefik static
configuration. Remove the ports mapping from app.
app:
# ports: removed, Traefik reaches the app on the Docker network
labels:
- traefik.enable=true
- traefik.http.routers.tprm.rule=Host(`tprm.example.com`)
- traefik.http.routers.tprm.entrypoints=websecure
- traefik.http.routers.tprm.tls.certresolver=le
- traefik.http.routers.tprm.middlewares=tprm-noforwarded
- traefik.http.middlewares.tprm-noforwarded.headers.customrequestheaders.Forwarded=
- traefik.http.services.tprm.loadbalancer.server.port=8080Traefik sets X-Forwarded-For itself and, unless the entrypoint trusts forwarded
headers, ignores the client's. It sets no request body limit unless you add a
buffering middleware; if you do, raise maxRequestBodyBytes to S3_MAX_UPLOAD_MB.
Single sign-on behind the proxy
- Register exactly
<PUBLIC_URL>/api/v1/auth/sso/callbackat your identity provider, or for Sign in with mlab.sh in the mlab.sh app. Organization > Single sign-on shows the value the app will send, read-only: compare it with the provider's. - Changing the hostname means changing
PUBLIC_URL, restartingappand updating the redirect URI at the provider, in that order. - The app itself calls the issuer (
<issuer>/.well-known/openid-configuration, token and profile endpoints): outbound HTTPS from theappcontainer is needed, not from the proxy. - An SSO sign-in started before a restart of
appcannot complete: sign-in state is kept in memory for 10 minutes.
See Single sign-on for the messages of the sign-in page.
Check it
# Through the proxy: the sign-in page, with HSTS when COOKIE_SECURE=true
curl -sI https://tprm.example.com/auth/login | grep -iE '^(HTTP|strict-transport|x-frame)'
# The client IP the app logged for that request
docker compose logs --tail 20 app | grep "auth/login"The log line shows Request: GET [<ip>] ...: it must be your client's address,
not the proxy's.