`

nginx log_format 记录自定义header信息

 
阅读更多
原文地址:http://notelifes.com/2013/07/nginx-log_format-%E8%AE%B0%E5%BD%95%E8%87%AA%E5%AE%9A%E4%B9%89header%E4%BF%A1%E6%81%AF/



文档地址:http://wiki.nginx.org/NginxHttpUpstreamModule


在开发云盘的时候,曾经定过一个协议,对于一些业务上可重试的逻辑,例如:数据库繁忙,请稍后在试;当前操作失败,请稍后在试;或者其他的一些个能够通过重试解决的错误,且不需要用户感知的,我们在返回的时候,使用的http状态吗是500;客户端会自动进行逻辑重试;

但是我们发现一个不方便的地方,没办法很直观的看到当前的500是因为什么而导致的,排查往往需要对着inner-error.log进行对应,比较不方便;但是我们又会将当前错误码通过header进行返回;

于是,是否有可能将header信息中的自定义内容输出到log中呢?看了一下http://wiki.nginx.org/HttpLogModule nginx的log模块,看到了如下让人惊喜的信息:

The headers, transmitted to client, begin from the prefix “sent_http_”, for example, $sent_http_content_range.
Note that variables produced by other modules can also be logged. For example you can log upstream response headers with the prefix “upstream_http_”, see upstream .
原来通过sent_http_+ 自定义头,就可以实现获取变量的内容了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics