以屏蔽47.92.79.0到47.92.79.255整个IP段为例。
在 Nginx 配置中屏蔽 47.92.79.0/24 整个 IP 段(即 47.92.79.0 到 47.92.79.255),可以在 http、server 或 location 块中使用 deny 指令。
方法 1:直接在 nginx.conf 或站点配置中屏蔽
http {
# 屏蔽 47.92.79.0/24 整个 IP 段
deny 47.92.79.0/24;
allow all;
server {
listen 80;
server_name example.com;
location / {
# 其他配置...
}
}
}
说明:
deny 47.92.79.0/24; 表示拒绝该 IP 段的所有访问。
allow all; 允许其他 IP 访问(如果只屏蔽某些 IP,建议保留 allow all 避免误封)。
方法 2:在 server 或 location 块中屏蔽
如果只想在某个 server 或 location 生效:
server {
listen 80;
server_name example.com;
# 屏蔽 47.92.79.0/24
deny 47.92.79.0/24;
allow all;
location / {
# 其他配置...
}
}
方法 3:单独屏蔽多个 IP 段
如果需要屏蔽多个 IP 段:
deny 47.92.79.0/24;
deny 123.45.67.0/24;
allow all;
验证配置并重启 Nginx
1、检查语法是否正确:
nginx -t
2、重启 Nginx 生效:
systemctl restart nginx # 或 service nginx restart
3、测试是否生效:
使用 curl 或浏览器访问,看是否返回 403 Forbidden。
或者用 iptables 临时模拟该 IP 访问:
bash curl --interface 47.92.79.1 http://example.com
额外优化
结合 fail2ban 自动封禁恶意 IP(适用于频繁攻击的情况)。
使用 geo 模块管理大量 IP 黑名单:
http {
geo $blocked_ip {
default 0;
47.92.79.0/24 1;
123.45.67.0/24 1;
}
server {
if ($blocked_ip) {
return 403;
}
}
}
这样配置后,47.92.79.0/24 的所有 IP 访问都会被 Nginx 拒绝。
