Nginx log_format性能参数$request_time $upstream_response_time
log_format是指存储日志的时候所采用的格式,可以在/etc/nginx//nginx.conf的http字段中设置
默认配置如下:
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;
在分析性能过程,有时需要分析请求耗时,可以添加$request_time $upstream_response_time两个参数实现:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status"'; access_log /var/log/nginx/access.log main;
完整案例:
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { map $http_upgrade $connection_upgrade { default upgrade; '' close; } include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" $upstream_addr ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; server_tokens off; add_header X-Frame-Options SAMEORIGIN; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
具体参数解释如下: