• Nginx + GeoIP2 + libmaxminddb IP信息解析和地理定位,区域限制访问


    1.nginx安装ngx_http_geoip2_module 模块

    • 1.1
      首先下载nginx的第三方模块ngx_http_geoip2_module ,下载地址https://github.com/leev/ngx_http_geoip2_module/
    • 1.2
      然后对nginx增加ngx_http_geoip2_module模块
      #下载后解压至/home/user/
      #你的nginx安装目录下执行,如果你之前有手动安装过其他模块,也要在后面加上
      sudo ./configure --prefix=你的nginx安装路径 --add-module=/home/user/ngx_http_geoip2_module-master/
      sudo make
      #只执行make即可。正常情况下会在安装目录下的objs目录下生成新的nginx二进制文件,替换运行中nginx的即可,可把当前nginx备份一下。
      #替换后执行
      ./nginx -V
      # 即可看见添加的 ngx_http_geoip2_module 
      

    2.安装GeoIP2离线数据库

    • 2.1
      geoip2下载离线数据库库需要注册用户才可下载。官方网址https://www.maxmind.com/en/home
      如图所示

    • 2.2
      下载至本地后上传,解压。放到指定的目录下即可,自定义。后面配置的时候会用到这个文件所在路径

    3.安装libmaxminddb

    • 3.1
      下载 https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz 并编译

      tar -xzf libmaxminddb-1.3.2.tar.gz
      cd libmaxminddb-1.3.2
      ./configure
      make
      make check 
      sudo make install
      sudo ldconfig
      
    • 3.2
      执行完毕,可手动测试是否可行

      
      mmdblookup --file /opt/fy/3rd/GeoIP/GeoLite2-City_20210406/GeoLite2-City.mmdb --ip  220.181.38.148 
      # --file 后面跟的是上面解压后的mmdb文件地址
      # --ip 则是你要查询的ip
      
      

      正常会返回一个json数据

      
      {
          "city": 
            {
              "geoname_id": 
                1795565 <uint32>
              "names": 
                {
                  "de": 
                    "Shenzhen" <utf8_string>
                  "en": 
                    "Shenzhen" <utf8_string>
                  "es": 
                    "Shenzhen" <utf8_string>
                  "fr": 
                    "Shenzhen" <utf8_string>
                  "ja": 
                    "深セン市" <utf8_string>
                  "pt-BR": 
                    "Shenzhen" <utf8_string>
                  "ru": 
                    "Шэньчжэнь" <utf8_string>
                  "zh-CN": 
                    "深圳市" <utf8_string>
                }
            }
          "continent": 
            {
              "code": 
                "AS" <utf8_string>
              "geoname_id": 
                6255147 <uint32>
              "names": 
                {
                  "de": 
                    "Asien" <utf8_string>
                  "en": 
                    "Asia" <utf8_string>
                  "es": 
                    "Asia" <utf8_string>
                  "fr": 
                    "Asie" <utf8_string>
                  "ja": 
                    "アジア" <utf8_string>
                  "pt-BR": 
                    "Ásia" <utf8_string>
                  "ru": 
                    "Азия" <utf8_string>
                  "zh-CN": 
                    "亚洲" <utf8_string>
                }
            }
          "country": 
            {
              "geoname_id": 
                1814991 <uint32>
              "iso_code": 
                "CN" <utf8_string>
              "names": 
                {
                  "de": 
                    "China" <utf8_string>
                  "en": 
                    "China" <utf8_string>
                  "es": 
                    "China" <utf8_string>
                  "fr": 
                    "Chine" <utf8_string>
                  "ja": 
                    "中国" <utf8_string>
                  "pt-BR": 
                    "China" <utf8_string>
                  "ru": 
                    "Китай" <utf8_string>
                  "zh-CN": 
                    "中国" <utf8_string>
                }
            }
          "location": 
            {
              "accuracy_radius": 
                5 <uint16>
              "latitude": 
                22.533300 <double>
              "longitude": 
                114.133300 <double>
              "time_zone": 
                "Asia/Shanghai" <utf8_string>
            }
          "registered_country": 
            {
              "geoname_id": 
                1814991 <uint32>
              "iso_code": 
                "CN" <utf8_string>
              "names": 
                {
                  "de": 
                    "China" <utf8_string>
                  "en": 
                    "China" <utf8_string>
                  "es": 
                    "China" <utf8_string>
                  "fr": 
                    "Chine" <utf8_string>
                  "ja": 
                    "中国" <utf8_string>
                  "pt-BR": 
                    "China" <utf8_string>
                  "ru": 
                    "Китай" <utf8_string>
                  "zh-CN": 
                    "中国" <utf8_string>
                }
            }
          "subdivisions": 
            [
              {
                "geoname_id": 
                  1809935 <uint32>
                "iso_code": 
                  "GD" <utf8_string>
                "names": 
                  {
                    "en": 
                      "Guangdong" <utf8_string>
                    "fr": 
                      "Province de Guangdong" <utf8_string>
                    "zh-CN": 
                      "广东" <utf8_string>
                  }
              }
            ]
        }
      
      

      使用小技巧

      --ip 后面可以接json对应的tag
      例如 
      mmdblookup --file /home/user/GeoIP/GeoLite2-City_20210406/GeoLite2-City.mmdb --ip  1.1.1.1 country names en
      "China" <utf8_string>
      mmdblookup --file /home/user/GeoIP/GeoLite2-City_20210406/GeoLite2-City.mmdb --ip  1.1.1.1 city names zh-CN
      "深圳市" <utf8_string>
      

      ok,成功运行

    4. nginx 配置GeoIP2

    • 4.1
      所有国家代码的列表 https://dev.maxmind.com/geoip/legacy/codes/iso3166/
      
      #http 下新增
      geoip2 /home/user/GeoLite2-Country_20210406/GeoLite2-Country.mmdb{
                  #$geoip2_data_city_name source=$remote_addr country names en; 
                  $geoip2_data_country_code source=$remote_addr country iso_code;
      }
      #注意,这里的source是重点,把访问的真实ip当做来源配置。    
      #或者用map
      #配置1: 允许所有国家访问,除了美国和日本
      map $geoip2_data_country_code $allowed_country {
          default yes;
          US no;
          JP no;
      }
      
      #配置2: 只允许中国访问,可以在后面叠加可访问的国家iso_code;
      map $geoip2_data_country_code $allowed_country {
          default no;
          CN yes;
         
      }
      
      server 下增加
      
      #简单用法
      location / {
        if ($geoip2_data_country_code != CN ) { 
          return 403; 
          } 
        #当通过mmdb数据库解析出$remote_addr中的ip对应的iso_code !=CN时返回403,或进入特定的页面或者网站
      }
      #搭配map,配置1
      if ($allowed_country = yes) {
              return 403;
      }
      #搭配map,配置2
      if ($allowed_country = no) {
              return 403;
      }
      
      
      配好后重启nginx即可放心食用。
      p.s. 由于是离线的数据库,可手动进行更新,即替换最新数据文件
  • 相关阅读:
    Android 查看服务service是否正在运行 列出正在运行的服务
    应用获取root权限分析及总结
    图片高斯模糊效果
    Android的启动脚本–init.rc
    前台页面——js/jq 取/设值
    Access denied for user 'bruce'@'localhost' to database 'DBbruce'
    python基础-面向对象opp
    python基础-软件目录结构规范
    python基础-装饰器
    python基础列表生成式,迭代器
  • 原文地址:https://www.cnblogs.com/s42-/p/14668356.html
Copyright © 2020-2023  润新知