Skip to content

This is IP2Location Nginx module that enables the user to find the country, region (state), city, latitude, longitude, zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation, usage type, address type and IAB category by IP address or hostname originates from.

License

Notifications You must be signed in to change notification settings

ip2location/ip2location-nginx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nginx IP2Location module

Description

The Nginx IP2Location module enables user to easily perform client's IP to geographical location lookup by using IP2Location database.

The IP2Location database can be downloaded from https://lite.ip2location.com (Free) or https://www.ip2location.com (Commercial).

Installation

  1. Download IP2location C library from https://github.com/chrislim2888/IP2Location-C-Library

  2. Compile and install IP2Location C library.

  3. Download IP2Location module and decompress the package.

    wget https://github.com/ip2location/ip2location-nginx/archive/master.zip
    unzip master.zip
    rm master.zip
  4. Download the latest Nginx source code from https://nginx.org/en/download.html

    wget https://nginx.org/download/nginx-x.y.z.tar.gz
  5. Decompress and go into Nginx source directory.

    tar xvfz nginx-x.y.z.tar.gz
    cd nginx-x.y.z
  6. Re-compile Nginx from source to include this module.

    Static Module

    ./configure --add-module=/absolute/path/to/nginx-ip2location-master
    make
    make install

    Dynamic Module

    ./configure --add-dynamic-module=/absolute/path/to/nginx-ip2location-master
    make
    make install

Nginx Configuration

Insert the configuration below to your nginx.conf.

Syntax      : load_module modules/ngx_http_ip2location_module.so;
Default     : -
Context     : main
Description : Load IP2Location Nginx module if it was compiled as dynamic.
Syntax      : ip2location_database path
Default     : none
Context     : http
Description : The absolute path to IP2Location BIN database.
Syntax      : ip2location_proxy_recursive on|off
Default     : off
Context     : http
Description : Enable recursive search in the x-forwarded-for headers.
Syntax      : ip2location_proxy cidr|address
Default     : none
Context     : http
Description : Set a list of proxies to translate x-forwarded-for headers for.

Example:

http {
	...
	
	ip2location_database		/usr/share/ip2location/DB6.BIN;
	ip2location_proxy_recursive	on;
	ip2location_proxy		192.168.1.0/24;
}

Variables

The following variables will be made available in Nginx:

$ip2location_country_short
$ip2location_country_long
$ip2location_region
$ip2location_city
$ip2location_isp
$ip2location_latitude
$ip2location_longitude
$ip2location_domain
$ip2location_zipcode
$ip2location_timezone
$ip2location_netspeed
$ip2location_iddcode
$ip2location_areacode
$ip2location_weatherstationcode
$ip2location_weatherstationname
$ip2location_mcc
$ip2location_mnc
$ip2location_elevation
$ip2location_usagetype
$ip2location_addresstype
$ip2location_category
$ip2location_district
$ip2location_asn
$ip2location_as

Usage Example

Add Server Variables
server {
	listen 80 default_server;
	root /var/www;
	index index.html index.php;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	server_name _;

	location / {
		try_files $uri $uri/ =404;
	}

	location ~ \.php$ {
		fastcgi_pass php-fpm-sock;
		fastcgi_index index.php;
		include fastcgi.conf;

		# Add custom header to view result in HTTP response
		add_header X-Country-Code $ip2location_country_short;
		add_header X-Country-Name $ip2location_country_long;

		fastcgi_param IP2LOCATION_COUNTRY_SHORT       $ip2location_country_short;
		fastcgi_param IP2LOCATION_COUNTRY_LONG        $ip2location_country_long;
		fastcgi_param IP2LOCATION_REGION              $ip2location_region;
		fastcgi_param IP2LOCATION_CITY                $ip2location_city;
		fastcgi_param IP2LOCATION_ISP                 $ip2location_isp;
	}
}

Notes: Restart Nginx and view your server response header to confirm the variables are added.

Block Single Country
if ( $ip2location_country_short = 'US' ) {
    return 444;
}
Block Multiple Countries
map $ip2location_country_short $blacklist_country {
	default no;
	AU yes;
	IN yes;
	NG yes;
}

server {
    ...
        
	if ( $blacklist_country = yes ) {
		return 444;
	}
}

IPv4 BIN vs IPv6 BIN

Use the IPv4 BIN file if you just need to query IPv4 addresses.

If you query an IPv6 address using the IPv4 BIN, you'll see the INVALID_IP_ADDRESS error.

Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.

Support

Please visit us at https://www.ip2location.com for services and databases we offer.

For support, please email us at support@ip2location.com

About

This is IP2Location Nginx module that enables the user to find the country, region (state), city, latitude, longitude, zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation, usage type, address type and IAB category by IP address or hostname originates from.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages