Free / Open Source
IP2Location IP Geolocation Nginx Module
This is an IP2Location IP Geolocation Nginx Module that enables the user to identify the country code and country name by IP address. In general, it is faster, easier and more accurate than reverse DNS lookups.
Installation
- Download IP2location C library from here.
- Download and decompress this Nginx module package.
- Change the path to IP2Location library in "ngx_http_ip2location_module.c".
- Re-compile Nginx from source to include this module. Add the below directive into the compile of Nginx:
./configure --add-module=/absolute/path/to/nginx-ip2location-x.x.x make make install
- Edit your Nginx config file (nginx.conf), add the following lines under `http` context:
http { ip2location on; ip2location_database /absolute/path/to/DB1.BIN; ip2location_reverse_proxy on; }
Sample Codes
Block visitors from China:
if ( $ip2location_country_short = "CN" ) { return 403; }
Redirect visitors from Canada, United States, and Mexico to new page:
if ( $ip2location_country_short ~* "CA|US|MX" ) { rewrite ^ http://example.com/newpage.html permanent; }
Community Modules
-
ngx-ip2location
This is a IP2Location Nginx Module that enables the user to identify the country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, area code and weather by IP address.
Articles & Tutorials
You can always refer to our blog for the articles and tutorials about IP2Location NginX module.
Sample IP2Location Databases (BIN)