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/
配好后重啟nginx即可放心食用。#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; }
p.s. 由於是離線的數據庫,可手動進行更新,即替換最新數據文件