nginx module - geo

 

介紹

It creates variables with values depending on the client IP address.

目錄


ngx_http_geo_module

 

Syntax

geo [$arg_remote_addr] $geo {
    ...;
}

By default, the address is taken from the $remote_addr variable

geo $geo {
    default        0;
    127.0.0.1      2;
    10.1.0.0/16    1;
}

Opts

ranges

indicates that addresses are specified as ranges
This parameter should be the first.

Format: 10.1.0.0-10.1.255.255

include

includes a file with addresses and values.

proxy

proxy n.n.n.n;

Defines trusted addresses

When a request comes from n.n.n.n,
an address from the “X-Forwarded-For” request header field will be used instead.


Example

 

[1] allow_ip.conf

conf.d/allow_ip.conf

geo $allow_ip {
    default          0;
    10.0.0.0/8       1;
    172.16.0.0/12    1;
    192.168.0.0/16   1;
}

vhosts.conf

# FIRST on server
# 用於跳過 country checking
if ($allow_ip) { break; }
# 由 geoip2 提供
if ($allowed_country = no) { return 400; }

[2]

conf.d/geoip.conf

geo $country {
    ranges;
    default        ZZ;
    include        conf/geo.conf;
}

geo.conf

127.0.0.0/24              US;
10.1.0.0-10.1.255.255     RU;

 

Creative Commons license icon Creative Commons license icon