Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - Different proxy pass based on IP ranges

Tags:

nginx

proxy

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.

like image 384
dsw88 Avatar asked Dec 24 '22 20:12

dsw88


1 Answers

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;
    }
    ...
}
like image 87
Ivan Tsirulev Avatar answered Dec 28 '22 08:12

Ivan Tsirulev