New pre-release: Timecrack 1.0.1-beta.1 Is AvailableRead the post
Putting a Self-Hosted App Behind a Reverse Proxy

Putting a Self-Hosted App Behind a Reverse Proxy

Most self-hosted applications end up behind a reverse proxy, and most people set one up by copying a config they do not fully understand. It is worth ten minutes to understand it, because nearly every confusing self-hosting problem — the redirect loop, the mixed-content warning, the logs full of 127.0.0.1 — lives in this layer.

What it is for

A reverse proxy sits in front of your application and takes every request first. It exists to do the jobs the application should not:

Without one, your app is a process on a port. With one, it is a website.

A working nginx config

server {
    listen 443 ssl http2;
    server_name time.example.com;

    ssl_certificate     /etc/letsencrypt/live/time.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/time.example.com/privkey.pem;

    client_max_body_size 25m;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

server {
    listen 80;
    server_name time.example.com;
    return 301 https://$host$request_uri;
}

Four lines in there cause most of the trouble people have, so they are worth naming individually.

Host $host passes the original domain through. Without it the app sees 127.0.0.1 and builds links to itself pointing at localhost.

X-Forwarded-Proto $scheme tells the app the original request was HTTPS. Without it, an app that generates absolute URLs emits http:// links on an https:// page, and the browser blocks them as mixed content. If you have ever seen a login redirect loop behind a proxy, this header was missing.

X-Forwarded-For carries the real client IP. Without it every log line and every rate limit sees the proxy.

client_max_body_size is the upload limit, and nginx’s default of 1MB is smaller than most people expect. A failing upload with no useful error is usually this.

Bind the app to localhost

If the application still listens on 0.0.0.0:8080, anyone can reach it directly on that port and bypass the proxy entirely — including its TLS, its rate limits and its access rules. Bind to 127.0.0.1:8080, or in Docker map the port as 127.0.0.1:8080:80, and let the firewall close everything except 80 and 443.

Trust the headers, but only from the proxy

X-Forwarded-For is a header, and headers can be forged. An app that trusts it blindly, reachable directly, lets anyone claim any IP — which matters if you rate-limit or log by address. Configure the application’s trusted-proxy setting to your proxy’s address, and make sure the app is not directly reachable.

Caddy, if you want less config

time.example.com {
    reverse_proxy 127.0.0.1:8080
}

That is the whole file. Caddy sets the forwarded headers correctly by default and obtains and renews the certificate on its own. For a single self-hosted app it is hard to argue against.

Debugging, in order

Requests fail at exactly one of three places, and you can tell which in a minute: curl -I http://127.0.0.1:8080 tests the app, curl -I https://time.example.com tests the proxy, and the browser tests DNS and the certificate. Start at the bottom and work out.

The Timecrack installation docs cover the application side, and docker-compose is the quickest way to get something on a port to point a proxy at.

Premium Services

Need Professional Help?

Get custom development, managed hosting, data migration, and technical support — directly from the creators of Timecrack.

Explore Premium