1

Declaring new variables such as map or geoIP2 are always on the the http context.

Lets say I need to check for location of an ip only on certain location block in my configuration, does that mean that this variables related to the geoIP2 will be calculated in any connection established to the server or only when its get called?

I wondered how NGINX actually works, when do the variables get calculated?

example:

http{
    geoip2 GeoIP/GeoLite2-Country.mmdb {
        $geoip2Country country iso_code;
    }
    map $geoip2Country $denyCountry {
        default  1;
        US  0;
    }
    
    location /zone/{    //the only location where I use the variable
        if ($denyCountry){
            return 444;
        }
    }
}
iTaMaR
  • 189
  • 2
  • 10
  • The variable is evaluated at the point its encountered in the configuration file. – Richard Smith Mar 08 '21 at 15:04
  • Which means? On every single connection made to server or only when its being used (on the location I mentioned)? – iTaMaR Mar 08 '21 at 15:35
  • Only if every connection uses the variable. If the variable is used inside a specific `location` block on a specific statement, then it will only be evaluated when a request matches that `location` and the statement is evaluated. – Richard Smith Mar 08 '21 at 15:39
  • Lets say I'm using geoIP2, which looks up for a country based on the client IP. in this point the variable related to the GeoIP2 is declared on the `http` block. Also I have a `map` that declares another variable based on that geoIP2 variable. I have only one `location` block that uses the `map` variable.. So is it evaluated for any other connection outside that `location`? – iTaMaR Mar 08 '21 at 15:47

0 Answers0