GeoIP.png

主理人序

当然选择自动更新啊!

GEOIP2(GeoLite2)介绍

P.S 我们不需要官方提供的 web 相关API服务,我们会把整个数据库下载下来自行查询;

GEOIP2官方页面(GeoLite2免费地理位置数据)
https://dev.maxmind.com/geoip/geoip2/geolite2/

下载及自动更新数据库可参考如下教程;

注册GeoLite2账号

官方页面中部分段落机翻如下

GeoLite2 - 注册并登陆账号门户/用户中心(account portal) - 点击 Download Databases .png

英语的朋友可以完全参阅: GeoLite2入门指南 ,可以非常快上手;或可继续参考下文进行操作;

下载GeoLite2数据库

1.注册 GeoLite2 账号;
2.登陆 用户中心
3.找到 Download Databases 并点击即可到达下载页面;
4.查看 你的license-key
5.数据是支持自动更新的,参阅,或参考在本文后文部分;

在这里我们选择的格式是 .mmdb,而不是CSV;

Download GeoIP2 and GeoIP Legacy Databases

以下载 GeoLite2 ASN 为例:

Database Details Download Links
GeoLite2 ASN Edition ID: GeoLite2-ASNFormat: GeoIP2 Binary (.mmdb) (APIs)Updated: 2021-04-06 Download GZIP Download SHA256 Get Permalinks

下载到本地:(指你的电脑而不是服务器) 直接点击 Download GZIP 即可下载;
下载到服务器: (如你的VPS),点击 Get Permalinks,出现如下提示;

# Database URL
https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=YOUR_LICENSE_KEY&suffix=tar.gz

# SHA256 URL
https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=YOUR_LICENSE_KEY&suffix=tar.gz.sha256

查看你的 YOUR_LICENSE_KEY

服务器终端输入:

wget https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=YOUR_LICENSE_KEY&suffix=tar.gz

Got it!

解压后就得到 ***.mmdb 文件了;更多玩法参阅:GeoLite2入门指南

.mmdb 查询语句

安装 mmdblookup

$ apt-get install libmaxminddb-dev

mmdblookup 查询语法

mmdblookup –file [FILE PATH] –ip [IP ADDRESS] [DATA PATH]

示例

$ mmdblookup --file /usr/share/GeoIP/GeoLite2-Country.mmdb --ip 8.8.8.8
$ mmdblookup --file /usr/share/GeoIP/GeoLite2-Country.mmdb  --ip 8.8.8.8 registered_country iso_code
  "US" <utf8_string> #获取国别代码

更多命令参数:

$ mmdblookup -h 

自动更新GeoLite2数据库

英语好的朋友可以参阅:Automatic Updates for GeoIP2 and GeoIP Legacy Databases;或可继续参考下文;

1.下载并安装 geoipupdate 更新程序;

Github 下载地址发布页

以 Ubuntu server 安装 geoipupdate_4.6.0_linux_386.deb 举例;

wget https://github.com/maxmind/geoipupdate/releases/download/v4.6.0/geoipupdate_4.6.0_linux_386.deb
dpkg -i geoipupdate_4.6.0_linux_386.deb
正在选中未选择的软件包 geoipupdate:i386。
(正在读取数据库 ... 系统当前共安装有 225443 个文件和目录。)
准备解压 geoipupdate_4.6.0_linux_386.deb  ...
正在解压 geoipupdate:i386 (4.6.0) ...
正在设置 geoipupdate:i386 (4.6.0) ...

2.修改 GeoIP.conf;

Ubuntu server 安装 deb 文件后,默认配置在 /etc/GeoIP.conf

Julia@localhost:~# find / -name "GeoIP.conf"
/etc/GeoIP.conf
/usr/local/etc/GeoIP.conf
/usr/share/doc/geoipupdate/GeoIP.conf

或参考此处修改配置文件 GeoIP.conf

$ geoipupdate #运行 geoipupdate 得到如下提示
error loading configuration file /etc/GeoIP.conf: invalid account ID format: strconv.Atoi: parsing "YOUR_ACCOUNT_ID_HERE": invalid syntax
vi /etc/GeoIP.conf #修改该配置文件

在相应位置填入相应信息:

# GeoIP.conf file - used by geoipupdate program to update databases
# from http://www.maxmind.com
AccountID YOUR_ACCOUNT_ID_HERE
LicenseKey YOUR_LICENSE_KEY_HERE
EditionIDs YOUR_EDITION_IDS_HERE

查看你的账户ID&LICENSE_KEY

EDITION_IDS 产品ID可在下载数据库查看;如 GeoLite2-ASN,这就是 Edition ID;

GeoLite2 - 注册并登陆账号门户/用户中心(account portal) - 点击 Download Databases .png

