Load balancing with 3 vms

Hi there,

I want to load balance 3 IPs. I have followed the caddyfiles docs with example but I can get it. They have kinda telling things quite directly. Now the issue is, I dont know what file do I have to edit to make a load balancer.?

Secondly, Do I have to also install caddy on all the 3 Vms in order to make load balance.

I mean I dont basically know what step do I have to follow. Im completely noob. But have used Nginx a lot and also have done reverse proxy and load balancing using Nginx.

Ill highly appreciate if the answer is with steps.

Thank you

What you’re looking for is the proxy directive: https://caddyserver.com/docs/proxy

If you only need simple random load balancing, one of the examples does just that:

proxy / web1.local:80 web2.local:90 web3.local:100
1 Like

Ok thanks a lot for the reply.

Do you mean do I have to make changes into /etc/caddy/Caddyfile and what other lines do I have to enter despite proxy / web1.local:80 web2.local:90 web3.local:100.

Further, how can I test it.

The link in @1lann’s post to the http.proxy documentation is pretty comprehensive, particularly the examples section:


These go in your Caddyfile, with some adaptation to suit your backend hosts.

As for testing, I’d recommend checking the access logs of your backends - Caddy’s proxy will show up as standard client requests.

1 Like

Thank you for your reply, I have done everything and its working perfectly fine… But one of my setting is giving 502 bad gateway when i access via browser. But when I try other 3 ips it works perfect. Why do I experience a 502 bad gateway…

Below is the Caddyfile


localhost:2021 {
        bind {$ADDRESS}

        proxy /

        log /var/log/caddy/cassandra-demo_access.log

        errors log /var/log/caddy/cassandra-demo_error.log



localhost:2020 {
        bind {$ADDRESS}

        proxy /

        log /var/log/caddy/kafka-demo_access.log {


The Cassandra config works perfect for me but the Kafka section is giving 502 bad gateway… What can be the Issue

A 502 Bad Gateway from Caddy means that when you tried to connect to localhost:2020, Caddy tried to proxy your request to (or .252 or .253) but wasn’t able to establish a connection.

You’ll need to troubleshoot why those endpoints aren’t accepting HTTP connections. Start by using curl -i from your Caddy host, which will give you a good idea of exactly how that backend is (or isn’t?) responding to Caddy.

1 Like