Caddy Reverse Proxy Examples: Mastering Proxying with Ease

In today's dynamic web landscape, reverse proxy servers play a pivotal role in enhancing security, performance, and flexibility. One of the most robust tools for implementing reverse proxies is Caddy, an open-source, powerful, and easy-to-use web server created in Go. Here, we explore various Caddy reverse proxy examples, showcasing its versatility and simplicity.

the diagram shows how forward and reverse prox are used to access data from servers
the diagram shows how forward and reverse prox are used to access data from servers

Before delving into specific use cases, let's ensure we're on the same page regarding reverse proxies. A reverse proxy sits in front of one or more servers, distributing workloads, securing connections, and potentially caching responses. Now, let's explore how Caddy excels in this arena.

proxy symbol
proxy symbol

Basic Caddy Reverse Proxy

Setting up a basic reverse proxy with Caddy is a breeze. Here's a simple example that proxies requests to an upstream server:

just because u stopped believing doesn't mean he stopped watching.
just because u stopped believing doesn't mean he stopped watching.

http://example.com {
 reverse_proxy localhost:3000
}
In this configuration, all requests made to http://example.com are forwarded to the server running at localhost:3000.

Using Named Servers in Caddy

an image of some cartoon characters with different facial expressions on their faces and body parts
an image of some cartoon characters with different facial expressions on their faces and body parts

Named servers allow you to define an upstream server with a name that you can reference elsewhere. This promotes code reuse and simplifies managing multiple servers.

api.example.com {
 reverse_proxy localhost:8080
}

app.example.com {
 reverse_proxy localhost:3000
}
In this example, api.example.com and app.example.com both serve different applications running on the same machine.

Load-balancing with Caddy

Ergo Proxy
Ergo Proxy

Caddy allows you to load-balance between servers, ensuring no single server becomes a bottleneck. Here's how to create a simple round-robin load balancer:

Round-Robin Load Balancing

example.com {
 reverse_proxy round_robin localhost:8080 localhost:3000
}
In this configuration, requests are distributed between the servers at localhost:8080 and localhost:3000 in a round-robin fashion.

three people standing next to each other in front of a dark background with the words okay written on it
three people standing next to each other in front of a dark background with the words okay written on it

Least Connections Load Balancing

You can also load-balance based on the server with the fewest active connections:

two people with masks on their faces and one is wearing a hood over his face
two people with masks on their faces and one is wearing a hood over his face
Kate proxy (Creepypasta)
Kate proxy (Creepypasta)
a drawing of a person sitting in the grass with their hands on his knees and mouth open
a drawing of a person sitting in the grass with their hands on his knees and mouth open
cute Toby
cute Toby
Circus Wallpaper, Creepypasta Proxy
Circus Wallpaper, Creepypasta Proxy
A “proxy’s pledge”
A “proxy’s pledge”
Rouge proxy icon
Rouge proxy icon
Homicidal Lou, Creepypastas Boys, Liu Creepypasta Cosplay, Creepypastas Liu, Chris Creepypasta, Liu Woods Creepypasta, Liu Creepypasta Fanart, Sully Creepypasta, Creepypasta Characters Art
Homicidal Lou, Creepypastas Boys, Liu Creepypasta Cosplay, Creepypastas Liu, Chris Creepypasta, Liu Woods Creepypasta, Liu Creepypasta Fanart, Sully Creepypasta, Creepypasta Characters Art
a black and white photo with the words, once a prosy always a prosy
a black and white photo with the words, once a prosy always a prosy
proxy Slenderman
proxy Slenderman
Proxys
Proxys
Emo Re-l
Emo Re-l
proxy
proxy
some cartoon characters are posing for a picture together with their faces painted black and white
some cartoon characters are posing for a picture together with their faces painted black and white
a black and white drawing of a woman in a short dress with her hand on her hip
a black and white drawing of a woman in a short dress with her hand on her hip
Proxy symbol - Creepypastas
Proxy symbol - Creepypastas
a collage of images with green and black colors
a collage of images with green and black colors

example.com {
 reverse_proxy least_conn localhost:8080 localhost:3000
}
This ensures that the server with the lightest load handles each new incoming request.

Caddy's extensive configuration options and straightforward syntax make it an excellent choice for setting up reverse proxies. Whether you're looking to improve performance, distribute workloads, or enhance security, Caddy's reverse proxy capabilities can help streamline your setup.

As web development continues to evolve, keeping up with the latest tools and best practices is paramount. Explore Caddy's documentation and experiment with various configurations to unlock its full potential for your projects. Happy coding!