Reverse proxy based on remote addr

Hello folks. I’m very new to Caddy, so please bear with me.

Is it possible to change the reverse proxy destination based on visitor IP?

So, “if coming from x.x.x.x, go to place; if coming from y.y.y.y, then go to a different place”

Thanks.

Hi @dpantel, welcome to the Caddy community!

You can get this functionality by leveraging the rewrite functionality and Placeholders. The general idea is to test for the circumstances you want, append a proxy-specific prefix to the URI if it’s a match, and then you can strip that prefix back out when you proxy the request upstream.

example.com {

  # Send one specific IP address to upstream1
  rewrite {
    if {remote} is 192.168.1.1
    to /proxy1{uri}
  }
  proxy /proxy1 http://upstream1 {
    without /proxy1
  }

  # Send anyone in a specific subnet to upstream2
  rewrite {
    if {remote} starts_with 192.168.2.
    to /proxy2{uri}
  }
  proxy /proxy2 http://upstream2 {
    without /proxy2
  }

  # upstream3 catches everyone else
  proxy / http://upstream3
}

https://caddyserver.com/docs/proxy
https://caddyserver.com/docs/rewrite
https://caddyserver.com/docs/placeholders

Thanks for a quick response. I’ll give the ideas a whirl.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.