Use simple example below to add a unique HTTP X-Request-Id to each requests and access it in Symfony applications. Assuming that you have a Nginx and PHP-FPM based Symfony application.


Nginx config


server {
...

location ~ ^/index\.php(/|$) {
...
fastcgi_param HTTP_X_REQUEST_ID $request_id;

internal;
}

...
}

Symfony


config/services.yaml


parameters:
env(HTTP_X_REQUEST_ID): ~

services:
App\Controller\DockerController:
tags: ['controller.service_arguments']
arguments:
$xRequestId: '%env(string:HTTP_X_REQUEST_ID)%'

DockerController


class DockerController
{
private $xRequestId;

public function __construct(?string $xRequestId)
{
$this->xRequestId = $xRequestId;
}

public function index(Request $request): Response
{
print_r($_SERVER);
print_r($request->headers->all());

return new Response('X-Request-Id: '.$this->xRequestId);
}
}

Result


Array
(
...
[HTTP_X_REQUEST_ID] => c4c46e8c2d323d829dcb2ed1c5a3c48d
...
)

Array
(
...
[x-request-id] => Array
(
[0] => c4c46e8c2d323d829dcb2ed1c5a3c48d
)
...
)

X-Request-Id: c4c46e8c2d323d829dcb2ed1c5a3c48d