Discopter Pi Guide

Chapter 13: Using a Reverse Proxy

A reverse proxy is an extremely versatile tool for someone self-hosting. With a reverse proxy, you'll get these benefits:

Three services in particular are used by self-hosting Pi enthusiasts: Nginx Proxy Manager (NPM), Traefik, and Caddy.

NPM (not to be confused with the similar npm, or Node Package Manager), is as close to a default as you can get. On a Raspberry Pi, it is typically run as a Docker container. It runs on port 81 by default and allows for easily adding sites and requesting certificates. It also breaks all the time. As of the writing of this guide, the latest Docker image is a buggy mess that frequently locks you out. As easy as it is to use, I don't recommend it. It not only adds another web service and Docker container for us to worry about, but there's a good chance it'll stop working at some point while you're trying to conduct maintenance.

Traefik is very similar to NPM. Most users consider it a bit more complex to manage, but it also offers a GUI option for configuring a reverse proxy. Although not considered as buggy as NPM, we're going to forgo this option for the same reason.

Caddy is an open source alternative to NPM and Traefik. Unlike the others, Caddy is a CLI-only application, so very much on theme for our setup. Although the command line interface may make it seem more difficult, it's in some ways the easiest to use of the three. Configuration is done using a text file named Caddyfile (no file extension) where reverse proxy settings can be defined. As a low overhead, easy to use option, it's what we will go with.

Step 1: Install and Configure Caddy

  1. Installation is done with just a normal apt install command.
    sudo apt install caddy
  2. You can make a new directory to contain your Caddyfile, or just write it on your home directory. Make sure you capitalize the C in Caddyfile.
    nano Caddyfile
  3. Copy and paste the below in the new file. Replace <url> with your Vaultwarden domain. If accessing it by warden.MyAwesomePi.com, put that instead of <url>. If you used a port other than 8080, replace that as well.
    # Vaultwarden
    <url> {
        reverse_proxy localhost:8080
    }
  4. Make sure you are in the same directory as your Caddyfile and reload Caddy to have the configuration take effect.
    caddy reload
  5. If you get a formatting error, use the following commands to fix your Caddyfile and then reload it.
    caddy fmt Caddyfile >' Caddyfile.formatted
    mv Caddyfile.formatted Caddyfile
    code caddy reload

That's it. Your Pi is now a reverse proxy server. Caddy will automatically request an SSL certificate from Let's Encrypt and proxy requests to the appropriate port. Any additional services can be easily added to the Caddyfile. If you added a Nextcloud instance and a website using a www subdomain, your Caddyfile might look like the below:

# Vaultwarden
warden.myawesomepi.com {
    reverse_proxy localhost:8080
}

# Nextcloud
cloud.myawesomepi.com {
    reverse_proxy localhost:8070
}

# Nodejs Website
www.myawesomepi.com {
    reverse_proxy localhost:3000
}