GeoIP是IP地址对应的地理位置数据库,本文介绍怎么使用Nginx-GeoIP获得访问者所在的地理位置。
GeoIP模块设置了多个环境变量:$geoip_country_name
,$geoip_country_code
,$geoip_city
等等,你可以在PHP脚本或Nginx配置文件中使用这些变量。例如:你可以根据访问者所在国家设置网站语言。
我使用的系统环境:
# 确保Nginx支持GeoIP
1 |
$ nginx -V |
# 下载GeoIP数据库
在Ubuntu系统上可以直接使用apt安装geoip-database,但是它只包含国家信息,而且也不是最新数据库。我从http://geolite.maxmind.com下载最新的GeoIP数据库。
创建一个目录:
1 |
$ sudo mkdir /etc/nginx/geoip |
使用wget下载:
1 2 3 4 5 |
$ cd /etc/nginx/geoip $ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz $ sudo gunzip GeoIP.dat.gz # 国家数据库 $ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz $ sudo gunzip GeoLiteCity.dat.gz # 城市数据库 |
# 配置Nginx
编辑配置文件:
1 |
$ sudo vim /etc/nginx/nginx.conf |
把GeoIP数据库文件路径添加到http段:
1 2 |
geoip_country /etc/nginx/geoip/GeoIP.dat; geoip_city /etc/nginx/geoip/GeoLiteCity.dat; |
为了可以在PHP中使用这些GeoIP变量,我们需要设置一些fastcgi_param参数。
1 |
$ sudo vim /etc/nginx/fastcgi_params |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
### 设置GEOIP变量 ### 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虚拟主机配置文件中(location ~ \.php$)有一行 include /etc/nginx/fastcgi_params。
最后,重启Nginx:
1 |
$ sudo systemctl restart nginx |
重启PHP-FPM:
1 |
$ sudo systemctl restart php7.0-fpm |
测试
创建一个PHP测试文件:
1 |
$ sudo vim /var/www/html/geoip.php |
使用如下代码访问GeoIP变量:
1 2 |
$geoip_country_code = getenv(GEOIP_COUNTRY_CODE); $geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE']; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<html> <body> <?php $geoip_country_code = getenv(GEOIP_COUNTRY_CODE); $geoip_country_name = getenv(GEOIP_COUNTRY_NAME); $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE); echo 'country_code: '.$geoip_country_code.'<br>'; echo 'country_name: '.$geoip_country_name.'<br>'; echo 'city_country_code: '.$geoip_city_country_code.'<br>'; ?> </body> </html> |
访问http://your_server/geoip.php测试。
其它配置
如果你使用Nginx做反向代理,而想把GeoIP变量传入到后端,你应该创建或编辑proxy.conf文件:
1 |
$ sudo vim /etc/nginx/proxy.conf |
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
### 设置GEOIP变量 ### 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 proxy配置中添加 include /etc/nginx/proxy.conf。
在Nginx配置文件中使用GeoIP变量:
1 2 3 4 |
location / { index index.html index.php; try_files /index_$geoip_country_code.html /index.html; } |