http_geo module

http_geo module#

The module creates variables with values depending on the client IP address.

Example Configuration#

geo $geo {
    default        0;      2; 1;    1;

    ::1            2;
    2001:0db8::/32 1;




geo [$address] $variable { … }




Describes the dependency of values of the specified variable on the client IP address. By default, the address is taken from the $remote_addr variable, but it can also be taken from another variable, for example:

geo $arg_remote_addr $geo {


Since variables are evaluated only when used, the mere existence of even a large number of declared geo variables does not cause any extra costs for request processing.

If the value of a variable does not represent a valid IP address then the “” address is used.

Addresses are specified either as prefixes in CIDR notation (including individual addresses) or as ranges .

The following special parameters are also supported:


deletes the specified network


a value set to the variable if the client address does not match any of the specified addresses. When addresses are specified in CIDR notation, “” and “::/0” can be used instead of default. When default is not specified, the default value will be an empty string


includes a file with addresses and values. There can be several inclusions.


defines trusted addresses. When a request comes from a trusted address, an address from the “X-Forwarded-For” request header field will be used instead. In contrast to the regular addresses, trusted addresses are checked sequentially.


enables recursive address search. If recursive search is disabled then instead of the original client address that matches one of the trusted addresses, the last address sent in “X-Forwarded-For” will be used. If recursive search is enabled then instead of the original client address that matches one of the trusted addresses, the last non-trusted address sent in “X-Forwarded-For” will be used.


indicates that addresses are specified as ranges. This parameter should be the first. To speed up loading of a geo base, addresses should be put in ascending order.


geo $country {
    default        ZZ;
    include        conf/geo.conf;
    proxy          2001:0db8::/32;   US;   RU;    RU; UK;

The conf/geo.conf file could contain the following lines:    RU; RU;

A value of the most specific match is used. For example, for the address the value RU will be chosen, not US.

Sample range description:

geo $country {
    default                   ZZ;       US;       RU;     US;     RU; UK;