nginx:安装用于国家/城市级别地理位置定位的GeoIP模块

时间:2020-01-09 10:42:18  来源:igfitidea点击:

如何安装用于国家和/或者城市级别地理位置定位的GeoIP nginx模块?

ngx_http_geoip_module随附了以上版本的nginx服务器0.7.63和0.8.6。
它将与MaxMind GeoIP数据库的客户端IP地址(即ip /位置查找)相匹配。

安装MaxMind C API

执行以下命令以安装MaxMind C API:

# cd /tmp

# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

# tar -zxvf GeoIP.tar.gz

# cd GeoIP-1.4.6

# yum install zlib-devel

# ./configure

# make

# make install

您需要按如下方式配置动态链接程序运行时绑定:

# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf

运行ldconfig激活配置:

# ldconfig

# ldconfig -v | less

使用GeoIP模块编译nginx

获取最新的源代码:

# cd /tmp

# wget http://nginx.org/download/nginx-0.8.52.tar.gz

# tar -zxvf nginx-0.8.52.tar.gz

# cd nginx-0.8.52

# yum install gcc pcre-devel.x86_64 openssl-devel.x86_64

# ./configure --without-http_empty_gif_module --with-poll_module --with-http_stub_status_module --with-http_ssl_module --with-ipv6 --with-http_geoip_module

# make

# make install

确保使用with-http_geoip_module选项./configure nginx。

抓住MaxMind数据库

输入以下命令以获取免费的geo_city数据库:

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -O /usr/local/share/GeoIP/GeoLiteCity.dat.gz

# gunzip /usr/local/share/GeoIP/GeoLiteCity.dat.gz

获取免费的geo_country数据库:

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O /usr/local/share/GeoIP/GeoIP.dat.gz

# gunzip /usr/local/share/GeoIP/GeoIP.dat.gz

关于MaxMind付费版本的注释

如果您需要IP地址的99.8%准确性,请使用付费版本。
编辑/usr/local/etc/GeoIP.conf,执行:

# vi /usr/local/etc/GeoIP.conf

更新配置如下:

# see https://www.maxmind.com/app/license_key_login
# Enter your license key here
LicenseKey YOUR_LICENSE_KEY_HERE
 
# Enter your User ID here
UserId YOUR_USER_ID_HERE
 
# Enter your Product ID here i.e. 106 geoip country
ProductIds 106

保存并关闭文件。
获取最新的许可版本数据库,执行:

# /usr/local/bin/geoipupdate

nginx配置

编辑nginx.conf,执行:

# vi /usr/local/nginx/conf/nginx.conf

找到http部分,并针对geoip country查找进行如下更新:

### SET the path to the .dat file used for determining the visitor's country from the IP-address ###
  geoip_country /usr/local/share/GeoIP/GeoIP.dat;
 
  ### SET FASTCGI Variables ### 
  fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
  fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
  fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

保存并关闭文件。
如果您要进行城市级别的地理位置定位,请按以下步骤进行设置:

### SET the path to the .dat file used for determining the visitor's country from the IP-address ###
  geoip_city  /usr/local/share/GeoIP/GeoLiteCity.dat;
 
  ### SET FASTCGI Variables ### 
  fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
  fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
  fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
  fastcgi_param GEOIP_REGION $geoip_region;
  fastcgi_param GEOIP_CITY $geoip_city;
  fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
  fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
  fastcgi_param GEOIP_LATITUDE $geoip_latitude;
  fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

保存并关闭文件。
最后,重新加载nginx:

# /usr/local/nginx/sbin/nginx -s reload

PHP测试脚本

按照geoip.php创建一个PHP测试脚本

<html>
<head>
  <title>What is my IP address - determine or retrieve my IP address</title>
</head>
<body>
<?php
     if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your IP address is : $ipaddress";
    }
    $country = getenv(GEOIP_COUNTRY_NAME);
    $country_code = getenv(GEOIP_COUNTRY_CODE);
    echo "<br/>Your country : $country ( $country_code ) ";
?>
</body>
</html>

如何找到MaxMind数据库信息?

执行以下命令:

# /usr/local/bin/geoiplookup -v 1.2.3.4

输出示例:

GeoIP Country Edition: GEO-106 20101001 Build 1 Copyright (c) 2010 MaxMind Inc All Rights Reserved

如何在Nginx配置中使用国家/地区代码?

您可以按以下方式使用它:

### USA specific config ###
if ($geoip_country_code = US) {
    do something here for USA visitors;
    # e.g. set root path /var/www/html/content/usa/;
}
 
### India specific config ###
if ($geoip_country_code = IN) {
    do something here for Indian visitors ;
    # e.g. set root path /var/www/html/content/india/;
}

如何测试城市级地理位置定位?

使用以下php代码:

<html>
<head>
<title>What is my IP address - determine or retrieve my IP address</title>
</head>
<body>
<?php
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "<br>Your IP address is : $ipaddress";
    }
  $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
  $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3); 
  $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME); 
  $geoip_region = getenv(GEOIP_REGION); 
  $geoip_city = getenv(GEOIP_CITY);   
  $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
  $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE); 
  $geoip_latitude = getenv(GEOIP_LATITUDE); 
  $geoip_longitude = getenv(GEOIP_LONGITUDE);
  echo "<br>Country : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) ";
  echo "<br>Region :  $geoip_region";
  echo "<br>City :  $geoip_city ";
  echo "<br>Postal code :  $geoip_postal_code";
  echo "<br>City continent code :  $geoip_city_continent_code";
  echo "<br>Geoip latitude :  $geoip_latitude ";
  echo "<br>Geoip longitude :   $geoip_longitude ";
 
?>
</body>
</html>