Geo模块

摘要

This module creates variables, whose values depend on the IP-address of the client.

Example configuration:

  1. geo $geo {
  2. default 0;
  3. 127.0.0.1/32 2;
  4. 192.168.1.0/24 1;
  5. 10.1.0.0/16 1;
  6. }

指令

geo

syntax:*geo [$ip_variable] $variable { … }*

default: none

context:*http*

The directive describes the dependency of the value of a variable on the IP-address of a client. By default, the IP-address used for the lookup is $remote_addr, but since version 0.7.27 it is possible to specify which variable should be used.

  1. geo $arg_remote_addr $geo {
  2. ...;
  3. }

Addresses are assigned in the form CIDR. Furthermore, there are four special parameters:

Example of the description:

  • delete – deletes the specified network (0.7.23).
  • default - the value of variable, if the client address does not correspond to any assigned address. It is possible so to write instead of default 0.0.0.0/0.
  • include - text file with addresses and values information. Several files can be inluded like this.
  • proxy - specifies the address of proxy server (0.8.7+). NEED MORE DESCRIPTION…
  • ranges – specifies that the addresses specified are in the form of ranges (0.7.23). This directive must be the first.
    1. geo $country {
    2. default no;
    3. include conf/geo.conf;
    4. 127.0.0.0/24 us;
    5. 127.0.0.1/32 ru;
    6. 10.1.0.0/16 ru;
    7. 192.168.1.0/24 uk;
    8. }

In the file conf/geo.conf:

  1. 10.2.0.0/16 ru;
  2. 192.168.2.0/24 ru;

The value will be the the one with maximum agreement. For example, the IP address 127.0.0.1 will get the value "ru", but not "us".

Example with ranges:

  1. geo $country {
  2. ranges;
  3. default no;
  4. 127.0.0.0-127.0.0.0 us;
  5. 127.0.0.1-127.0.0.1 ru;
  6. 127.0.0.1-127.0.0.255 us;
  7. 10.1.0.0-10.1.255.255 ru;
  8. 192.168.1.0-192.168.1.255 uk;
  9. }

References

原文: https://wizardforcel.gitbooks.io/nginx-doc/content/Text/3.10_geo.html