说明:日志中心指的是服务端,站点指的是客户端(你的网站)
日志中心配置
脚本
curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/deploy_server.sh && chmod +x deploy_server.sh
默认路径:/data/nginx_logs
./deploy_server.sh
日志轮转文件
cat /etc/logrotate.d/nginx-log-center
创建errors目录, 防止脚本没有创建
mkdir -p /data/nginx_logs/errors
查看日志大小
cd /data/nginx_logs/active
du -h * | sort -h
清除配置
curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/cleanup_server.sh && chmod +x cleanup_server.sh && ./cleanup_server.sh
站点日志配置
- 添加日志格式
/etc/nginx/nginx.conf 在原本的log下方添加
log_format syslog_combined
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
- 站点日志脚本
curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/deploy_client.sh && chmod +x deploy_client.sh
运行脚本
./deploy_client.sh <日志中心IP> all
注意:转发后的日志不会保存在本地
站点恢复
BACKUP_DIR=$(ls -d /root/rsyslog_backup_* | tail -1) && \
cp -f "${BACKUP_DIR}/nginx.conf" /etc/nginx/ && \
cp -f "${BACKUP_DIR}/sites-available/"* /etc/nginx/sites-available/ && \
nginx -t && systemctl reload nginx && echo "✅ Nginx 配置已恢复并重载"
清理配置
curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/cleanup_rsyslog_client.sh && chmod +x cleanup_rsyslog_client.sh && ./cleanup_rsyslog_client.sh
清理备份
rm -rf rsyslog_backup_*