【架构】Nginx如何设置X-Request-ID请求头,记录请求时间:毫秒?
Nginx is awesome, but it’s missing some common features. For instance, a common thing to add to access logs is a unique ID per request, so that you can track the flow of a single request through multiple services. Another thing it’s missing is the ability to log request_time in milliseconds, rather than seconds with a millisecond granularity. Using Lua, we can add these features ourselves.
I’ll show the whole solution, then I’ll break it down into parts:
http {
...
map $host $request_time_ms {
default '';
}
map $host $uuid {
default '';
}
lua_package_path '/etc/nginx/uuid4.lua';
init_by_lua '
uuid4 = require "uuid4"
math = require "math"
';
log_by_lua '
ngx.var.request_time_ms = math.floor(tonumber(ngx.var.request_time) * 1000)
';
log_format mycustomformat '[$time_local] "$request" $status $request_length $bytes_sent $request_time_ms $uuid';
access_log /var/log/nginx/access.log mycustomformat;
...
}
server {
...
set_by_lua $uuid '
if ngx.var.http_x_request_id == nil then
return uuid4.getUUID()
else
return ngx.var.http_x_request_id
end
';
...
}
It’s necessary to set variables before we use them in Lua. Using map is a trick to set variables in the http context (you can’t use set $variable ” in http). For the case of uuid, we are going to set it in the server section (during the rewrite context), but in case it’s not set, we want to avoid throwing errors. Here’s how we set these variables:
map $host $request_time_ms {
default '';
}
map $host $uuid {
default '';
}
Next we add a uuid4 library to our path, and include the libraries into our context:
lua_package_path '/etc/nginx/uuid4.lua';
init_by_lua '
uuid4 = require "uuid4"
math = require "math"
';
Using the log_by_lua function, we’ll set the request_time_ms variable we’ll use in the log_format config. This Lua function is called in the log context, before logs are written, allowing us to make the variables available to it:
log_by_lua '
ngx.var.request_time_ms = math.floor(tonumber(ngx.var.request_time) * 1000)
';
Next we set the log format, and use it for the access log:
log_format mycustomformat '[$time_local] "$request" $status $request_length $bytes_sent $request_time_ms $uuid';
access_log /var/log/nginx/access.log mycustomformat;
Lastly, we set the uuid during the rewrite context in the server section, using set_by_lua. To facilitate following a request across services, we’ll reuse the header if it’s already set. If the header isn’t set, then this request didn’t come from another service, so we’ll generate a UUID:
server {
...
set_by_lua $uuid '
if ngx.var.http_x_request_id == nil then
return uuid4.getUUID()
else
return ngx.var.http_x_request_id
end
'
...
}
If you’re trusting this header data in any way, you should be sure to filter/restrict that header appropriately so that the client can’t change it.
Update (Thursday December 11 2014): Edited the post to move the uuid generation into the server section and using set_by_lua, so that the uuid can be set to/from the header to flow through the stacks properly. Shout out to Asher Feldman for working out a better solution with me.
参考资料:
Using Lua in Nginx for unique request IDs and millisecond times in logs:http://ryandlane.com/blog/2014/12/11/using-lua-in-nginx-for-unique-request-ids-and-millisecond-times-in-logs/
Simple nginx lua script to add UUID to each request for end to end request tracking:https://gist.github.com/erikcw/e999e1fb438dbbb91533
Is there a way to log a per request unique id for nginx?:http://serverfault.com/questions/580394/is-there-a-way-to-log-a-per-request-unique-id-for-nginx
http://stackoverflow.com/questions/17748735/setting-a-trace-id-in-nginx-load-balancer
https://github.com/kali/nginx-operationid
【架构】Nginx如何设置X-Request-ID请求头,记录请求时间:毫秒?的更多相关文章
- request获取请求头和请求数据
package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...
- HTTP 请求头与请求体 - 某熊的全栈之路 - SegmentFault
本文从属于笔者的HTTP 理解与实践系列文章,对于HTTP的学习主要包含HTTP 基础.HTTP 请求头与请求体.HTTP 响应头与状态码.HTTP 缓存这四个部分,而对于HTTP相关的扩展与引申,我 ...
- 【转载】HTTP 请求头与请求体
原文地址: https://segmentfault.com/a/1190000006689767 HTTP Request HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典 ...
- HttpServletRequest对象,请求行、请求头、请求体
HttpServletRequest 公共接口类HttpServletRequest继承自ServletRequest.客户端浏览器发出的请求被封装成为一个HttpServletRequest对象.对 ...
- Django中获取参数(路径,查询,请求头,请求体)
一.通常HTTP协议向服务器传参有几种途径 : 提取URL的特定部分,如/weather/shanghai/2018,可以在服务器端的路由中用正则表达式截取: 查询字符串(query string), ...
- HTTP请求报文(请求行、请求头、请求体)
HTTP协议 1.简介 HTTP协议(Hyper Text Transfer Protocol,超文本传输协议),是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...
- iOS开发——网络篇——文件下载(NSMutableData、NSFileHandle、NSOutputStream)和上传、压缩和解压(三方框架ZipArchive),请求头和请求体格式,断点续传Range
一.小文件下载 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion ...
- HTTP请求行、请求头、请求体详解
HTTP 请求头各参数具体含义 Header 解释 示例Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/htmlAccept-Charset 浏览器可以接 ...
- HTTP请求行、请求头、请求体详解(转)
转自 https://blog.csdn.net/u010256388/article/details/68491509/ HTTP请求报文解剖 HTTP请求报文由3部分组成(请求行+请求头+ ...
- 【校招面试 之 网络】第3题 HTTP请求行、请求头、请求体详解
1.HTTP请求报文解剖 HTTP请求报文由3部分组成(请求行+请求头+请求体): 下面是一个实际的请求报文: ①是请求方法,GET和POST是最常见的HTTP方法,除此以外还包括DELETE.HEA ...
随机推荐
- 给定一列数字将其平移n位
原题的意思是给定一个指定长度的数组,然后接受一个数字m,将原数组前m位移动到最后,且顺序不变. 看到这个题,想到的第一个方法就是在用一个数组来储存改变后的数字,代码如下 int func(){ int ...
- Python实例 | 贴吧签到
第一步 查看HTTP请求的内容 首先需要通过浏览器或者其他http包分析软件来观察,签到的时候进行了什么请求. Firefox浏览器就足以做到这一点,Wireshark是更加专业的包分析软件,它除了能 ...
- macos不能打开windows samba共享问题(转载)
转自:https://www.macx.cn/thread-2095377-1-1.html?mod=viewthread&tid=2095377&extra=page%253D1&a ...
- Hadoop中文编码乱码相关问题
mapreduce程序处理GBK编码数据并输出GBK编码数据, hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出window系统默认的是GBK,有些格式文件 ...
- 10 个常用的 es6 特性
1. const and let 除了函数作用域之外,增加了块级作用域和常量.const 定义的绑定不可以修改,let定义的绑定在{ }不能访问.之前的 var 如果不在函数作用域内,相当于定义了一 ...
- Xamarin.Forms获取设备屏幕大小
Xamarin.Forms获取设备屏幕大小 可以借助device.Display获取.基本形式如下: var display = device.Display;然后就可以获取屏幕大小.display. ...
- Codeforces 961E 主席树
题意: 给出一个n个数的序列,求有几对(i,j)满足a[i]>=j&&a[j]>=i,(i,j)和(j,i)只能算一对. 考虑第i个数会有几个j(j<i)满足条件,首 ...
- 【Floyd】噪音恐惧症
[UVA10048]噪音恐惧症 题面略 试题分析:直接Floyd一下维护u到v的路径最大值最小就可以了,1A 代码: #include<iostream> #include<cstr ...
- 【FFT卷积】BZOJ3527-力
[题目大意] [思路] 很好这很FFT…… 想了半天也没明白到底什么是卷积∑的上下界,我当初学的时候没说一定要从0开始啊quq 我还是背不出FFT的模板我要狗带了 我上面写的什么乱七八糟的,要什么数学 ...
- [BZOJ1003](ZJOI 2006) 物流运输trans
[题目描述] 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟 ...