---allow, deny
-ngx_http_access_module和ngx_stream_access_module模块,前者针对http请求,后者针对stream连接,它们的指令非常简单,仅包含allow和deny指令,唯一区别就是作用域不同
禁止所有的IP访问,192.168.110.100除外。
http {
server {
listen 80;
server_name localhost;
allow 192.168.110.100;
deny all;
}
}
# 禁止192.168.110.1访问
deny 192.168.110.1;
# 禁止192.168.110.1到192.168.255.254
deny 192.168.110.0/16;
# 禁止192.168.110.1到192.168.110.254
deny 192.168.110.0/24;
# 禁止所有的IP访问
deny all;Tips:如果指定了allow,需要配合deny使用,否则就是允许所有的IP地址访问。
---限制国家/城市
ngx_http_geoip_module来实现根据国家/城市进行访问限制
-配置
http {
include mime.types;
default_type application/octet-stream;
# 自定义日志格式
log_format geoip '$http_x_forwarded_for_temp - $remote_user [$time_local] - $request - $status - $geoip2_country_name_en - $geoip2_city_name_en';
geoip2 /usr/local/nginx/GeoLite2-Country.mmdb {
auto_reload 5m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_country_code source=$http_x_forwarded_for_temp country iso_code;
$geoip2_country_name_en source=$http_x_forwarded_for_temp country names en;
$geoip2_country_name_zh source=$http_x_forwarded_for_temp country names zh-CN;
}
geoip2 /usr/local/nginx/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_city_name_en source=$http_x_forwarded_for_temp city names en;
$geoip2_city_name_zh source=$http_x_forwarded_for_temp city names zh-CN;
}
server {
listen 80;
server_name localhost;
access_log logs/geoip.log geoip;
default_type text/html;
# 正则匹配取反
if ($geoip2_city_name_en !~ 'Shanghai'){
return 403 "<h1>Forbidden!</h1><p>You don't have permission to access the URL on this server.</p>";
}
location / {
return 200 '<p>Real IP: $http_x_forwarded_for_temp</p><p>Country: $geoip2_country_name_en</p><p>City: $geoip2_city_name_en</p>';
}
}
}
签名:这个人很懒,什么也没有留下!