描述: 本次实践将根据请求者的国家显示中文或者英文的IP地址位置等相关信息在网页上,通过前面的学习,我们知道 geoip2 在检索有关 geoip 数据库的元数据时,其语法格式为 $variable_name metadata
实践流程
Step 1.编辑Nginx.conf主配置文件在 http 片段中,自定义定义访问日志格式后加入如下两个 geoip2 指令片段
http { ..... geoip2 /usr/local/GeoIP2/GeoLite2-Country.mmdb { # 启用自动重新加载将使 nginx 以指定的时间间隔检查数据库的修改时间,如果发生更改则重新加载。 auto_reload 7d; $geoip2_country_code country names en; } geoip2 /usr/local/GeoIP2/GeoLite2-City.mmdb { # 中国IP访问都显示中文 $geoip2_data_country "default=中国" source=$remote_addr country names zh-CN; # 中国 $geoip2_data_country_code country iso_code; # CN $geoip2_data_country_continent continent names zh-CN; # 亚洲 $geoip2_data_country_continent_code continent code; # AS $geoip2_data_province_name subdivisions 0 names zh-CN; # 浙江省 $geoip2_data_province_isocode subdivisions 0 names iso_code; # "ZJ" $geoip2_data_city city names zh-CN; # 杭州 $geoip2_data_city_longitude location longitude; # 120.161200 $geoip2_data_city_latitude location latitude; # 30.299400 $geoip2_data_city_time_zone location time_zone; # "Asia/Shanghai" # 中国以外的访问都是显示英文 $geoip2_data_country_en "default=United States" source=$remote_addr country names en; # United States $geoip2_data_country_code country iso_code; # US $geoip2_data_country_continent_en continent names en; # North America $geoip2_data_country_continent_code continent code; # NA $geoip2_data_province_name_en subdivisions 0 names en; # "" $geoip2_data_province_isocode subdivisions 0 names iso_code; # "" $geoip2_data_city city names en; # 杭州 $geoip2_data_city_longitude location longitude; # 120.161200 $geoip2_data_city_latitude location latitude; # 30.299400 $geoip2_data_city_time_zone location time_zone; # "Asia/Shanghai" } .... map $geoip2_data_country_code $CN { CN yes; TW yes; HK yes; MO yes; default no; } }
同样编辑conf.d/demo.conf, 此处使用虚拟主机头做演, 加入如下指令片段,其主要作用是根据区其地区,使用中英文显示请求者IP地理位置信息。
$ vim conf.d/demo.conf server { ... # 精准匹配 location = /api/v1/ip { # 当访问者IP来自 `CN|TW|HK|MO` 时将会以json的形式进行返回中文的IP地址信息。 if ( $geoip2_data_country_code ~* (CN|TW|HK|MO) ){ rewrite (.*) /api/v1/ip/cn last; } rewrite (.*) /api/v1/ip/en last; } # 中文显示 location /api/v1/ip/cn { default_type application/json; return 200 '{"ip":"$remote_addr","country":{"name": "$geoip2_data_country", "iso_code": "$geoip2_data_country_code", "continent": "$geoip2_data_country_continent","continent_code": "$geoip2_data_country_continent_code"},"province":{"name":"$geoip2_data_province_name","iso_code":"$geoip2_data_province_isocode"},"city":{"name":"$geoip2_data_city","timezone":"$geoip2_data_city_time_zone"},"location":{"longitude":"$geoip2_data_city_longitude","latitude":"$geoip2_data_city_latitude"}}'; } # 英文显示 location /api/v1/ip/en { default_type application/json; return 200 '{"ip":"$remote_addr","country":{"name": "$geoip2_data_country_en", "iso_code": "$geoip2_data_country_code", "continent": "$geoip2_data_country_continent_en","continent_code": "$geoip2_data_country_continent_code"},"province":{"name":"$geoip2_data_province_name_en","iso_code":"$geoip2_data_province_isocode"},"city":{"name":"$geoip2_data_city","timezone":"$geoip2_data_city_time_zone"},"location":{"longitude":"$geoip2_data_city_longitude","latitude":"$geoip2_data_city_latitude"}}'; } .... }
如果使用了cloudflare加速你的网站
这是默认获取的ip其实不是客户端ip而是cloudflare的cdn服务器IP,这时候在nginx.conf中的代码需要做调整,将source指定为$HTTP_CF_CONNECTING_IP,完整代码如下:
geoip2 /var/geoip2/GeoLite2-City.mmdb { auto_reload 60m; $geoip2_data_city_continent_code source=$HTTP_CF_CONNECTING_IP continent code; $geoip2_data_city_continent_name source=$HTTP_CF_CONNECTING_IP continent names zh-CN; $geoip2_data_city_country_code source=$HTTP_CF_CONNECTING_IP country iso_code; $geoip2_data_city_country_name source=$HTTP_CF_CONNECTING_IP country names zh-CN; $geoip2_data_city_region_code source=$HTTP_CF_CONNECTING_IP subdivisions 0 iso_code; $geoip2_data_city_region_name source=$HTTP_CF_CONNECTING_IP subdivisions 0 names zh-CN; $geoip2_data_city_location_latitude source=$HTTP_CF_CONNECTING_IP location latitude; $geoip2_data_city_location_longitude source=$HTTP_CF_CONNECTING_IP location longitude; $geoip2_data_city_postal source=$HTTP_CF_CONNECTING_IP postal code; $geoip2_data_city_city_name source=$HTTP_CF_CONNECTING_IP city names en; } geoip2 /var/geoip2/GeoIP2-ISP.mmdb { auto_reload 60m; $geoip2_data_isp_isp source=$HTTP_CF_CONNECTING_IP isp; $geoip2_data_isp_org source=$HTTP_CF_CONNECTING_IP organization; }
如果拥有了isp的库
你就可以通过在nginx.conf中添加以下代码
geoip2 /var/geoip2/GeoIP2-ISP.mmdb { $geoip2_data_isp_isp isp; $geoip2_data_isp_org organization; }