Giter Site home page Giter Site logo

millken / ngx_geo_mod Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jie123108/ngx_geo_mod

0.0 3.0 0.0 1.04 MB

The nginx geo plugin, according to the IP address query provinces, city, ISP information。地理位置信息模块,根据IP地址查询的省份,城市,ISP信息

Makefile 0.68% C 79.62% Nginx 3.74% Lua 8.99% Perl 6.98%

ngx_geo_mod's Introduction

Nginx mmap geo module

ngx_geo_mod is a geographic location information module. According to the client IP query you can inquire the provinces, cities, ISP information, and then write them to http request header.

This module contains a compiler that compiles text format geo data into binary data. This module uses this binary data, and the use of mmap to load the file to memory, does not require any analytical process.

中文版说明

Table of Contents

Synopsis

http {
    include       mime.types;
    default_type  application/octet-stream;

    # set the data file path
    geodata_file /root/work/GitHub/ngx_geo_mod/top20.geo;
    # Get the IP address from the URL, only for testing.
    ip_from_url on;
    # Get the IP address from the request header, set to on when front-end have proxy.
    ip_from_head on;
    # Set the trusted proxy address. when the ip_from_head is true to used
    proxies 127.0.0.1/24;
    proxies_recursive on;

    # used variables in access log.
    log_format  main  '$remote_addr@$http_x_province $http_x_city '
                      '$http_x_isp [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;

    # Use the geo 
	mm_geo on;
    server {
        listen       80;
        server_name  localhost;
        location /area {
            # used variables in module
        	echo "      ip: $http_x_ip";
        	echo "province: $http_x_province";
        	echo "    city: $http_x_city";
        	echo "     isp: $http_x_isp";
        }

        location /not_used {
            # do not use the geo
            mm_geo off;
            echo "$http_x_province";
        }
    }
}

Compatibility

This module is compatible with the following version of nginx:

  • 1.7.x (last tested: 1.7.4)
  • 1.6.x (last tested: 1.6.1)
  • 1.4.x (last tested: 1.4.7)
  • 1.2.x (last tested: 1.2.9)
  • 1.0.x (last tested: 1.0.15)

Installation

# echo-nginx-module is for testing only,  this module does not depend on it.
cd nginx-1.x.x
./configure --add-module=path/to/ngx_geo_mod \
            --add-module=path/to/echo-nginx-module
make
make install

Compile The GEO Compiler

cd path/to/ngx_geo_mod
make
>gcc -g geodata_compiler.c array.c -o geoc
>gcc -D_TOOLS_ -g geodata.c -o geot
>gcc -g -Werror geodata.c -fPIC -shared -o libgeo.so
  • geoc: is the geo data compiler.
  • geot: is test program for the geo data.
  • libgeo.so is dynamic library.

Directives

geodata_file

syntax: geodata_file <path to binary geodata file>

default: --

context: http

Specified the GEO data file path. The data file using geoc compiled.Compile Geo Data File

mm_geo

syntax: mm_geo <on | off>

default: off

context: http,server,location,location if

Open or close the geo module. If open the GEO module, the HTTP request header will add 4 custom headers:

  • x-province province info
  • x-city city info
  • x-isp ISP info
  • x-ip IP

Those request heads can be used in other modules, See also: Variable Usage

ip_from_url

syntax: ip_from_url <on | off>

default: off

context: http

If set to on, module can get IP info by request args, the instruction is mainly used for testing. if set to on, can set the IP info by http args ip, such as: http://server/url?ip=192.168.1.40

ip_from_head

syntax: ip_from_head <on | off>

default: off

context: http

If set to on, module can get IP info by Request Header “X-Real-IP” or “X-Forwarded-For”, When the nginx front is an agent you can use this option.

proxies

syntax: proxies <address | CIDR>

default: --

context: http Define trusted addresses. When a request comes from a trusted address, an address from the “X-Forwarded-For” request header field will be used instead.

proxies_recursive

syntax: proxies_recursive <on | off>

default: off

context: http

If recursive search is disabled, the original client address that matches one of the trusted addresses is replaced by the last address sent in the request header field defined by the real_ip_header directive.

If recursive search is enabled, the original client address that matches one of the trusted addresses is replaced by the last non-trusted address sent in the request header field.

Get The IP Order

The GEO module to obtain IP address in the following orders:

  • if ip_from_url set to on, get IP info from http get args “ip”。
  • if ip_from_head set to on,get IP info from request head “X-Real-IP”,if not, from request head “X-Forwarded-For”.
  • Otherwise, the use IP of socket.

Variable Usage

Instruction mm_geo set to on, If find the geo info, 4 geo info will be added to the request headers。Those request headers can be used in other modules, Used for $http_HEADER-NAME.

  • Used in access log:
log_format  main  '$http_x_province $http_x_city $http_x_isp xxxx';
  • Used in echo module:
location /area {
    # used variables in module
	echo "      ip: $http_x_ip";
	echo "province: $http_x_province";
	echo "    city: $http_x_city";
	echo "     isp: $http_x_isp";
}

Compile Geo Data File

Geo Data File Format

Before using the compiler GEO, prepare text format data file. The file format is as follows:

################# comment ##################
1.2.3.0  1.2.3.255   BeiJing   BeiJing Unicom
1.2.4.0  1.2.4.255   BeiJing   BeiJing Telecom
1.2.5.0  1.3.5.255   BeiJing   BeiJing Mobile
2.1.1.0  2.1.2.255   HuBei Wuhan Unicom
  • comments

  • Data has 5 columns,Respectively for:IP-begin,IP-end,Province,City,ISP。Between the columns are divided by one or more spaces(or Tab)。
  • IP data must be sorted by ip. Otherwise you will get a compiler error. Note: a IP segment can not contain anther IP segment.
Compile Data File
./geoc geodata.txt
--------- Compile geodata.txt OK
--------- Output: geodata.geo

After compilation, datafile.geo can be used for mm_geo module.

Test Binary GEO Data File By geot
./geot geodata.geo
Input a ip:1.2.4.1
> [1.2.4.1] -----------  [1.2.4.0-1.2.4.255 BeiJing BeiJing Telecom]

Authors

Back to TOC

Copyright & License

This module is licenced under the BSD license.

Copyright (C) 2014, by liuxiaojie (刘小杰) [email protected]

All rights reserved.

ngx_geo_mod's People

Contributors

jie123108 avatar

Watchers

 avatar James Cloos avatar  avatar

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.