Giter Club home page Giter Club logo

ip2location / ip2location-nginx Goto Github PK

View Code? Open in Web Editor NEW
54.0 7.0 19.0 50 KB

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.

Home Page: http://www.ip2location.com

License: MIT License

C 100.00%
nginx ip2location ip-database ip-lookup ip-geolocation ip-address-location geolocation

ip2location-nginx's Introduction

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 [email protected]

ip2location-nginx's People

Contributors

ip2location avatar morganov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ip2location-nginx's Issues

nginx config check not pass

my nginx binary file has been added module
./configure --add-module=/usr/local/src/ip2location-nginx-master/
but when i use "nginx -t " to check config
i get this wrong
root@test-nginx01:/usr/local/nginx/sbin# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ip2location" in /usr/local/nginx/conf/nginx.conf:47
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

/usr/local/nginx/conf/nginx.conf:47 is
ip2location on;
ip2location_database /usr/local/nginx/conf/IP2LOCATION-LITE-DB1.BIN;
ip2location_access_type shared_memory;

thx :)

Can not load database file on nginx reload

Hi,

When I'm trying to do a nginx reload, I've got in error logs this

[emerg] can not load database file /root/ip2locationdb/A8F5F167F44F4964E6C998DEE827110C.bin using "shared_memory" access type in /etc/nginx/conf.d/ip2location.conf:2

If I do a nginx restart, everything goes ok. What's wrong with this?

somthing wrong with me with the new commit

make[1]: *** No rule to make target `/var/softs/ip2location-nginx/ngx_http_ip2location.c', needed by `objs/addon/ip2location-nginx/ngx_http_ip2location.o'.  Stop.
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory `/var/softs/tengine-2.1.2'
make: *** [build] Error 2

my nginx host cpu use time too high

when i use ip2location on nginx(add this config "ip2location_database /path/IP-COUNTRY-REGION-CITY.BIN;") , my nginx host cpu use time will be too high after about 10 hours , and ip2location out of work too , how can i solve this problem
thanks

can not load database file on service reload

2019/09/11 08:49:21 [emerg] 53086#102353: can not load database file /var/db/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN using "shared_memory" access type in /usr/local/etc/nginx/nginx.conf:65

This appears to be related to having more than 1 nginx worker. As mentioned in #6 and #5. The same issue exists with the ip2proxy nginx module.

If you reload a second time, the error does not recur.

Additional details:

  1. The .bin file is not corrupted (it loads cleanly with a restart and via the perl module).
  2. Permissions are fine (global readable).
  3. Available memory is not an issue.

nginx multi-process error with ip2location

when i set nginx worker_processes to 1, everything is ok, but if i set worker_processes larger than 1, i got nginx error "can not load database file ", that means only one process can load database file?

Configurable support for headers other than X-Forwarded-For

Currently lookups are only performed on the X-Forwarded-For header. There are cases where a site may want to use an alternative header for determining client IP. eg: X-Client-IP

I propose a new nginx configuration directive so that this header can be specified.

Syntax: ip2location_proxy_header header
Default: X-Forwarded-For
Context: http
Description: Define the HTTP header used for determining client IP.

Proxy, VPN, TOR detection

Is it possible to use this module with PX2BIN database to detect Proxy, VPN, TOR status for IP? Any special varaible for this?

Something broken with latest commits

Running nginx 1.20.2 w/latest ip2location library/nginx code, I'm finding that after a reload, the $ip2location_zipcode variable is sometimes full of junk data.

Examples:

\x14\x16\x04\x1B\x1C\xB4\xC4
ngiri\x05Rukwa\x07Rumonge\x04Ruse\x06Rutana\x06Ruvuma\x06Ruyigi\x13Ryazanskaya
-
-
\x84B\xFF>\x84B
3\x84B\xFB>\x84B
-
B\xF8\xF2\x83B\xE0\x0B\x84B\xD59\x84B\x0F?\x84B
-
\x14\x16\x04\x1B\x1C\xB4\xC4
lectronics
\xC8\xD6\x94=\x17\x11\x9D=\x13>\xAE=\xE7\xB9+BGJ|BOM|B\x97\xC3|B\x15=}B\xBFX\x83B\x0F\xEC\x83Bq\xEF\x83B\x95\xF7\x83B\x85;\x84B\xFB>\x84B
-
-
RSV
SFL0029\x08USFL0030\x08USFL0031\x08USFL0032\x08USFL0033\x08USFL0034\x08USFL0036\x08USFL0038\x08USFL0039\x08USFL0
-

Error compile on latest mainline Nginx 1.23

make[1]: Entering directory /usr/src/nginx-1.23.0' cc -c -fPIC -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \ -o objs/addon/ip2location-nginx/ngx_http_ip2location_module.o \ /usr/src/ip2location-nginx/ngx_http_ip2location_module.c /usr/src/ip2location-nginx/ngx_http_ip2location_module.c: In function ‘ngx_http_ip2location_get_records’: /usr/src/ip2location-nginx/ngx_http_ip2location_module.c:315:8: error: assignment from incompatible pointer type [-Werror] xfwd = &r->headers_in.x_forwarded_for; ^ /usr/src/ip2location-nginx/ngx_http_ip2location_module.c:318:4: error: passing argument 3 of ‘ngx_http_get_forwarded_addr’ from incompatible pointer type [-Werror] (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL, gcf->proxies, gcf->proxy_recursive); ^ In file included from src/http/ngx_http.h:36:0, from /usr/src/ip2location-nginx/ngx_http_ip2location_module.c:10: src/http/ngx_http_core_module.h:531:11: note: expected ‘struct ngx_table_elt_t *’ but argument is of type ‘struct ngx_array_t *’ ngx_int_t ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr, ^ cc1: all warnings being treated as errors make[1]: *** [objs/addon/ip2location-nginx/ngx_http_ip2location_module.o] Error 1 make[1]: Leaving directory /usr/src/nginx-1.23.0'
make: *** [modules] Error 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.