sreyansjain

sreyansjain

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

Most Liked

rasputin303

rasputin303

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.

carterbryden

carterbryden

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.

Exadra37

Exadra37

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.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14403 124
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement