Ubuntu 16.04配置Nginx使用GeoIP

本文解释如何在Ubuntu 16.04上使用nginx的GeoIP模块,以了解您的访问者来自哪里。 GeoIP模块设置多个变量,如$geoip_country_name,$geoip_country_code,$geoip_city等,您可以在PHP脚本中或直接在nginx配置中使用这些变量,例如,根据用户所在的国家/地区以不同的语言提供内容。

了解Nginx是否支持GeoIP



在我们开始之前,我们必须找出GeoIP模块是否内置到我们的nginx服务器:

  1. nginx -V

root@server1:~# nginx -V
nginx version: nginx/1.10.0 (Ubuntu)
built with OpenSSL 1.0.2g-fips 1 Mar 2016
TLS SNI support enabled
configure arguments: –with-cc-opt=’-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2′ –with-ld-opt=’-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now’ –prefix=/usr/share/nginx –conf-path=/etc/nginx/nginx.conf –http-log-path=/var/log/nginx/access.log –error-log-path=/var/log/nginx/error.log –lock-path=/var/lock/nginx.lock –pid-path=/run/nginx.pid –http-client-body-temp-path=/var/lib/nginx/body –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –http-proxy-temp-path=/var/lib/nginx/proxy –http-scgi-temp-path=/var/lib/nginx/scgi –http-uwsgi-temp-path=/var/lib/nginx/uwsgi –with-debug –with-pcre-jit –with-ipv6 –with-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_auth_request_module –with-http_addition_module –with-http_dav_module –with-http_geoip_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_v2_module –with-http_sub_module –with-http_xslt_module –with-stream –with-stream_ssl_module –with-mail –with-mail_ssl_module –with-threads
如果看到–with-http_geoip_module,则已经把geoip模块编译到nginx中了。

下载GeoIP数据库



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

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

配置Nginx



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

  1. nano /etc/nginx/nginx.conf

…并将geoip_country和geoip_city指令添加到http {}代码块中:

  1. [...]
  2. http {
  3.  
  4.  ##
  5.  # Basic Settings
  6.  ##
  7.  
  8.  geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
  9.  geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database
  10. [...]

geoip_country指令可用变量如下:
$geoip_country_code – 两个字母的国家/地区代码,例如RU,US。
$geoip_country_code3 – 三个字母的国家/地区代码,例如,RUS,USA。
$geoip_country_name – 国家/地区的(详细)名称,例如俄罗斯联邦,美国等。
geoip_city指令提供以下变量:
$geoip_city_country_code – 两个字母的国家/地区代码,例如RU,US。
$geoip_city_country_code3 – 三字母国家/地区代码,例如,RUS,USA。
$geoip_city_country_name – 国家/地区的名称,例如,俄罗斯联邦,美国 – 如果可用。
$geoip_region – 区域的名称(省,地区,州,省,联邦土地等),例如,Moscow City,DC – 如果可用。
$geoip_city – 城市的名称,例如,莫斯科,华盛顿,里斯本等 – 如果可用。
$geoip_postal_code – 邮政编码或邮政编码(如果有)。
$geoip_city_continent_code – 如果可用。
$geoip_latitude – latitude – 如果可用。
$geoip_longitude – longitude – 如果可用。
为了使这些变量可用于您的PHP脚本,我们必须设置一些fastcgi_param指令。 最好在文件/etc/nginx/fastcgi_params中执行此操作,其他fastcgi_param指令为:

  1. nano /etc/nginx/fastcgi_params
  1. [...]
  2. ### SET GEOIP Variables ###
  3. fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
  4. fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
  5. fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
  6.  
  7. fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
  8. fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
  9. fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
  10. fastcgi_param GEOIP_REGION $geoip_region;
  11. fastcgi_param GEOIP_CITY $geoip_city;
  12. fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
  13. fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
  14. fastcgi_param GEOIP_LATITUDE $geoip_latitude;
  15. fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

(确保在你的location〜\ .php $ {}容器包含/etc/nginx/fastcgi_params;。)
如果你使用nginx作为反向代理,并希望将GeoIP变量传递给后端,你应该创建/编辑文件/etc/nginx/proxy.conf …

  1. nano /etc/nginx/proxy.conf

…并添加以下行:

  1. [...]
  2. ### SET GEOIP Variables ###
  3. proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
  4. proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
  5. proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
  6.  
  7. proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
  8. proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
  9. proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
  10. proxy_set_header GEOIP_REGION $geoip_region;
  11. proxy_set_header GEOIP_CITY $geoip_city;
  12. proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
  13. proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
  14. proxy_set_header GEOIP_LATITUDE $geoip_latitude;
  15. proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

(确保您在nginx代理配置中使用了include /etc/nginx/proxy.conf行,否则后端不能使用GeoIP变量。)
现在重新加载nginx …

  1. systemctl reload nginx.service

重新启动PHP-FPM,如下所示:

  1. systemctl restart php7.0-fpm.service

简单的测试



要查看GeoIP模块是否正常工作,我们可以在www.example.com根目录(例如/var/www/www.example.com/web)中创建一个PHP文件:

  1. nano /var/www/www.example.com/web/geoiptest.php

我们可以按如下方式访问GeoIP变量:
$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
或者:
$geoip_country_code = $_SERVER[‘GEOIP_COUNTRY_CODE’];

  1. <html>
  2. <body>
  3. <?php
  4.  
  5. $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
  6. /*
  7. $geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE']; // works as well
  8. */
  9. $geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3);
  10. $geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
  11.  
  12. $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
  13. $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
  14. $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
  15. $geoip_region = getenv(GEOIP_REGION);
  16. $geoip_city = getenv(GEOIP_CITY);
  17. $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
  18. $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
  19. $geoip_latitude = getenv(GEOIP_LATITUDE);
  20. $geoip_longitude = getenv(GEOIP_LONGITUDE);
  21.  
  22. echo 'country_code: '.$geoip_country_code.'<br>';
  23. echo 'country_code3: '.$geoip_country_code3.'<br>';
  24. echo 'country_name: '.$geoip_country_name.'<br>';
  25.  
  26. echo 'city_country_code: '.$geoip_city_country_code.'<br>';
  27. echo 'city_country_code3: '.$geoip_city_country_code3.'<br>';
  28. echo 'city_country_name: '.$geoip_city_country_name.'<br>';
  29. echo 'region: '.$geoip_region.'<br>';
  30. echo 'city: '.$geoip_city.'<br>';
  31. echo 'postal_code: '.$geoip_postal_code.'<br>';
  32. echo 'city_continent_code: '.$geoip_city_continent_code.'<br>';
  33. echo 'latitude: '.$geoip_latitude.'<br>';
  34. echo 'longitude: '.$geoip_longitude.'<br>';
  35.  
  36. ?>
  37. </body>
  38. </html>

在浏览器打开此文件后将会看到你本地IP的详细信息

标签:UbuntuNginx 发布于:2019-11-21 05:31:50