geo instruction uses ngx_http_geo_module module. By default, there Nginx load the module, and, unless the --without-http_geo_module.
ngx_http_geo_module module can be used to create a variable whose value depends on the client IP address.
geo instruction
Syntax: geo [$ address] $ variable {...} default: - configuration section: http defined obtain an IP address from the client specified variable. By default, nginx get the client IP address from $ remote_addr variable, but can also be obtained from other variables. Such as
geo $remote_addr $geo {
default 0;
127.0.0.1 1;
}
geo $arg_ttlsa_com $geo {
default 0;
127.0.0.1 1;
}
If the value of the variable not represent a valid IP address, then nginx will use the address "255.255.255.255". nginx described by CIDR address or addresses, supports the following several parameters: delete: to delete the specified network default: If the client does not match the address of any address defined, nginx will use this value. If you are using CIDR, you can use "0.0.0.0/0" instead of the default. include: and the address values comprises a defined file can contain multiple. proxy: the definition of a trusted address. If the request is from a trusted address, nginx will use its "X-Forwarded-For" head address obtained. With respect to the normal address, the address is sequentially detected trusted. proxy_recursive: open recursive lookups address. If you turn off recursive lookups, when the client address matches a trusted address, nginx will use the last address "X-Forwarded-For" in place of the original client address. If you open recursive search, if the client address matches a trusted address, nginx will use the "X-Forwarded-For" the last place of the original client address and all trusted addresses do not match the address. ranges: using the defined address in the form of addresses, this parameter must be in the first place. In order to accelerate the load address database, the address shall be defined in ascending order.
geo $country {
default ZZ;
include conf/geo.conf;
delete 127.0.0.0/16;
proxy 192.168.100.0/24;
proxy 2001:0db8::/32;
127.0.0.0/24 US;
127.0.0.1/32 RU;
10.1.0.0/16 RU;
192.168.1.0/24 UK;
}
vim conf/geo.conf
10.2.0.0/16 RU;
192.168.2.0/24 RU;
Addresses example:
geo $country {
ranges;
default ZZ;
127.0.0.0-127.0.0.0 US;
127.0.0.1-127.0.0.1 RU;
127.0.0.1-127.0.0.255 US;
10.1.0.0-10.1.255.255 RU;
192.168.1.0-192.168.1.255 UK;
}
[warning] to follow the most accurate matching principle, i.e. using nginx values most accurately match the client's address. [/ warning]
Suitable examples of
the above example described an example of almost all the official website. The following illustrates the usage to facilitate the understanding of the instructions. 1. Use the default variable is $ remote_addr
http {
#geo $remote_addr $ttlsa_com {
geo $ttlsa_com {
default 0;
127.0.0.1 1;
}
server {
listen 8080;
server_name test.ttlsa.com;
location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=默北
1
默北
2. Use the specified variable
http {
geo $arg_boy $ttlsa_com {
default 0;
127.0.0.1 1;
8.8.8.8 2;
}
server {
listen 8080;
server_name test.ttlsa.com;
location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=8.8.8.8
2
8.8.8.8
3. matching principle
http {
geo $arg_boy $ttlsa_com {
default 0;
127.0.0.1/24 24;
127.0.0.1/32 32;
8.8.8.8 2;
}
server {
listen 8080;
server_name test.ttlsa.com;
location /hello {
default_type text/plain;
echo $ttlsa_com;
echo $arg_boy;
}
}
}
# curl 127.0.0.1:8080/hello?boy=127.0.0.1
32
127.0.0.1
# curl 127.0.0.1:8080/hello?boy=127.0.0.12
24
127.0.0.12
[Warning] geo instruction is carried out primarily in accordance with the variable assigned IP. Thus the block can only be defined geo IP or network segment, otherwise it will error "nginx: [emerg] invalid network". [/ Warning] For reprint please indicate the source: http: //www.ttlsa.com/html/3203.html
Reproduced in: https: //my.oschina.net/766/blog/210854