在CentOS上配置Nginx以防止CC(Challenge Collapsar)攻击,可以通过以下几种方法来实现:
1. 使用limit_req_zone
和limit_req
limit_req_zone
和limit_req
模块可以限制请求的速率,从而防止CC攻击。
配置步骤:
-
编辑Nginx配置文件: 打开你的Nginx配置文件,通常位于
/etc/nginx/nginx.conf
或/etc/nginx/conf.d/default.conf
。 -
定义限速区域: 在
http
块中添加limit_req_zone
指令。http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s; ... }
-
应用限速规则: 在需要保护的
server
或location
块中添加limit_req
指令。server { ... location / { limit_req zone=mylimit burst=5; ... } }
2. 使用ngx_http_limit_conn_module
这个模块可以限制并发连接数,从而防止CC攻击。
配置步骤:
-
编辑Nginx配置文件: 打开你的Nginx配置文件。
-
定义连接限制区域: 在
http
块中添加limit_conn_zone
指令。http { limit_conn_zone $binary_remote_addr zone=addr:10m; ... }
-
应用连接限制规则: 在需要保护的
server
或location
块中添加limit_conn
指令。server { ... location / { limit_conn addr 10; ... } }
3. 使用ngx_http_security_headers_module
这个模块可以添加安全头,帮助防止一些常见的攻击。
配置步骤:
-
安装模块(如果尚未安装):
sudo yum install nginx-mod-http-security-headers
-
编辑Nginx配置文件: 打开你的Nginx配置文件。
-
添加安全头: 在
server
或location
块中添加安全头配置。server { ... add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection "1; mode=block"; ... }
4. 使用fail2ban
fail2ban
可以监控日志文件并自动封禁恶意IP地址。
安装和配置步骤:
-
安装fail2ban:
sudo yum install fail2ban
-
配置fail2ban: 编辑
/etc/fail2ban/jail.local
文件,添加以下内容:[nginx-cc] enabled = true filter = nginx-cc action = iptables-multiport[name=nginx-cc, port="http,https", protocol=tcp] logpath = /var/log/nginx/access.log bantime = 600 findtime = 600 maxretry = 3
-
创建过滤器: 编辑
/etc/fail2ban/filter.d/nginx-cc.conf
文件,添加以下内容:[Definition] failregex = ^
-.*"(GET|POST).*HTTP.*" 400 ignoreregex = -
重启fail2ban:
sudo systemctl restart fail2ban
通过以上方法,你可以有效地防止CC攻击。建议根据你的具体需求和环境选择合适的配置。