website logo

Last Updated:

Use Caddy as Reverse Proxy

Caddy is a fast and open source tool that works as a reverse proxy and a static file server. Caddy is written in Golang and hence faster than other traditional web servers.

Caddy Server as a Reverse Proxy

Follow these steps to install and run Caddy as a reverse proxy in your machine.

  1. Install caddy from the package manager.
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
  1. Check if Caddy is running in the background.
systemctl status caddy
  1. Your caddyfile is saved in /etc/caddy/Caddyfile. Once you change any configuration in your Caddyfile, make sure to reload Caddy using the reload command.
caddy reload --config /etc/caddy/Caddyfile
  1. Open Caddyfile file and delete all the boilerplate configurations.
sudo nano /etc/caddy/Caddyfile
  1. Now add the following configuration in the Caddyfile. This configuration works as a reverse proxy at port 80 to a server running at localhost:5000.
:80 {
	reverse_proxy localhost:5000
}
  1. Instead of using port 80, you can also add the domain name. First, make sure to point the domain name to this server (by editing the A records). Caddy will proxy your traffic to the desired destination.
example.com {
	reverse_proxy localhost:5000
}

Serving multiple site using Caddy

Serving multiple site hosted on the same machine is very easy with Caddy. You can just declare each site with their domain in the Caddyfile and you are good to go.

example.com {
	reverse_proxy localhost:5000
}

example2.com {
	reverse_proxy localhost:3000
}

Automatic HTTPS using Caddy Server

If you use your domain name in the Caddyfile instead of a port number, Caddy will automatically issue a public HTTPS certificate for your domain.

Now you don’t have to spend any money for HTTPS certificates. Caddy gets you covered.

Conclusion

In this blog, you have learned how to use Caddy server as a reverse proxy. How to host multiple sites in the same machine and adding automatic SSL certificate for your website. I hope you have learned something new. Let’s see on another blog.

See Also