确保你拥有服务器root权限,并已安装Nginx 1.18+版本。通过以下命令验证:
``` nginx -v ```若未安装,在Ubuntu/Debian系统执行:
``` sudo apt update sudo apt install nginx -y ```在CentOS/RHEL系统执行:
``` sudo yum install epel-release sudo yum install nginx -y ```编辑Nginx配置文件,通常位于/etc/nginx/nginx.conf或/etc/nginx/sites-available/default。在server块内添加:
``` location / { allow 192.168.1.100; deny all; } ```此配置仅允许IP 192.168.1.100访问,拒绝其他所有IP。allow和deny指令的顺序至关重要,Nginx按顺序匹配,遇到第一个匹配规则即停止。
要允许整个网段访问,使用CIDR表示法:
``` location /admin { allow 10.0.0.0/24; allow 172.16.0.0/16; deny all; } ```此配置允许10.0.0.0到10.0.0.255,以及172.16.0.0到172.16.255.255的IP访问/admin路径。
创建黑名单配置文件/etc/nginx/blockips.conf:
``` deny 203.0.113.5; deny 198.51.100.0/24; ```在主配置文件的http块内引入:
``` include /etc/nginx/blockips.conf; ```需要更新黑名单时,直接编辑blockips.conf文件,然后重载Nginx:
``` sudo nginx -s reload ```首先安装所需依赖和数据库:
``` Ubuntu/Debian sudo apt install libnginx-mod-http-geoip2 CentOS/RHEL 8+ sudo dnf install nginx-module-geoip2 ```下载最新的GeoIP2国家数据库:
``` sudo mkdir -p /usr/share/GeoIP cd /usr/share/GeoIP sudo wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz sudo tar -xzf GeoLite2-Country.tar.gz --strip-components=1 ```在nginx.conf的http块顶部添加:
``` load_module modules/ngx_http_geoip2_module.so; http { geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb { $geoip2_country_code country iso_code; } map $geoip2_country_code $allowed_country { default yes; CN no; 阻止中国IP RU no; 阻止俄罗斯IP KP no; 阻止朝鲜IP } } ```在server块中应用规则:
``` server { if ($allowed_country = no) { return 403; } } ```使用htpasswd工具创建认证文件,先安装apache2-utils:
``` Ubuntu/Debian sudo apt install apache2-utils CentOS/RHEL sudo yum install httpd-tools ```创建用户密码文件:
``` sudo htpasswd -c /etc/nginx/.htpasswd username1 ```系统会提示输入密码。添加第二个用户时去掉-c参数:
``` sudo htpasswd /etc/nginx/.htpasswd username2 ```在需要保护的location块中添加:
``` location /private { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/.htpasswd; } ```阻止恶意爬虫和扫描器:
``` location / { if ($http_user_agent ~ (wget|curl|scrapy|python-requests)) { return 403; } } ```在http块中定义限制区域:
``` http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { location /api { limit_req zone=one burst=5 nodelay; } } } ```
此配置限制每个IP对/api路径每秒最多1个请求,允许突发5个请求。
防止单个IP消耗过多连接:
``` http { limit_conn_zone $binary_remote_addr zone=addr:10m; server { limit_conn addr 10; } } ```在nginx.conf的http块中添加日志格式:
``` http { log_format restricted '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$geoip2_country_code"'; access_log /var/log/nginx/access.log restricted; } ```创建监控脚本/usr/local/bin/monitor_blocks.sh:
``` !/bin/bash tail -f /var/log/nginx/access.log | grep -E " 403 | 444 " ```赋予执行权限:
``` sudo chmod +x /usr/local/bin/monitor_blocks.sh ```使用awk分析被拒绝的IP:
``` sudo awk '$9 == 403 {print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20 ```以下是一个完整的server块配置,整合了多种限制方式:
``` server { listen 80; server_name example.com; IP白名单(管理后台) location /admin { allow 192.168.1.0/24; allow 10.0.0.50; deny all; auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.htpasswd_admin; } API接口频率限制 location /api { limit_req zone=api_limit burst=10; limit_conn api_conn 20; 阻止特定User-Agent if ($http_user_agent ~ (bot|crawler|spider)) { return 403; } } 国家限制(公开站点) location / { if ($geoip2_country_code ~ (CN|RU)) { return 403; } } 记录所有403错误到单独日志 error_log /var/log/nginx/restriction_errors.log; } ```在应用配置前,务必测试语法:
``` sudo nginx -t ```看到"nginx: configuration file /etc/nginx/nginx.conf test is successful"表示配置正确。
应用配置:
``` sudo systemctl reload nginx ```测试访问控制是否生效:
``` 测试IP白名单 curl -I http://example.com/admin 测试基础认证 curl -u username:password http://example.com/private 测试频率限制 for i in {1..15}; do curl http://example.com/api; done ```问题1:配置修改后不生效
检查配置是否正确加载:
``` sudo nginx -T | grep -A5 -B5 "your_location" ```问题2:误封自己IP
临时通过服务器控制台添加允许规则:
``` sudo iptables -A INPUT -p tcp -s YOUR_IP --dport 80 -j ACCEPT ```问题3:GeoIP国家识别错误
更新GeoIP数据库:
``` cd /usr/share/GeoIP sudo wget -O GeoLite2-Country.tar.gz https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_LICENSE&suffix=tar.gz sudo tar -xzf GeoLite2-Country.tar.gz --strip-components=1 sudo nginx -s reload ```1. 每周检查并清理旧日志:
``` sudo find /var/log/nginx -name ".log" -mtime +30 -delete ```2. 每月更新GeoIP数据库
3. 每季度审查访问规则,移除不再需要的限制
4. 监控系统负载,调整连接数限制参数
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图