megnlingling 发表于 2018-11-10 06:35:35

nginx增加GeoIP模块

  Linux安装GeoIP
  附件地址:链接: https://pan.baidu.com/s/1dFl1zZN 密码: x37s
  ./configure
  make
  make install
  如果原已经安装好的nginx,现在需要添加一个未被编译安装的模块
  1.nginx -V 可以查看原来编译时都带了哪些参数
  原来的参数:
  –prefix=/app/nginx
  添加后的参数:
  ./configure –prefix=/app/nginx –with-http_geoip_module
  2.编译
  make (千万不要make install,否则就是覆盖安装了)
  3.备份原有./nginx
  4.把编译完的nginx(在安装目录的objs文件夹下)二进制文件,复制到/usr/local/nginx/sbin/
  cp -rf ./objs/nginx /app/nginx/sbin/
  5../nginx -V 测试成功
  在操作过程中遇到错误:
  /usr/local/nginx/sbin/nginx:
  error while loading shared libraries: libGeoIP.so.1: cannot open shared object file: No such file or directory
  解决方案:
  查看一下类库路径
  ldd /usr/local/nginx/sbin/nginx
  结果:
  这里写图片描述
  ln -s /usr/local/lib/libGeoIP.so* /lib64/
  ldconfig
  /usr/local/nginx/sbin/nginx -V 牛逼解决
  附加个使用说明:
  http {
  ...
  geoip_city    /var/GeoIP/GeoLiteCity.dat;#geoip的库文件
  #handler中可以接收header中的值(Geo-City的key 等)
  proxy_set_header    Geo-Country-Code $geoip_city_country_code;
  proxy_set_header    Geo-Region $geoip_region;
  proxy_set_header    Geo-City $geoip_city;
  proxy_set_header    Geo-Postal-Code $geoip_postal_code;
  proxy_set_header    Geo-Continent-Code $geoip_city_continent_code;
  proxy_set_header    Geo-Latitude $geoip_latitude;
  proxy_set_header    Geo-longitude $geoip_longitude;
  ...
  server{
  ...
  #下面代码可以放到 location模块下
  add_header geoip_city $geoip_city;
  add_header geoip_city_country_code $geoip_city_country_code;
  add_header geoip_region $geoip_region;
  add_header geoip_postal_code $geoip_postal_code;
  add_header geoip_city_continent_code $geoip_city_continent_code;
  add_header geoip_latitude $geoip_latitude;
  add_header geoip_longitude $geoip_longitude;
  ...
  }
  }
  ·$geoip_country_code - 两个字母的国家代码,如:"RU", "US"。
  ·$geoip_country_code3 - 三个字母的国家代码,如:"RUS", "USA"。
  ·$geoip_country_name - 国家的完整名称,如:"Russian Federation", "United States"(如果可用)。
  ·$geoip_region - 地区的名称(类似于省,地区,州,行政区,联邦土地等),如:"Moscow City", "DC"(如果可用)。
  ·$geoip_city - 城市名称,如"Moscow", "Washington"(如果可用)。
  ·$geoip_postal_code - 邮政编码(如果可用)。
  ·$geoip_city_continent_code(如果可用)。
  ·$geoip_latitude - 所在维度(如果可用)。
  ·$geoip_longitude - 所在经度(如果可用)。
  附件:
  GeoLiteCity.dat
  及省市区字典sql
  https://pan.baidu.com/s/1o8B73cQ
  附一篇不错的文章
  http://www.52os.net/articles/configure-nginx-using-geoip-allow-whitelist.html

页: [1]
查看完整版本: nginx增加GeoIP模块