项目作者: ip2location

项目描述 :
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, and usage type by IP address or hostname originates from.
高级语言: C
项目地址: git://github.com/ip2location/ip2location-nginx.git
创建时间: 2017-01-12T04:52:45Z
项目社区:https://github.com/ip2location/ip2location-nginx

开源协议:MIT License

下载


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.

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

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

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

    Static Module

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

    Dynamic Module

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

Nginx Configuration

Insert the configuration below to your nginx.conf.

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

Example:

  1. http {
  2. ...
  3. ip2location_database /usr/share/ip2location/DB6.BIN;
  4. ip2location_proxy_recursive on;
  5. ip2location_proxy 192.168.1.0/24;
  6. }

Variables

The following variables will be made available in Nginx:

  1. $ip2location_country_short
  2. $ip2location_country_long
  3. $ip2location_region
  4. $ip2location_city
  5. $ip2location_isp
  6. $ip2location_latitude
  7. $ip2location_longitude
  8. $ip2location_domain
  9. $ip2location_zipcode
  10. $ip2location_timezone
  11. $ip2location_netspeed
  12. $ip2location_iddcode
  13. $ip2location_areacode
  14. $ip2location_weatherstationcode
  15. $ip2location_weatherstationname
  16. $ip2location_mcc
  17. $ip2location_mnc
  18. $ip2location_elevation
  19. $ip2location_usagetype
  20. $ip2location_addresstype
  21. $ip2location_category
  22. $ip2location_district
  23. $ip2location_asn
  24. $ip2location_as

Usage Example

Add Server Variables
  1. server {
  2. listen 80 default_server;
  3. root /var/www;
  4. index index.html index.php;
  5. access_log /var/log/nginx/access.log;
  6. error_log /var/log/nginx/error.log;
  7. server_name _;
  8. location / {
  9. try_files $uri $uri/ =404;
  10. }
  11. location ~ \.php$ {
  12. fastcgi_pass php-fpm-sock;
  13. fastcgi_index index.php;
  14. include fastcgi.conf;
  15. # Add custom header to view result in HTTP response
  16. add_header X-Country-Code $ip2location_country_short;
  17. add_header X-Country-Name $ip2location_country_long;
  18. fastcgi_param IP2LOCATION_COUNTRY_SHORT $ip2location_country_short;
  19. fastcgi_param IP2LOCATION_COUNTRY_LONG $ip2location_country_long;
  20. fastcgi_param IP2LOCATION_REGION $ip2location_region;
  21. fastcgi_param IP2LOCATION_CITY $ip2location_city;
  22. fastcgi_param IP2LOCATION_ISP $ip2location_isp;
  23. }
  24. }

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

Block Single Country
  1. if ( $ip2location_country_short = 'US' ) {
  2. return 444;
  3. }
Block Multiple Countries
  1. map $ip2location_country_short $blacklist_country {
  2. default no;
  3. AU yes;
  4. IN yes;
  5. NG yes;
  6. }
  7. server {
  8. ...
  9. if ( $blacklist_country = yes ) {
  10. return 444;
  11. }
  12. }

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