Nginx系列(十一)——通过日志进行故障排查

Debugging and Troubleshooting with Access Logs, Error Logs, and Request Tracing
故障排查

Configuring Access Logs
请求日志配置
http {
log_format geoproxy #日志格式命名为geoproxy
'[$time_local] $remote_addr '
'$realip_remote_addr $remote_user '
'$request_method $server_protocol '
'$scheme $server_name $uri $status '
'$request_time $body_bytes_sent '
'$geoip_city_country_code3 $geoip_region '
'"$geoip_city" $http_x_forwarded_for '
'$upstream_status $upstream_response_time '
'"$http_referer" "$http_user_agent"';
...
}
使用以上格式的日志
server {
access_log /var/log/nginx/access.log geoproxy;
...
}


Configuring Error Logs
配置错误日志
error_log /var/log/nginx/error.log warn; #levels的值可以是debug,info,notice,warn,error,
crit,alert,emerg . 这个功能需要nginx在配置时使用了with-debug选项


Forwarding to Syslog
转发系统日志
使用以下配置
error_log syslog:server=10.0.1.42 debug;
access_log syslog:server=10.0.1.42,tag=nginx,severity=info geoproxy;
syslog的服务器可以是elk等常见的日志集中分析平台


Request Tracing
请求跟踪
log_format trace '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for" $request_id';
upstream backend {
server 10.0.0.42;
}
server {
listen 80;
add_header X-Request-ID $request_id; # Return to client。添加一个request_id值到日志中
location / {
proxy_pass http://backend;
proxy_set_header X-Request-ID $request_id; #Pass to app。添加request id 到header中
access_log /var/log/nginx/access_trace.log trace;
}
}

posted on 2020-05-24 23:31  标配的小号  阅读(568)  评论(0编辑  收藏  举报

导航