2020年,最新NGINX的ngx_http_geoip2模塊以精准禁止特定國家或者地區IP訪問


1.0 geoip2核心識別庫

    安裝geoip2 lib步驟:

cd /usr/local/src
rm -f libmaxminddb-1.4.2.tar.gz
wget https://github.com/maxmind/libmaxminddb/releases/download/1.4.2/libmaxminddb-1.4.2.tar.gz
tar -xzf libmaxminddb-1.4.2.tar.gz
cd libmaxminddb-1.4.2
yum install gcc gcc-c++ make -y
./configure
make
make check
sudo make install

echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
sudo ldconfig

 

 

2.0 下載ngx_http_geoip2_module 模塊

cd /usr/local/src
wget https://github.com/leev/ngx_http_geoip2_module/archive/3.3.tar.gz
tar -xzf 3.3.tar.gz
mv ngx_http_geoip2_module-3.3 ngx_http_geoip2_module 

nginx集成步驟:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1
useradd -M -s /sbin/nologin www
yum install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel -y ./configure --user=www --group=www --prefix=/usr/local/nginx \ --with-ld-opt="-Wl,-rpath -Wl,/usr/local/lib" \ --with-http_sub_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_ssl_module \ --with-http_v2_module \ --add-module=/usr/local/src/ngx_http_geoip2_module make make install

 

geoip2 IP地址庫下載:

2020年最新GeoLite2-City.mmdb 無法直接下載,必須注冊maxmind賬號

01. 需要在maxmind 后台注冊賬號,並且生成Account/User ID 和 License key
02. 安裝geoipupdate, 下載地址https://github.com/maxmind/geoipupdate/releases
03. 配置geoipupdate的GeoIP.conf , 填寫maxmind賬號的User ID 和 License key 和 EditionIDs

博主使用的是centos 7
安裝如下
cd /usr/local/src/
wget https://github.com/maxmind/geoipupdate/releases/download/v4.2.0/geoipupdate_4.2.0_linux_amd64.rpm rpm -ivh geoipupdate_4.2.0_linux_amd64.rpm rpm -ql geoipupdate vi /etc/GeoIP.conf #填寫AccountID XXXX #填寫LicenseKey XXXX #EditionIDs可以不修改,系統默認有填 GeoLite2-Country GeoLite2-City #筆者EditionIDs只保留GeoLite2-City #保存退出 運行geoipupdate /usr/bin/geoipupdate cd /usr/share/GeoIP/ 會看到GeoLite2-City.mmdb 把GeoLite2-City.mmdb文件cp到需要使用的目錄
sudo mkdir -p /usr/local/nginx/geoip/
\cp -rf /usr/share/GeoIP/GeoLite2-City.mmdb /usr/local/nginx/geoip/maxmind-city.mmdb

 

注意GeoLite2 City 和GeoLite2 Country 兩個IP庫,請下載City的mmdb數據文件,較於其他兩者信息更豐富

 

nginx 配置geoip2 樣例,geoip2的配置字段在http

http {
    ...
geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb {
    $geoip2_data_country_code default=US source=$remote_addr country iso_code;
    $geoip2_data_country_name country names en;
    $geoip2_data_city_name default=London city names en;
    $geoip2_data_province_name subdivisions 0 names en;
    $geoip2_data_province_isocode subdivisions 0 iso_code;
}
    ....

    fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
    fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
    fastcgi_param CITY_NAME    $geoip2_data_city_name;
    ....
}

stream {
    ...
geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb {
    $geoip2_data_country_code default=US source=$remote_addr country iso_code;
    $geoip2_data_country_name country names en;
    $geoip2_data_city_name default=London city names en;
    $geoip2_data_province_name subdivisions 0 names en;
    $geoip2_data_province_isocode subdivisions 0 iso_code;
    }
    ...
}

nginx配置中的變量名如,geoip2_data_country_code,geoip2_data_country_name 等等,都是自定義的名稱,可以加在日志字段中

添加 Nginx 配置 獲取訪問者的IP和國家代碼。

location /get_ip {
    default_type text/plain;
    return 200 "$remote_addr $geoip2_data_country_code\n";
}

 

 

3.0 在nginx 中配置黑名單國家的變量 $blacklist_country

在http{} 字段,任何include之前,添加如下配置

[...]
    map geoip2_data_country_code $allowed_country {
        default yes;
        US no;
        JP no;
        SG no;
    }
[...]

以上配置將允許所有的國家,除了美國,日本,和新加坡 (您可以查看所有國家代碼的列表,點這里

相反的,如果你想阻止所有國家,除了少數幾個國家可以訪問,請參考如下的配置

[...]
    map geoip2_data_country_code $allowed_country {
        default no;
        CN yes;
        HK yes;
        US yes;
    }
[...]

 

現在,你做了如上配置,並不會阻止任何的國家,那只是設置了一個變量$allowed_country
想要實際阻止國家/地區,必須修改vhost的配置
把下面的代碼放到server{}字段, 這個代碼也可以放到location{}字段

[...]
        if ($allowed_country = no) {
            return 403;
        }
[...]

任何從黑名單國家的用戶訪問,都會收到403錯誤代碼,

修改完配置,不要忘記reload nginx

/usr/local/nginx/sbin/nginx -s reload

 

 

4.0 通過如下命令可以直接本地查詢IP信息

/usr/local/bin/mmdblookup --file /usr/local/nginx/geoip/maxmind-city.mmdb --ip 8.8.8.8

會出來許多信息,用json格式展示

  {
    "country":
      {
        "geoname_id":
          6252001 <uint32>
        "iso_code":
          "US" <utf8_string>
        "names":
          {
            "de":
              "USA" <utf8_string>
            "en":
              "United States" <utf8_string>
          }
      }
  }

 

如果IP 后面可以跟不同的查詢,如下面查詢了國家的英文名

/usr/local/bin/mmdblookup --file /usr/local/nginx/geoip/maxmind-city.mmdb --ip 8.8.8.8 country names en
  "United States" <utf8_string>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM