Skip to content

Instantly share code, notes, and snippets.

@dbvan
Forked from oroce/README.md
Created April 14, 2020 05:58
Show Gist options
  • Save dbvan/565221242c7ab681acd5540e11f7d6b2 to your computer and use it in GitHub Desktop.
Save dbvan/565221242c7ab681acd5540e11f7d6b2 to your computer and use it in GitHub Desktop.
nginx request id
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'request-id:"$sent_http_x_request_id"';
access_log logs/access.log main;
server {
server_name localhost;
set $rid $request_id;
if ($http_x_request_id != '') {
set $rid $http_x_request_id;
}
if ($arg_requestId != '') {
set $rid $arg_requestId;
}
add_header X-Request-Id $rid;
root html;
index index.html index.htm;
}
}

Auto generating X-Request-Id

Command:

curl -I 192.168.0.100

Output:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Thu, 12 Jun 2014 07:34:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 11 Jun 2014 19:48:59 GMT
Connection: keep-alive
ETag: "5398b2ab-264"
X-Request-Id: 7ffce2be-30c9-4412-a8f8-8042f2657f4f
Accept-Ranges: bytes

Log:

192.168.0.120 - - [12/Jun/2014:09:34:40 +0200] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.30.0" "-" request-id:"7ffce2be-30c9-4412-a8f8-8042f2657f4f"

Read from header

Command:

curl -I -H "X-Request-Id: request id from header" localhost

Output:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Thu, 12 Jun 2014 07:40:25 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 11 Jun 2014 19:48:59 GMT
Connection: keep-alive
ETag: "5398b2ab-264"
X-Request-Id: request id from header
Accept-Ranges: bytes

Log:

192.168.0.120 - - [12/Jun/2014:09:40:25 +0200] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.30.0" "-" request-id:"request id from header"

Read from querystring

Command:

curl -I localhost?requestId=requestId-from-querystring

Output:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Thu, 12 Jun 2014 07:42:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 11 Jun 2014 19:48:59 GMT
Connection: keep-alive
ETag: "5398b2ab-264"
X-Request-Id: requestId-from-querystring
Accept-Ranges: bytes

Log:

192.168.0.120 - - [12/Jun/2014:09:42:42 +0200] "HEAD /?requestId=requestId-from-querystring HTTP/1.1" 200 0 "-" "curl/7.30.0" "-" request-id:"requestId-from-querystring"

Mixed (header and querystring)

Command:

curl -I -H "X-Request-Id: request-id from header" localhost?requestId=requestId-from-querystring

Output:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Thu, 12 Jun 2014 07:45:02 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 11 Jun 2014 19:48:59 GMT
Connection: keep-alive
ETag: "5398b2ab-264"
X-Request-Id: requestId-from-querystring
Accept-Ranges: bytes
var img = new Image();
var src = 'http://nginx-server.com/image.jpg';
var requestId = uuid.v4();
img.onerror = function() {
// send an error to your server including requestId
// and you can debug why the error happened
};
img.onload = function() {
// now you can send analytics to your server
// {
// "requestId": requestId,
// "src": src,
// "performance": window.performance.getEntries().slice(-1)
//}
};
img.src = src + '?requestId=' + requestId;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment