要優化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請求對系統性能的影響,并提高整體運行效率。