Application and use of Nginx GeoIP module

Source: Internet
Author: User
Tags geoip

reasons for using the GeoIP module (why): Requests are forwarded to customers in different cities for the needs of the project, eg:

When Tianjin User A visited www.XXXX.com, Nginx forwarded this request to the Tianjin server and the domain name of Tianjin, the browser automatically jump tj.XXXX.com

When Guangzhou User B visited www.XXXX.com, Nginx forwarded this request to the server in Guangzhou and the domain name of Guangzhou, the browser automatically jump gz.XXXX.com

The purpose of using the GeoIP module (what): Bo Master Understanding as two levels, the first technical level, can reduce the load of a server, to achieve load balancing effect; the second business level, can be the user to get better interaction, such as want to SouFun The home page of the search house is the local area data and so on

When to use the GeoIP module (when): Bo Master is understood as two levels, the first technical level, if the site access is particularly large when you can use this mode, effectively reduce the resource consumption of some servers, the second business level, when the business has the need of time, In other words, you need to use this feature when you have a local service point in a city

Use the GeoIP module application (Where): Bo Master Understanding for the container on the Nginx configuration, as the name implies GeoIP is Nginx compiler module, of course, to use on Nginx

people using the GeoIP module (WHO): Of course, the operation of the engineer, called yards to configure them will not AH!

How to Use the GeoIP module: The following explains how to use the GeoIP module

Download the required packages first: Linux System, Nginx1.8.1, Echo-nginx-module-master, GeoIP.dat GeoLiteCity.dat

Note: Bloggers have used this module on Linux Ubuntu12.04 and CentOS 6, and have operated on two versions of Nginx1.8.1 and Nginx1.4.2.

Step one, compile nginx original parameters to keep the inconvenience need to add two parameters for--add-module=/usr/src/echo-nginx-module-master and--with-http_geoip_module two parameters

Step Two, make

Step three, make install

To see if the installation was successful

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7C/1D/wKiom1bOvwHj6P3xAAEolIqj34k999.jpg "title=" Qq20160225164423.jpg "alt=" Wkiom1bovwhj6p3xaaeoliqj34k999.jpg "/>

Step four, modify Nginx configuration file in the HTTP segment to add two lines of parameters, respectively:

Geoip_country/var/aeoip/geoip.dat;geoip_city/var/aeoip/geolitecity.dat;

Add a new server segment as follows:

server {listen 9999;       server_name localhost;               Location/hello {Default_type text/plain;               echo $geoip _city;               echo $geoip _country_code;               Did echo test the Nginx GeoIP module successfully?; } }

Step five, add NGIXN variables to modify the Fastcgi_params file, add the following:

#geofastcgi_param geoip_city_country_code $geoip _city_country_code;fastcgi_param geoip_city_country_code3 $geoip _ City_country_code3;fastcgi_param geoip_city_country_name $geoip _city_country_name;fastcgi_param GEOIP_REGION $ Geoip_region;fastcgi_param geoip_city $geoip _city;fastcgi_param geoip_postal_code $geoip _postal_code;fastcgi_param Geoip_city_continent_code $geoip _city_continent_code;fastcgi_param geoip_latitude $geoip _latitude;fastcgi_param Geoip_longitude $geoip _longitude;

Step Six: Testing client access using the extranet Http://HostIp:9999/hello


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7C/1E/wKiom1bOwevB_sCfAADx-lv8ukc662.png "style=" float: none; "title=" ~ $G _l}}nc}55 (61S ' e$8kw2 (1). png "alt=" Wkiom1bowevb_scfaadx-lv8ukc662.png "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7C/1F/wKiom1bOyFrj19QbAACOm0o4kRs578.jpg "title=" lqv~ Virc2uk2395hdvs (CWI (1). jpg "alt=" wkiom1boyfrj19qbaacom0o4krs578.jpg "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7C/1E/wKioL1bOyYTCT2nyAAAj5cjICwE987.png "title=" M3~]wz $Q% (@F1]917h]f%6t (1). png "alt=" Wkiol1boyytct2nyaaaj5cjicwe987.png "/>

The installation of the Nginx GeoIP module is successful.

PS1: Parameter Introduction

$geoip _country_code-Two-letter country code, such as: "RU", "US".

$geoip _country_code3-three-letter country code, such as: "RUS", "USA".

$geoip _country_name-The full name of the country, such as: "Russian Federation", "states" (if available).

$geoip _region-The name of the region (similar to province, region, state, district, Federal land, etc.), such as: "Moscow City", "DC" (if available).

$geoip _city-The city name, such as "Moscow", "Washington" (if available).

$geoip _postal_code-Zip code (if available).

$geoip _city_continent_code (if available).

$geoip _latitude-the dimension (if available).

$geoip _longitude-The longitude (if available).

PS2: The forwarding request for Nginx using GEO is not elaborated in this article, the following information for reference network information

Like what:

Location/{
if ($geoip _city = Guangzhou) {
Root/data/web/guangzhou;
}
if ($geoip _region = "30") {
Root/data/web/guangdong;
}
}

Or

if ($geoip _region = 30) {
Rewrite ^/index.html http://www.renzhemao.com/home.html permanent;
}

When IP comes from Guangdong, redirect the homepage to home.html.

Or

location/geoip/{
Rewrite. */?country= $geoip _country_code;
}

When unsure whether a city name is specific, you can use the strings command to view the string, such as:

Strings GeoLiteCity.dat |grep-i Guangzhou

This matches to the Guangzhou, so we can use Guangzhou the city name.

Of course variables support regular matching, such as:
if ($geoip _region ~ "(30|31)")

We may not be accurate when testing, because we use the data source is free, the specific can be queried on the official website: Http://www.maxmind.com/en/geoip_demo



Reference: http://www.ttlsa.com/nginx/using-nginx-geo-method/

http://ju.outofmemory.cn/entry/16264

Http://www.tuicool.com/articles/6femi2

Http://shouce.jb51.net/nginx/OptionalHTTPmodules/GeoIP.html

Https://www.aliyun.com/zixun/content/3_12_518421.html

http://ju.outofmemory.cn/entry/96242

Http://www.zybang.com/question/fe24b27b69b842690db277d2246c60a2.html

This article from "Loyalty" blog, declined reprint!

Application and use of Nginx GeoIP module

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: info-contact@alibabacloud.com 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.