Making IP-based routing decisions in Clojure

Ring has :remote-addr but that won’t always be correct if you’re using a CDN and/or a load balancer and you’ll need to write a short middleware function to pull the correct value from whatever headers are provided.

At work, we have both F5 BigIP and various CDNs in front of things and so we have to account for the headers for these and how IP addresses and headers are passed through. CloudFlare, for example, can be configured to pass Class E IPv4 addresses to the backend but also pass IPv6 addresses in headers. You’ll have to think about IPv4 vs IPv6 in any IP-based routing solution you set up, BTW.

Also, I seem to recall there’s an assumption in some Ring middleware about the comma-separated sequence of IPs that are passed through proxy pipelines – and it’s definitely wrong for our stack at work and we overwrite :remote-addr for that reason. See How to get the client IP address in ring-clojure? - Stack Overflow for example for some of the issues involved.

1 Like