- nginx
- nginx-x-rid-header module (https://github.com/newobj/nginx-x-rid-header)
We want to track image downloads from client side and in javascript we cannot get the header values:(.
I used a terrible log format, you'd probably change that.
We want to track image downloads from client side and in javascript we cannot get the header values:(.
I used a terrible log format, you'd probably change that.
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; | |
} | |
} |
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"
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"
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"
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; |