Caddy Server as a reverse proxy for Phoenix Applications

Caddy is fantastic little production ready server that we can use instead of Apache or Nginx reverse proxy in front of multiple phoenix applications in one box.

The best thing is that we get https without needing to do anything on phoenix side. Caddy automatically provisions a certificate from Let’s encrypt.

All we need to do is just write one line of code.

Some important commands

sudo caddy start
sudo caddy stop
sudo caddy reload

Concept

We need to have a file Caddyfile (no extension) in the directory from where we would start the caddy server.

Say we have Caddyfile in the home directory - /home/sher

This is the content of the Caddyfile -

use.example.app {
        reverse_proxy * http://localhost:6001
}

test.example.app {
        reverse_proxy * http://localhost:8066
}
example.app {
        reverse_proxy * http://localhost:8000
}
another-site.com {
        reverse_proxy * http://localhost:4002
}

So we just need to write one line for each domain and our job is done.
Please note

test.example.app {
        reverse_proxy * http://localhost:8066
}

Here the phoenix app is still running in localhost but the final url would be https://test.example.app. This happens by magic. So you don’t need to do anything and https should automatically work.

This automatically gives you HTTP/2 which is faster than HTTP/1.1.

My Use case

I run a software development agency and we have several projects running in one box, so Caddy really helps there.

More advanced stuff is also possible. Check out the docs.

I found Caddy really interesting and thought that I should share it with the community.

P.S: I am not associated with Caddy in any way

8 Likes

You also have the library site_encrypt from @sasajuric.

I am using Traefik for the same propose because I deploy with Docker.

If not in mistake Traefik uses Caddy under the hood.

3 Likes

I’m using Caddy as a reverse proxy for Phoenix too! I spent a weekend trying and failing to understand Nginx configs, Docker and so on, and eventually gave up. Then I found Caddy and all the SSL/LetsEncrypt stuff just works out of the box.

4 Likes

I’m also running a couple of umbrella phoenix apps in production with caddy-2 as reverse proxy. I really appreciate the simplicity of its configuration. I had a bit hard time figuring how to install it on ubuntu server, but I eventually find some good tutorials that helped me.

1 Like

I love Caddy!

I’ve been building a small SAAS app with Elixir & Phoenix that makes heavy use of Caddy V2 and it’s been amazing to use. The app is an API for other apps that want to let their users connect custom domains or spin up subdomains on the fly. Each of my users gets their own dedicated Caddy instances that my app manages, and it’s worked flawlessly.

I’d really recommend checking out the JSON API as well as an alternative to using a Caddyfile. V2 has an admin API you can use to make updates while it’s running. Can’t recommend it enough.

Caddy is also built mostly by one person, Matt Holt, who can always use new sponsors on GitHub, if it’s working well for you :+1:. Great project to support.

1 Like