Using Caddy server as a reverse proxy
In this post I want to share a simple solution for the simple problem I wanted to solve some time ago. The problem definition is following:
I have a top level domain and a VPS operating on it. I also have a blog you are reading now hosted with Gitlab Pages. I want to host a website from my top level domain.
Hosting pages using Gitlab pages (similarly to Github Pages) is very convenient to set up, you just set up a pipeline and a subdomain configure the subdomain.
It get’s difficult assuming I want to keep my VPS with my top level domain.
There are few ways to achieve that. Assuming I want to keep the deployment process simple, preferably uniform with the blog, the simplest way to go would be a reverse proxy setup.
I’ve considered a few implementation options including things like nginx, but looking to learn something new and perhaps simpler than the options I already knew, I’ve found Caddy. Quoting it’s homepage
Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go
Setting up a basic reverse proxy with Caddy is as simple as running
$ caddy reverse-proxy --from example.com --to localhost:9000
If you are looking for a bit more advanced setup, you can set it up with Caddyfile. It’s a very simple, boilerplate free format. Additionally Caddy automatically acquires SSL certificates using Let’s Encrypt.
Building my reverse proxy from https://michalp.net to https://home.michalp.net was as simple as this:
michalp.net {
# Run reverse proxy
reverse_proxy https://home.michalp.net {
header_up Host {http.reverse_proxy.upstream.hostport}
}
# Enable compression
encode zstd gzip
}
Now wanting to set up my mastodon alias @majkp@social.michalp.net was as simple as this:
social.michalp.net {
header Content-type application/json
respond /.well-known/webfinger 200 {
body `{
"subject": "acct:majkp@hostux.social",
"aliases": [
"https://hostux.social/@majkp",
"https://hostux.social/users/majkp"
],
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": "https://hostux.social/@majkp"
},
{
"rel": "self",
"type": "application/activity+json",
"href": "https://hostux.social/users/majkp"
},
{
"rel": "http://ostatus.org/schema/1.0/subscribe",
"template": "https://hostux.social/authorize_interaction?uri={uri}"
}
]
}`
}
}
Next time you need to set up a HTTP server, you might want to consider Caddy.