主理人序
当然选择自动更新啊!
GEOIP2(GeoLite2)介绍
GEOIP2官方页面(GeoLite2免费地理位置数据):
https://dev.maxmind.com/geoip/geoip2/geolite2/ ;
下载及自动更新数据库可参考如下教程;
注册GeoLite2账号
官方页面中部分段落机翻如下:
To receive access to download the GeoLite2 databases or query the GeoLite2 web services at no charge, sign up for a GeoLite2 account.
Once you’ve set up an account, login to your account portal and select “Download Databases” to access the databases, or generate a license key to begin querying the web services (see more information in the Integration section below). You may also be interested in our guide to Getting Started with GeoLite2.
要免费下载GeoLite2数据库或查询GeoLite2 Web服务,请 注册 一个GeoLite2帐户。
设置帐户后,登录到 帐户门户/用户中心 并选择“下载数据库”以访问数据库,或生成许可证密钥以开始查询Web服务(请参阅下面的“集成”部分中的更多信息)。您可能也对我们的《GeoLite2入门指南》感兴趣。
下载GeoLite2数据库
2.登陆 用户中心;
3.找到 Download Databases 并点击即可到达下载页面;
4.查看 你的license-key;
5.数据是支持自动更新的,参阅,或参考在本文后文部分;
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数据库
1.下载并安装 geoipupdate 更新程序;
以 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
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 设置自动运行;
$ find / -name "geoipupdate"
/usr/share/doc/geoipupdate
/usr/bin/geoipupdate
$ crontab -e
32 15 * * 4 /usr/bin/geoipupdate
Got it!
# 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.国别代码;
版权属于:毒奶
联系我们:https://limbopro.com/6.html
毒奶搜索:https://limbopro.com/search.html
机场推荐:https://limbopro.com/865.html IEPL专线/100Gb/¥15/月起
毒奶导航:https://limbopro.com/daohang/index.html本文链接:https://limbopro.com/archives/15531.html
本文采用 CC BY-NC-SA 4.0 许可协议,转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!