通过Nginx日志减少服务器负载可以从多个方面入手,主要包括日志轮转、日志分析和日志压缩等。以下是具体的优化措施:
/var/log/nginx/access.log {
daily;
rotate 14;
missingok;
notifempty;
compress;
delaycompress;
create 0640 www-data www-data;
sharedscripts;
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 cat /var/run/nginx.pid;
fi
endscript
}
# 统计PV、UV数
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20
# 统计访问最多的Referer
cat access.log | awk -F ' ' '{print $10}' | sort | uniq -c | sort -nr | head -20
# 统计访问最高的URI
cat access.log | awk -F ' ' '{print $9}' | sort | uniq -c | sort -nr | head -20
# 统计访问最多的IP和User-Agent
cat access.log | awk -F ' ' '{print $1}' | sort | uniq -c | sort -nr | head -20
http {
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain application/xml application/json application/javascript text/css;
}
通过上述措施,可以有效减少Nginx日志对服务器资源的占用,提高服务器的整体性能。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何通过Debian Nginx日志监控服务器负载