Nginx配置分析器:
# 使用gixy扫描Nginx配置
sudo pip install gixy
gixy /etc/nginx/nginx.conf
其他扫描工具:
# 使用nmap扫描开放端口和服务
sudo nmap -sV -T4 -A -v your-server-ip
# 使用Nikto进行Web服务器扫描
nikto -h your-domain.com
# 使用testssl.sh检查SSL配置
./testssl.sh your-domain.com
# 使用SSL Labs在线测试
# 访问: https://www.ssllabs.com/ssltest/
# 禁用server tokens(隐藏Nginx版本信息)
server_tokens off;
# 限制HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
# 启用XSS保护
add_header X-XSS-Protection "1; mode=block";
# 防止MIME类型嗅探
add_header X-Content-Type-Options nosniff;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_ecdh_curve secp384r1;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# 限制连接频率
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
# 限制并发连接数
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
limit_conn addr 10;
limit_req zone=one burst=5;
}
# 禁止敏感文件访问
location ~* /(\.git|\.env|\.htaccess|\.svn) {
deny all;
return 404;
}
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.example.com; img-src 'self' data: https://*.example.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; frame-src 'self' https://www.youtube.com; object-src 'none'";
add_header Referrer-Policy "strict-origin-when-cross-origin";
add_header Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()";
# 安装Fail2Ban
sudo apt-get install fail2ban
# 配置Nginx相关规则
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
在/etc/fail2ban/jail.local
中添加:
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
[nginx-badbots]
enabled = true
filter = apache-badbots
port = http,https
logpath = /var/log/nginx/access.log
maxretry = 2
设置定期任务检查Nginx配置和日志:
# 每周检查一次配置
0 0 * * 0 /usr/sbin/nginx -t && systemctl reload nginx
# 每日分析日志中的可疑活动
0 1 * * * /usr/bin/fail2ban-client status
nginx
autoindex off;
通过以上措施,您可以显著提高Nginx服务器的安全性,并建立有效的监控机制来及时发现潜在威胁。