Configure Caddy as a Reverse Proxy

Configuration

By Jennifer Mathew

Updated on Jan 27, 2024

In this article, we'll explain how to configure Caddy as a reverse proxy. This guide will show you how to get a production-ready reverse proxy. We can use Caddy to act as a reverse proxy and forward incoming connection to the application.

You can protect your application from security attacks  by configuring Caddy as a reverse proxy. With Caddy all responses are served to the user over HTTPS. This article assumes that you have a backend HTTP service running at 127.0.0.1:9000. 

Edit Caddyfile

Use your favorite editor to edit Caddyfile.

sudo nano /etc/caddy/Caddyfile

Add reverse_proxy directive in configuration block.

reverse_proxy localhost:9000

Save and exit the file.

The reverse_proxy will divert all the traffic from 9000 port to 80/443.

Test and Reload Caddy

Once we done with all above steps,  test and reload the Caddy to reflect the changes. First, change the current working director to /etc/caddy/ or where your Caddyfile present.

sudo caddy validate
sudo caddy reload

Finally, navigate to your browser and visit your server IP or domain name to verify that the backend application handles the request.

That's it, we have seen how to configure Caddy as a reverse proxy.