4

I've got a case where I need to do a different proxy pass in Nginx depending on which CIDR the client's IP address is part of.

So, for example, let's say I have the following CIDRs:

  10.50.0.0/16
  10.51.0.0/16
  10.52.0.0/16

Each of those client addresses needs to have a different proxy_pass in Nginx. How would I go about doing this? I'm very new to Nginx so achieving things like this are still a bit confusing.

1 Answer 1

11

You could use Geo module. Your configuration then would look somewhat like this:

geo $upstream  {
    default default_upstream;

    10.50.0.0/16 some_upstream;
    10.51.0.0/16 another_upstream;
}

upstream default_upstream {
    server 192.168.0.1:80;
}

upstream some_upstream {
    server 192.168.0.2:80;
}

upstream another_upstream {
    server 192.168.0.3:80;
}

server {
    ...
    location ... {
        ...
        proxy_pass http://$upstream;
    }
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.