要优化Nginx日志中的GET请求,可以采取以下措施:
warn或error,以减少不必要的信息记录。error_log /var/log/nginx/error.log warn;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
access_log_buffer_size和error_log_buffer_size来提高日志写入性能。http {
...
access_log_buffer_size 16k;
error_log_buffer_size 16k;
...
}
log_by_lua_file模块或第三方模块如ngx_http_log_module的异步日志功能。http {
...
access_log off;
...
server {
...
location / {
log_by_lua_file '/path/to/async_log.lua';
...
}
}
}
ngx_http_limit_req_module限制请求速率,防止恶意攻击和突发流量。http {
...
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
...
server {
...
location / {
limit_req zone=mylimit burst=5;
...
}
}
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}
http {
...
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
...
}
logrotate工具定期清理和压缩旧日志文件。/etc/logrotate.d/nginx
{
daily
missingok
rotate 7
compress
notifempty
create 0640 root adm
}
通过以上措施,可以有效减少Nginx日志中的GET请求对系统性能的影响,并提高整体运行效率。