Ngix geoip_module 讀取IP地域信息
基於IP地址匹配MaxMind GeoIP 二進制文件,讀取IP所在地域信息。
初始安裝並沒有這個模塊,需要自己安裝。
使用場景
-
區別國內外IP作爲HTTP訪問規則
-
區別國內城市地域作爲HTTP訪問規則
配置
加載模塊
在nginx.conf中加入:
load_module "modules/ngx_http_geoip_module.so"
load_module "modules/ngx_stream_geoip_module.so"
下載MaxMind的ip地域信息文件。
server內容如下:
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
listen 80;
server_name localhost;
location / {
if ($geoip_country_code != CN){
return 403;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
}
/myip
配置返回了訪問者的IP、國家名、國家碼、城市。便於訪問自己的出口IP。
/
配置了非中國IP的HTTP訪問限制