Automatic Https With Caddy - Extremely Easy

Today I had a chance to try Caddy for dev purpose, and you guess what, it was extremely easy and funny. Only two steps and boom, you got a secured website almost instantly.
Here are the steps I followed:

Install Caddy on Ubuntu/Debian

Check their documentation for another OS/installation method.

1sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
2curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
3curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
4sudo apt update
5sudo apt install caddy

Edit Caddyfile and restart the service

1sudo vim /etc/caddy/Caddyfile
1dev-api.your-domain.com
2
3reverse_proxy 127.0.0.1:8000
1sudo systemctl restart caddy
2# or
3sudo caddy reload

That's it, you can now browse https://dev-api.your-domain.com.
In my case, a REST API service was running on port 8000 and I configured Caddy as a reverse proxy.
Configuring Caddy should be easy for other use cases as well, I'm pretty sure.

If you were to configure multiple domains:

1api.domain.com {
2  reverse_proxy 127.0.0.1:8000
3}
4
5app.domain.com {
6  reverse_proxy 127.0.0.1:3000
7}

If you were to serve SPA(Single Page Application):

 1prod.domain.com {
 2        encode zstd gzip
 3        root * /home/user/domain.com/prod
 4        file_server
 5        try_files {path} /index.html
 6}
 7
 8staging.domain.com {
 9        encode zstd gzip
10        root * /home/user/domain.com/staging
11        file_server
12        try_files {path} /index.html
13}
14
15qa.domain.com {
16        encode zstd gzip
17        root * /home/user/domain.com/qa
18        file_server
19        try_files {path} /index.html
20}

Happy coding!

comments powered by Disqus