Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a request id in apache and forward it to underlying systems?

Tags:

logging

apache

I would like to add a unique id to each request done through apache which will be used in the access log and is forwarded to underlying systems in a header to be used in their logs.

Request id

What is the best solution to accomplish this?

like image 258
David Berg Avatar asked Nov 01 '16 12:11

David Berg


People also ask

Where does Apache write its incoming request logs?

In Linux, Apache commonly writes logs to the /var/log/apache2 or /var/log/httpd directories depending on your OS and Virtual Host overrides. You can also define a LogFormat string after the filename, which will only apply the format string to this file.

What is referer in Apache log?

In HTTP, "Referer" (a misspelling of Referrer) is the name of an optional HTTP header field that identifies the address of the web page (i.e., the URI or IRI), from which the resource has been requested. By checking the referrer, the server providing the new web page can see where the request originated.


1 Answers

mod_unique_id will provide an environment variable UNIQUE_ID with a unique identifier for each request. You can add it to request headers with:

RequestHeader set uniqueid %{UNIQUE_ID}e

If you add that header to apache logs, for example:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{uniqueid}i\"" combined 

you willl get something like:

10.0.2.2 - - [01/Nov/2016:23:12:40 +0000] "GET /index.html HTTP/1.1" 404 208 "WBkhaJRMNmj7U9aiFl2pzQAAAAA"
like image 107
Dusan Bajic Avatar answered Sep 28 '22 11:09

Dusan Bajic