在CentOS上配置Nginx以防止CC(Challenge Collapsar)攻击,可以通过以下几种方法来实现:
limit_req_zone和limit_reqlimit_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;
...
}
}
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;
...
}
}
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";
...
}
fail2banfail2ban可以监控日志文件并自动封禁恶意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 = ^<HOST> -.*"(GET|POST).*HTTP.*" 400
ignoreregex =
重启fail2ban:
sudo systemctl restart fail2ban
通过以上方法,你可以有效地防止CC攻击。建议根据你的具体需求和环境选择合适的配置。