How to Reverse Proxy on a Machine with Multiple IP Addresses?

3. The problem I’m having:

Hi, I have a Ubuntu 20.04 machine with a single network interface that has 3 IP addresses assigned to it. On this machine, I will like to run 3 instances of a program on this machine, each listening to a different port (8000, 8001, 8002).

Is it possible to configure Caddy to act as a reverse proxy for all traffic to these 3 IP addresses, so that they are routed to the correct instance of the program?

For example, if we have the IP addresses 10.0.0.10, 10.0.0.11, 10.0.0.12, we will like to configure Caddy such that

  • Program #1 listening to localhost:8000 <=> ┌────┐ <=> 10.0.0.10:8000
  • Program #2 listening to localhost:8001 <=> │Caddy │ <=> 10.0.0.11:8000
  • Program #3 listening to localhost:8002 <=> └────┘ <=> 10.0.0.12:8000

Thank you!

Yep, you can use the bind subdirective to bind to a specific network interface:

Maybe something like this:

10.0.0.10:8000 {
	bind 10.0.0.10
	reverse_proxy localhost:8000
}

10.0.0.11:8000 {
	bind 10.0.0.11
	reverse_proxy localhost:8001
}

So this is a bit counter intuitive, but note that you still need to use bind even if you specify the IP in the site address, because Caddy will listen on 0.0.0.0 regardless. Also, kinda awkward, but because it would be ambiguous, you need to specify a hostname (either an IP or domain) for the site address, otherwise Caddy will complain about duplicate site addresses (e.g. if you tried to use :8000 as the site address for both)

(Memo to self) Now that Caddy 2 is done, it might be worth investigating to see if this is still a necessary limitation, when the bind addresses are clearly different…

This topic was automatically closed after 30 days. New replies are no longer allowed.