Debian Nginx 日志安全的防范要点
一 核心风险与防护目标
二 部署与权限加固
location ~* \.(log|txt)$ {
deny all;
return 403;
}
三 日志内容与传输安全
http {
log_format safe '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"SESSIONID=${cookie_SESSIONID:+***}"';
access_log /var/log/nginx/access.log safe;
error_log /var/log/nginx/error.log warn;
}
四 轮转、保留与监控阻断
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0644 root adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
sudo apt-get install fail2ban
# /etc/fail2ban/jail.local 示例
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
action = iptables[name=nginx-http-auth, port="http,https", protocol=tcp]
logpath = /var/log/nginx/access.log
maxretry = 3
bantime = 600
常用命令:sudo systemctl restart fail2ban、fail2ban-client status。五 快速核查清单