Nginx+geoipupdate实现自动更新国家IP库

前两天部署了一个拦截境外IP访问的东西,但是国家IP数据库是一直都在变的,并不是说下载一次数据库就可以,所以我们还需要做一个自动更新的功能来实现自动更新IP数据库,这样就不用人工更新了
一、环境

1、CentOS7.9
2、MaxMind ID和LicenseKey
3、geoipupdate

二、官网

CentOS7: http://isoredirect.centos.org/centos/7/isos/x86_64/
MaxMind: https://maxmind.com
geoipupdate: https://github.com/maxmind/geoipupdate/releases

三、步骤
1、获取geoipupdate官方工具
1
2
3
4
wget https://github.com/maxmind/geoipupdate/releases/download/v6.0.0/geoipupdate_6.0.0_linux_386.tar.gz  
tar -zxvf geoipupdate_6.0.0_linux_386.tar.gz
mv geoipupdate /usr/local/nginx/IPData/
mv GeoIP.conf /usr/local/nginx/IPData/
2、创建LicenseKey

创建LicenseKey1
创建LicenseKey2
创建LicenseKey3
创建LicenseKey4
创建LicenseKey5

3、配置GeoIP.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Please see https://dev.maxmind.com/geoip/updating-databases?lang=en for
# instructions on setting up geoipupdate, including information on how to
# download a pre-filled GeoIP.conf file.

# Replace YOUR_ACCOUNT_ID_HERE and YOUR_LICENSE_KEY_HERE with an active account
# ID and license key combination associated with your MaxMind account. These
# are available from https://www.maxmind.com/en/my_license_key.
AccountID xxxx #这里填写刚才记下的ID
LicenseKey xxxx_xxxxxxxxxxxxxxx_xxx #这里填写刚才记下的key

# Enter the edition IDs of the databases you would like to update.
# Multiple edition IDs are separated by spaces.
EditionIDs GeoLite2-Country #这里根据需求,只需要国家库就保留Country,需要城市库就改成GeoLite2-City,两个都需要的话,就写GeoLite2-Country GeoLite2-City

# The remaining settings are OPTIONAL.

# The directory to store the database files. Defaults to /usr/local/share/GeoIP
DatabaseDirectory /usr/local/nginx/IPData/ #这里本来是注释掉的,取消注释修改成你数据库存放的位置

# The server to use. Defaults to "updates.maxmind.com".
# Host updates.maxmind.com

# The proxy host name or IP address. You may optionally specify a
# port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080
# will be used.
# Proxy 127.0.0.1:8888

# The user name and password to use with your proxy server.
# ProxyUserPassword username:password

# Whether to preserve modification times of files downloaded from the server.
# Defaults to "0".
# PreserveFileTimes 0

# The lock file to use. This ensures only one geoipupdate process can run at a
# time.
# Note: Once created, this lockfile is not removed from the filesystem.
# Defaults to ".geoipupdate.lock" under the DatabaseDirectory.
LockFile /usr/local/nginx/IPData/.geoipupdate.lock #这里本来是注释掉的,取消注释修改成你数据库存放的位置

# The amount of time to retry for when errors during HTTP transactions are
# encountered. It can be specified as a (possibly fractional) decimal number
# followed by a unit suffix. Valid time units are "ns", "us" (or "µs"), "ms",
# "s", "m", "h".
# Defaults to "5m" (5 minutes).
# RetryFor 5m

# The number of parallel database downloads.
# Defaults to "1".
# Parallelism 1

GeoIP配置

3、启动geoipupdate

根据官方提供的参数来看,默认数据库会下到/usr/local/share下
所以我们要加个参数让它存放到/usr/local/nginx/IPData
-d跟–database-directory一样,指定数据库存放位置
-f跟–config-file一样,就是指定配置文件

1
./geoipupdate -d /usr/local/nginx/IPData -f /usr/local/nginx/IPData/GeoIP.conf

等一会它就更新完了,更新完后用ll命令看下时间,如果时间是当前时间,那就说明更新成功。

4、创建定时任务,每天早上10点自动更新一次
1
2
3
4
创建一个shell脚本
mkdir -p /usr/local/script/logs/
cd /usr/local/nginx/IPData/
vim autoupdate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
########################################################################
# Function :自动更新IP数据库 #
# Platform :Centso 6.x and 7.x (Base) #
# Version :2.0 #
# C_Date :2023-08-01 #
# U_Date :2023-08-01 #
# Author :福建省智网云科科技有限公司 #
# Contact :StephenJose_Dai #
# Tips :Please don't delete it #
# Used :确保/usr/local/nginx/IPData下有geoipupdate和GeoIP.conf文件#
# 再把脚本放入 /usr/local/script/下,然后运行即可 #
# Log :用于自动更新国家IP数据库和城市IP数据库的程序 #
########################################################################
current_time=$(date "+%Y-%m-%d %H:%M:%S")
/usr/local/nginx/IPData/geoipupdate -d /usr/local/nginx/IPData/ -f /usr/local/nginx/IPData/GeoIP.conf
echo "[${current_time}] UpdateSuccessfully!"
1
chmod +x autoupdate.sh
1
2
crontab -e
00 10 * * * /usr/local/nginx/IPData/autoupdate.sh >> /usr/local/script/logs/geoipupdate.log 2>&1
1
2
重启crond服务
systemctl restart crond
四、总结

大致过程就是利用官方提供的geoipupdate配合LicenseKey向MaxMind发起合法的数据库下载请求,从而达到自动更新IP库的目的。

戴戴的Linux

文章目录
  1. 1. 前两天部署了一个拦截境外IP访问的东西,但是国家IP数据库是一直都在变的,并不是说下载一次数据库就可以,所以我们还需要做一个自动更新的功能来实现自动更新IP数据库,这样就不用人工更新了
  • 一、环境
  • 二、官网
  • 三、步骤
    1. 1. 1、获取geoipupdate官方工具
    2. 2. 2、创建LicenseKey
    3. 3. 3、配置GeoIP.conf
  • 3、启动geoipupdate
  • 4、创建定时任务,每天早上10点自动更新一次
  • 四、总结


  • 本站总访问量 本文总阅读量