Search code examples
nginxnginx-reverse-proxynginx-configwhitelist

How to bypass nginx proxy a list of IPS


I have an Nginx config with the redirect to holding pages:

    location / {
        ...
        if ($setholdingpage = 'True') {
        rewrite (^.*$) /holding-page last;
        }
           proxy_pass $backend;
    }

Also, I have a list of IPs that should be whitelisted and not redirected to holding pages. How it's possible to do?


Solution

  • You can use the Nginx geo module to create a variable based upon client IP address, you can specify individual IP addresses or CIDR ranges:

    geo $bypassip {
      default 0;
    
      64.233.160.0/19 1;
      66.102.0.0/20 1;
    }
    

    Then override your variable if the IP matches one in your list:

    if ($bypassip = 1){
      set $setholdingpage False;
    }
    

    I use a similar setup to block certain geographic regions but still allow Google crawlers to access my site.