Database Details Download Links
GeoLite2 ASN Edition ID: GeoLite2-ASNFormat: GeoIP2 Binary (.mmdb) (APIs)Updated: 2021-04-06 Download GZIPDownload SHA256Get Permalinks

3.运行GEOIP Update程序

Run geoipupdate. To fully automate this process on Linux or Unix, use a crontab file like:

4.配置 crontab 设置自动运行;

geoipupdate 程序具体安装路径请使用 find 命令查找;
$ find / -name "geoipupdate"
/usr/share/doc/geoipupdate
/usr/bin/geoipupdate
$ crontab -e
32 15 * * 4 /usr/bin/geoipupdate  

Got it!

Ubuntu server 下 下载的数据库存储在 /usr/share/GeoIP 目录,具体位置请以 GeoIP.conf 文件描述为准;
# The directory to store the database files. Defaults to /usr/share/GeoIP
# DatabaseDirectory /usr/share/GeoIP
root@localhost:/usr/share/GeoIP# tree
.
├── GeoIP.dat
├── GeoIPv6.dat
├── GeoLite2-ASN.mmdb
├── GeoLite2-City.mmdb
└── GeoLite2-Country.mmdb

0 directories, 5 files

国家代码(全)

对应的 mmdblookup 查询命令

mmdblookup --file /usr/share/GeoIP/GeoLite2-Country.mmdb  --ip 8.8.8.8 country iso_code

在 nginx 配置文件中的表示方法(示例,变量名称自拟)

http {

## GEOIP2 国家
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_Country_code country iso_code;
}

: server {
: listen          80;
: server_name     www.domain1.com;
: access_log      logs/domain1.access.log main;
: location / {
: index index.html;
: root  /var/www/domain1.com/htdocs;

if ($geoip2_Country_code = (HK|US)){
return 403;
}

: }
: }

}

国家代码 Country Code

A1,"Anonymous Proxy"
A2,"Satellite Provider"
O1,"Other Country"
AD,"Andorra"
AE,"United Arab Emirates"
AF,"Afghanistan"
AG,"Antigua and Barbuda"
AI,"Anguilla"
AL,"Albania"
AM,"Armenia"
AO,"Angola"
AP,"Asia/Pacific Region"
AQ,"Antarctica"
AR,"Argentina"
AS,"American Samoa"
AT,"Austria"
AU,"Australia"
AW,"Aruba"
AX,"Aland Islands"
AZ,"Azerbaijan"
BA,"Bosnia and Herzegovina"
BB,"Barbados"
BD,"Bangladesh"
BE,"Belgium"
BF,"Burkina Faso"
BG,"Bulgaria"
BH,"Bahrain"
BI,"Burundi"
BJ,"Benin"
BL,"Saint Barthelemy"
BM,"Bermuda"
BN,"Brunei Darussalam"
BO,"Bolivia"
BQ,"Bonaire, Saint Eustatius and Saba"
BR,"Brazil"
BS,"Bahamas"
BT,"Bhutan"
BV,"Bouvet Island"
BW,"Botswana"
BY,"Belarus"
BZ,"Belize"
CA,"Canada"
CC,"Cocos (Keeling) Islands"
CD,"Congo, The Democratic Republic of the"
CF,"Central African Republic"
CG,"Congo"
CH,"Switzerland"
CI,"Cote d'Ivoire"
CK,"Cook Islands"
CL,"Chile"
CM,"Cameroon"
CN,"China"
CO,"Colombia"
CR,"Costa Rica"
CU,"Cuba"
CV,"Cape Verde"
CW,"Curacao"
CX,"Christmas Island"
CY,"Cyprus"
CZ,"Czech Republic"
DE,"Germany"
DJ,"Djibouti"
DK,"Denmark"
DM,"Dominica"
DO,"Dominican Republic"
DZ,"Algeria"
EC,"Ecuador"
EE,"Estonia"
EG,"Egypt"
EH,"Western Sahara"
ER,"Eritrea"
ES,"Spain"
ET,"Ethiopia"
EU,"Europe"
FI,"Finland"
FJ,"Fiji"
FK,"Falkland Islands (Malvinas)"
FM,"Micronesia, Federated States of"
FO,"Faroe Islands"
FR,"France"
GA,"Gabon"
GB,"United Kingdom"
GD,"Grenada"
GE,"Georgia"
GF,"French Guiana"
GG,"Guernsey"
GH,"Ghana"
GI,"Gibraltar"
GL,"Greenland"
GM,"Gambia"
GN,"Guinea"
GP,"Guadeloupe"
GQ,"Equatorial Guinea"
GR,"Greece"
GS,"South Georgia and the South Sandwich Islands"
GT,"Guatemala"
GU,"Guam"
GW,"Guinea-Bissau"
GY,"Guyana"
HK,"Hong Kong"
HM,"Heard Island and McDonald Islands"
HN,"Honduras"
HR,"Croatia"
HT,"Haiti"
HU,"Hungary"
ID,"Indonesia"
IE,"Ireland"
IL,"Israel"
IM,"Isle of Man"
IN,"India"
IO,"British Indian Ocean Territory"
IQ,"Iraq"
IR,"Iran, Islamic Republic of"
IS,"Iceland"
IT,"Italy"
JE,"Jersey"
JM,"Jamaica"
JO,"Jordan"
JP,"Japan"
KE,"Kenya"
KG,"Kyrgyzstan"
KH,"Cambodia"
KI,"Kiribati"
KM,"Comoros"
KN,"Saint Kitts and Nevis"
KP,"Korea, Democratic People's Republic of"
KR,"Korea, Republic of"
KW,"Kuwait"
KY,"Cayman Islands"
KZ,"Kazakhstan"
LA,"Lao People's Democratic Republic"
LB,"Lebanon"
LC,"Saint Lucia"
LI,"Liechtenstein"
LK,"Sri Lanka"
LR,"Liberia"
LS,"Lesotho"
LT,"Lithuania"
LU,"Luxembourg"
LV,"Latvia"
LY,"Libyan Arab Jamahiriya"
MA,"Morocco"
MC,"Monaco"
MD,"Moldova, Republic of"
ME,"Montenegro"
MF,"Saint Martin"
MG,"Madagascar"
MH,"Marshall Islands"
MK,"Macedonia"
ML,"Mali"
MM,"Myanmar"
MN,"Mongolia"
MO,"Macao"
MP,"Northern Mariana Islands"
MQ,"Martinique"
MR,"Mauritania"
MS,"Montserrat"
MT,"Malta"
MU,"Mauritius"
MV,"Maldives"
MW,"Malawi"
MX,"Mexico"
MY,"Malaysia"
MZ,"Mozambique"
NA,"Namibia"
NC,"New Caledonia"
NE,"Niger"
NF,"Norfolk Island"
NG,"Nigeria"
NI,"Nicaragua"
NL,"Netherlands"
NO,"Norway"
NP,"Nepal"
NR,"Nauru"
NU,"Niue"
NZ,"New Zealand"
OM,"Oman"
PA,"Panama"
PE,"Peru"
PF,"French Polynesia"
PG,"Papua New Guinea"
PH,"Philippines"
PK,"Pakistan"
PL,"Poland"
PM,"Saint Pierre and Miquelon"
PN,"Pitcairn"
PR,"Puerto Rico"
PS,"Palestinian Territory"
PT,"Portugal"
PW,"Palau"
PY,"Paraguay"
QA,"Qatar"
RE,"Reunion"
RO,"Romania"
RS,"Serbia"
RU,"Russian Federation"
RW,"Rwanda"
SA,"Saudi Arabia"
SB,"Solomon Islands"
SC,"Seychelles"
SD,"Sudan"
SE,"Sweden"
SG,"Singapore"
SH,"Saint Helena"
SI,"Slovenia"
SJ,"Svalbard and Jan Mayen"
SK,"Slovakia"
SL,"Sierra Leone"
SM,"San Marino"
SN,"Senegal"
SO,"Somalia"
SR,"Suriname"
SS,"South Sudan"
ST,"Sao Tome and Principe"
SV,"El Salvador"
SX,"Sint Maarten"
SY,"Syrian Arab Republic"
SZ,"Swaziland"
TC,"Turks and Caicos Islands"
TD,"Chad"
TF,"French Southern Territories"
TG,"Togo"
TH,"Thailand"
TJ,"Tajikistan"
TK,"Tokelau"
TL,"Timor-Leste"
TM,"Turkmenistan"
TN,"Tunisia"
TO,"Tonga"
TR,"Turkey"
TT,"Trinidad and Tobago"
TV,"Tuvalu"
TW,"Taiwan"
TZ,"Tanzania, United Republic of"
UA,"Ukraine"
UG,"Uganda"
UM,"United States Minor Outlying Islands"
US,"United States"
UY,"Uruguay"
UZ,"Uzbekistan"
VA,"Holy See (Vatican City State)"
VC,"Saint Vincent and the Grenadines"
VE,"Venezuela"
VG,"Virgin Islands, British"
VI,"Virgin Islands, U.S."
VN,"Vietnam"
VU,"Vanuatu"
WF,"Wallis and Futuna"
WS,"Samoa"
YE,"Yemen"
YT,"Mayotte"
ZA,"South Africa"
ZM,"Zambia"
ZW,"Zimbabwe"

附注

1.Restricting Access by Geographical Location
2.国别代码

最后修改:2022 年 03 月 26 日 11 : 01 AM