Geo Use notes

Source: Internet
Author: User

the GEO directive is provided using the Ngx_http_geo_module module. By default,nginx has to load this module unless it is artificially--without-http_geo_module.


A module can be used to create a variable whose value depends on the client IP address.

How to use:

syntax: Geo [$address] $variable {...}
Default value:-

      Configuration segment: http
      Defines the IP address of the client to be obtained from the specified variable.

1. Default: Nginx obtains the client IP address from the $REMOTE_ADDR variable

 geo  $caddr  {     default 0; 1; 2;           include        conf/cadd.conf;      delete;      proxy;      proxy          2001:0db8::/32; }    parameter Interpretation:      nginx describes addresses through CIDR or address segments, supporting the following parameters:      Delete: Deletes the specified network      default: If the client address does not match any of the defined addresses, Nginx will use this value.   If using cidr,                can be used " "instead of default.      include:&nbSP; contains a file that defines an address and a value, which can contain more than one.      proxy: Defines a trusted address.   If the request comes from a trusted address, Nginx will use its "x-forwarded-for" header to get              Address.   The trusted address is sequential detection, relative to the normal address.      proxy_recursive: Turns on recursive lookup addresses.   If recursive lookup is turned off, the client address and a trusted address           , Nginx will use the last address in "x-forwarded-for" instead of the original customer                           End Address. If recursive lookup is turned on, the client address matches a trusted address,nginx                          will use the last address in "x-forwarded-for" that does not match all trusted addresses to substitute                          Replace the original client address.    &nbsP; ranges: Use the address segment to define the address, this parameter must be in the first place. To speed up the loading of the address library, the address should be defined in ascending order.            here, the default is to match the contents of $remote_addr  with geo{}    ,  If the match succeeds, set $caddr  to this match value (eg:  when $remote_addr  is   ' ', this time   $caddr     ' 1 '), it can be used in other scopes within the server .            instance one  :  Controls access to the directory based on the IP  geo obtained.           http{                   geo  $caddr  {              default 0;     1;            }         server {             ......                         location /addr {                root /webroot/default/;      #设置默认的目录                if   (  $caddr  = 1 ) {                   root /webroot/1/;        #根据 $ addr  to specify a different directory.                }             }                   }            #注:Base it part slightly.           echo  "Default page"  > / webroot/default/addr/index.html     echo  "11111 page"   >  /webroot/1/addr/index.html          now reload a little bit nginx           Test:         On, execute  curl            get   "11111  page "         other host  , execute  curl http://           get   "Default page"

2. Use the specified variable.

HTTP {Geo $arg _boy $blkIP {default 0;   Local;   Net1; Net2; } }

3. Matching principle

The GEO directive assigns values to variables based primarily on IP. Therefore, the GEO block can only define IP or network segment, otherwise it will error "Nginx: [Emerg] Invalid network".

Practical applications that can be combined with limit_req to achieve simple DDoS attacks

    http{        geo  $remote _addr  $black _iplist {           default 0;            include black_iplist.conf;         }                 limit_req_zone  $DDos _ip zone=ddos_ip:10m rate=10r/m;         limit_req zone=DDos_IP burst=1 nodelay;             server {                    location / {                  if  (  $black _iplist =  1 )         ####    determine if there is a blacklist;               {                 set  $DDos _ip  $black _iplist;   ####    Speed limit for IP; #               return  503;               ####     Direct return 503;               for IP          }             }        }    }

Geo Use notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.