一:安裝geoip2依賴擴(kuò)展
sudo apt install libmaxminddb-dev -y
二:下載ngx_http_geoip2_module
git clone https://github.com/leev/ngx_http_geoip2_module.git
三:在寶塔面板里邊安裝nginx的時候選擇編譯安裝,并手動增加擴(kuò)展,安裝時候點(diǎn)擊啟用
模塊名稱 ngx_http_geoip2_module
模塊說明 禁用國家ip
模塊參數(shù) --add-module=/path/to/ngx_http_geoip2_module
等待安裝后輸入 nginx -V 查看是否安裝成功
四:在寶塔中安裝geoip
配置nginx
在nginx配置文件中,http段加入以下代碼。以寶塔安裝的nginx為例,打開/www/server /nginx/conf/nginx.conf文件,
在http中添加
geoip2 /usr/share/GeoIP/Country.mmdb {
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN yes;
JP no;
DE no;
}
在站點(diǎn)配置文件添加:
if ($allowed_country = no) {
return 403;
}
發(fā)表評論