利用Nginx加GeoIP MaxMind数据库获取用户的地理位置

  • 版权声明:本文为博主chszs的原创文章,未获得博主授权均不能转载,否则视为侵权。
  • 本文讲述仅通过配置Nginx加上GeoIP MaxMind数据库,就能获得用户IP地址的实际物理位置,而无需编写任何代码。

地理位置数据在业务中有重要作用,这些数据可以用于向某些人群推广品牌、产品或服务,还有助于增强用户体验。
本文讲述仅通过配置Nginx加上GeoIP MaxMind数据库,就能获得用户IP地址的实际物理位置,而无需编写任何代码。
Nginx是一个开源的HTTP和IMAP/POP3代理服务器,主要用作Web服务器或反向代理服务器。Nginx的GeoIP模块(即ngx_http_geoip_module)使用了预编译的MaxMind数据库来设置变量,比如变量geoipcountryname、变量geoip_country_code、变量$geoip_city等等,而这些值则取决于用户客户端的访问地址。

  • 先决条件:Ubuntu 16.04
  • 用例:在Ubuntu系统安装Nginx,配置Nginx使用GeoIP MaxMind数据库,再根据用户的IP地址查找具体的地理位置。
  • 概要:
  • 1)安装和配置
  • 2)使用客户端IP数据取回具体的地理位置

一、安装和配置

下面看在Ubuntu上安装和配置NGINX,安装GeoIP模块,下载GeoIP MaxMind GeoCity和GeoCountry数据库,并配置让Nginx使用GeoIP MaxMind数据库。

要安装和配置Ubuntu,执行以下操作。
要安装Nginx发布包,先获得官方的签名Key:

$ curl -s https://nginx.org/keys/nginx_signing.key | sudo apt-key add –

再使用以下命令把Nginx仓库添加到apt源:

$ sudo echo -e "deb https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx\n deb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

使用以下命令重新同步apt源,并安装Nginx软件包:

$ sudo apt-get update
$ sudo apt-get install nginx

二、安装GeoIP模块

GeoIP模块用于查看连接到服务器的客户机的IP地址。
要安装GeoIP模块,执行以下的步骤。
使用以下命令下载并载入GeoIP模块到/usr/lib/Nginx/modules目录:

$ sudo add-apt-repository ppa:nginx/stable
$ sudo apt-get update
$ sudo apt-get install nginx-module-geoip

编辑Nginx的配置文件nginx.conf:

$ sudo nano /etc/nginx/nginx.conf

添加以下内容:

load_module "modules/ngx_http_geoip_module.so";

注意:如果–with-http-geoip-module已经存在于你安装的Nginx,那么跳过此步骤。

要检查GeoIP模块的存在,使用以下命令:

$ nginx -V

三、下载GeoIP MaxMind GeoCity和GeoCountry数据库

要在Ubuntu系统下载并提取MaxMind GeoCity和GeoCouontry数据库,使用以下命令:

mkdir - p / etc / nginx / geoip
cd / etc / nginx / geoip
wget http: //geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoI.dat.gz
gunzip GeoIP.dat.gz
wget http: //geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz

四、配置Nginx使用GeoIP MaxMind数据库

要配置Nginx使用GeoIP MaxMind GeoCity和GeoCountry数据库,需访问MaxMind geo-变量。执行以下命令:

load_module "modules/ngx_http_geoip_module.so";
worker_processes 1;
events {
 worker_connections 1024;
}
http {
 geoip_country / etc / nginx / geoip / GeoIP.dat;#
 the country IP database
 geoip_city / etc / nginx / geoip / GeoLiteCity.dat;#
 the city IP database
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for"';
 access_log /
  var / log / nginx / access.log main;#
 Set Geo variables in proxy headers
 proxy_set_header X_COUNTRY_CODE $geoip_country_code;
 proxy_set_header X_CITY_COUNTRY_CODE $geoip_city_country_code;
 proxy_set_header X_REGION $geoip_region;
 proxy_set_header X_CITY $geoip_city;
 proxy_set_header X_POSTAL_CODE $geoip_postal_code;
 server {
  listen 80;#
  All other traffic gets proxied right on through.
  location / {
   proxy_pass http: //127.0.0.1:3000;
  }
 }
}

五、使用客户端IP地址检索地理位置数据

下面创建一个简单的Web应用(Node.js程序),它可以返回JSON格式的请求报头参数。把自定义geo-字段添加到请求报头,并且可以从应用程序访问它。此Web应用通过Nginx进行反向代理。

要获得用户的地理位置数据,代码如下:

// This sample web application returns request headers in response
 const express = require('express')
 const app = express()
 var count = 1;
// Location "/show_my_identity" hosts the incoming request headers in response in JSON format
 app.get('/show_my_identity', function (req, res) {
 res.send(JSON.stringify(req.headers));
 console.log('Request',count++,'received from country : ',req.headers.x_country_code);
 })
// Default "/" message
 app.get('/', function (req, res) {
 res.send("Welcome to Treselle lab");
 })
// Application listening on port 3000
 app.listen(3000, function () {
 console.log('Treselle Lab - App listening on port 3000!')
 })

具有地理定位数据的应用程序的输出看起来与此类似:

利用Nginx加GeoIP MaxMind数据库获取用户的地理位置_nginx

注意:要运行此Node.js应用程序,相关的依赖模块要安装。

具有地理位置数据的应用程序日志与以下内容类似:

利用Nginx加GeoIP MaxMind数据库获取用户的地理位置_地理位置_02


就是这些!