使用GeoIP与Nginx在Ubuntu 12.04

在Ubuntu 12.04上使用GeoIP与Nginx

本教程将介绍如何在Ubuntu 12.04上使用nginx的GeoIP模块,以了解访问者的来源。 GeoIP模块设置多个变量,如$ geoip_country_name$ geoip_country_code$ geoip_city等,您可以在PHP脚本中使用,或直接在nginx配置中使用,例如,根据用户的国家/地区提供不同语言的内容。

我不会保证这将为您工作!

1初步说明

我正在使用网站www.example.com ,其中包含文件根/var/www/www.example.com/web/和Nginx vhost配置文件/etc/nginx/sites-enabled/www.example.com。虚拟主机

Ubuntu用户注意事项:

因为我们必须使用root权限运行本教程的所有步骤,所以我们可以使用字符串sudo在本教程中添加所有命令,也可以通过键入来成为root

sudo su

2找出如果Nginx支持GeoIP

在我们开始之前,我们必须确定GeoIP模块是否内置在我们的nginx服务器中:

nginx -V
root@server1:~# nginx -V
nginx version: nginx/1.1.19
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module
root@server1:~#

3下载GeoIP数据库

在Debian和Ubuntu上,有一个可以通过apt安装的geoip 数据库 ,但是它有点过时,只包含GeoIP.dat (国家数据库),而不是GeoLiteCity.dat (城市数据库)。 因此,我们不会安装该软件包,而是从GeoIP网站下载新的副本到/ etc / nginx / geoip目录:

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

4配置Nginx

现在我们配置nginx。 打开/etc/nginx/nginx.conf ...

vi /etc/nginx/nginx.conf

...并将geoip_countrygeoip_city指令添加到http {}容器中:

[...]
http {
        geoip_country  /etc/nginx/geoip/GeoIP.dat; # the country IP database
        geoip_city     /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database
[...]

geoip_country指令使以下变量可用:

  • $ geoip_country_code - 两个字母的国家代码,例如RUUS
  • $ geoip_country_code3 - 三个字母的国家/地区代码,例如, 美国 RUS
  • $ geoip_country_name - 国家的(详细)名称,例如俄罗斯联邦美国等。

geoip_city指令提供以下变量:

  • $ geoip_city_country_code - 两个字母的国家/地区代码,例如RUUS
  • $ geoip_city_country_code3 - 三个字母的国家/地区代码,例如RUSUSA
  • $ geoip_city_country_name - 国家的名称,例如俄罗斯联邦美国 - 如果可用。
  • $ geoip_region - 区域名称(省,地区,州,省,联邦土地等),例如, 莫斯科市DC (如果有的话)。
  • $ geoip_city - 城市的名称,例如莫斯科华盛顿里斯本等 - 如果有的话。
  • $ geoip_postal_code - 邮政编码或邮政编码(如果有的话)。
  • $ geoip_city_continent_code - 如果可用。
  • $ geoip_latitude - 纬度 - 如果可用。
  • $ geoip_longitude - 经度 - 如果可用。

为了使这些变量也可用于PHP脚本,我们必须设置一些fastcgi_param指令。 最好在文件/ etc / nginx / fastcgi_params中执行此操作,其他fastcgi_param指令是:

vi /etc/nginx/fastcgi_params
[...]
### SET GEOIP 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;

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;

(请确保您的vhost配置包含/ etc / nginx / fastcgi_params;在您的位置〜\ .php $ {}容器中,否则上述配置对您的虚拟主机无用)。

如果您使用nginx作为反向代理,并希望将GeoIP变量传递给后端,则应创建/编辑文件/etc/nginx/proxy.conf ...

vi /etc/nginx/proxy.conf

...并添加以下行:

[...]
### SET GEOIP Variables ###
proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;

proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
proxy_set_header GEOIP_REGION $geoip_region;
proxy_set_header GEOIP_CITY $geoip_city;
proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
proxy_set_header GEOIP_LATITUDE $geoip_latitude;
proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

(确保在您的nginx代理配置中使用包含/etc/nginx/proxy.conf的行,否则后端无法使用GeoIP变量。)

现在重新加载nginx ...

/etc/init.d/nginx reload

...使更改生效。

重新启动PHP-FPM如下:

/etc/init.d/php5-fpm restart

5短测

要了解GeoIP模块是否正常工作,我们可以在www.example.com网络空间中创建一个小型PHP文件(例如/var/www/www.example.com/web ):

vi /var/www/www.example.com/web/geoiptest.php

我们可以访问GeoIP变量,如下所示:

$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);

或者像这样:

$geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE'];
<html>
<body>
<?php

$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
/*
$geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE']; // works as well
*/
$geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3);
$geoip_country_name = getenv(GEOIP_COUNTRY_NAME);

$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 'country_code: '.$geoip_country_code.'<br>';
echo 'country_code3: '.$geoip_country_code3.'<br>';
echo 'country_name: '.$geoip_country_name.'<br>';

echo 'city_country_code: '.$geoip_city_country_code.'<br>';
echo 'city_country_code3: '.$geoip_city_country_code3.'<br>';
echo 'city_country_name: '.$geoip_city_country_name.'<br>';
echo 'region: '.$geoip_region.'<br>';
echo 'city: '.$geoip_city.'<br>';
echo 'postal_code: '.$geoip_postal_code.'<br>';
echo 'city_continent_code: '.$geoip_city_continent_code.'<br>';
echo 'latitude: '.$geoip_latitude.'<br>';
echo 'longitude: '.$geoip_longitude.'<br>';

?>
</body>
</html>

在浏览器中调用该文件( http://www.example.com/geoiptest.php ),您应该看到GeoIP在工作中(确保您从公共IP地址(而不是本地IP地址调用文件)

也可以直接在nginx配置中使用GeoIP变量,例如:

vi /etc/nginx/sites-enabled/www.example.com.vhost
[...]
        location / {
            index index.html index.php;
            try_files /index_$geoip_country_code.html /index.html;
        }
[...]
/etc/init.d/nginx reload

在此示例中,如果访问者来自德国(国家代码: DE ),并且文件index_DE.html存在,则会提供此文件,否则将提供默认的index.html文件:

这可以用于根据用户的来源为不同语言的内容提供服务。

6链接

关于作者

Falko Timme是所有者 Timme Hosting (超快nginx网页托管)。 他是youcl(自2005年以来)的主要维护者, 也是ISPConfig的核心开发人员之一 (自2000年起)。 他还为O'Reilly的“Linux系统管理”一书作出了贡献。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