I sent one request through NGINX to a FastAPI application. The same request ID came back in the response and turned up in both logs:
X-Request-ID: 0f7ed905ed2ad690a91044ab073fc167
api-1 | INFO: request_id=0f7ed905ed2ad690a91044ab073fc167 path=/echo
nginx-1 | request_id=0f7ed905ed2ad690a91044ab073fc167 "GET /echo?message=hello HTTP/1.1" status=200 upstream=172.18.0.2:8000 upstream_status=200
Then I changed the path from /echo to /missing. The response still had an ID. So did NGINX’s log. The application’s line carrying the ID was gone.
The rest of this page is the lab that produced those lines, what each variation showed, and what the configuration decides on the proxy’s behalf.
Following one request
Here is the successful request step by step. It is the same one the machine sends on a loop.
01The client asks
A plain request to the only port the lab publishes. Nothing in it identifies it yet.
GET /echo?message=hello HTTP/1.1 Host: 127.0.0.1:8088 Accept: */*02NGINX names the request
NGINX generates $request_id: sixteen random bytes, written as 32 hexadecimal characters.
$request_id = 7390507326cb5983b2657a4cb954846c03NGINX forwards it
Four proxy_set_header lines rewrite the request. The Host header is passed through unchanged; the other three are set by NGINX.
GET /echo?message=hello HTTP/1.1 Host: 127.0.0.1:8088 X-Forwarded-For: (the address NGINX saw) X-Forwarded-Proto: http X-Request-ID: 7390507326cb5983b2657a4cb954846c04FastAPI logs, then answers
The handler reads the header, writes one log line, and puts the same value in the JSON. This is the first record.
request_id=7390507326cb5983b2657a4cb954846c path=/echo HTTP/1.1 200 OK content-type: application/json {"service":"fastapi","message":"hello","request_id":"7390507326cb5983b2657a4cb954846c","forwarded_proto":"http"}05NGINX stamps the response
add_header puts the ID on the response, and "always" keeps it there for error responses too. The client now holds the second record.
HTTP/1.1 200 OK Server: nginx/1.30.7 Content-Type: application/json X-Request-ID: 7390507326cb5983b2657a4cb954846c {"service":"fastapi","message":"hello","request_id":"7390507326cb5983b2657a4cb954846c","forwarded_proto":"http"}06NGINX writes its access log
Last, because access logs are written when a request finishes. This is the third record, and the only one that shows the upstream’s status as well.
request_id=7390507326cb5983b2657a4cb954846c "GET /echo?message=hello HTTP/1.1" status=200 upstream=172.18.0.2:8000 upstream_status=200
The evidence
This is what happened, recorded on 26 November 2026 with NGINX 1.30.7 (nginx:stable-alpine) and Docker 29.8.2 on an Apple silicon Mac. The output is real. I removed health-check lines from the logs, and in the three shorter captures I left out response headers that do not matter there.
One request, three records
curl -si 'http://127.0.0.1:8088/echo?message=hello'
HTTP/1.1 200 OK
Server: nginx/1.30.7
Date: Thu, 26 Nov 2026 06:39:55 GMT
Content-Type: application/json
Content-Length: 112
Connection: keep-alive
X-Request-ID: 0f7ed905ed2ad690a91044ab073fc167
{"service":"fastapi","message":"hello","request_id":"0f7ed905ed2ad690a91044ab073fc167","forwarded_proto":"http"}
The same 32 characters appear in the header and in the body. Then the two logs:
api-1 | INFO: request_id=0f7ed905ed2ad690a91044ab073fc167 path=/echo
api-1 | INFO: 172.18.0.3:52970 - "GET /echo?message=hello HTTP/1.1" 200 OK
nginx-1 | request_id=0f7ed905ed2ad690a91044ab073fc167 "GET /echo?message=hello HTTP/1.1" status=200 upstream=172.18.0.2:8000 upstream_status=200
The second API line is Uvicorn’s own access log. It shows the peer FastAPI saw: 172.18.0.3, which is NGINX’s address on the Compose network, not my Mac. The addresses will differ on another machine.
An error still carries the ID
curl -si 'http://127.0.0.1:8088/missing'
HTTP/1.1 404 Not Found
Server: nginx/1.30.7
Content-Type: application/json
X-Request-ID: 9a12590cf9d3b4e6a76fd17bdc0ef475
api-1 | INFO: 172.18.0.3:52986 - "GET /missing HTTP/1.1" 404 Not Found
nginx-1 | request_id=9a12590cf9d3b4e6a76fd17bdc0ef475 "GET /missing HTTP/1.1" status=404 upstream=172.18.0.2:8000 upstream_status=404
This is always doing its job. It also answers the opening question. Uvicorn still logged the 404, but our /echo handler never ran, and that handler is what writes the line with the request ID. For this request the ID exists in two records, not three, and only NGINX’s log connects it to the upstream.
A client that brings its own ID
curl -si -H 'X-Request-ID: chosen-by-client' 'http://127.0.0.1:8088/echo?message=spoof'
HTTP/1.1 200 OK
X-Request-ID: 928b414d16e3988e62abab01c4dee569
{"service":"fastapi","message":"spoof","request_id":"928b414d16e3988e62abab01c4dee569","forwarded_proto":"http"}
The client’s value is gone. FastAPI received NGINX’s.
A request that skips the proxy
Run from inside the API container, straight to port 8000:
docker compose exec -T api python -c "from urllib.request import urlopen; print(urlopen('http://127.0.0.1:8000/echo?message=direct').read().decode())"
{"service":"fastapi","message":"direct","request_id":"","forwarded_proto":""}
api-1 | INFO: request_id= path=/echo
api-1 | INFO: 127.0.0.1:47108 - "GET /echo?message=direct HTTP/1.1" 200 OK
In this capture the direct request carried no X-Request-ID, so the handler logged an empty value. There is no forwarded protocol and no NGINX line at all.
How the lab is built
I wanted a place where I could watch one request go through a proxy and account for every mark it left. It is two containers and about forty lines of configuration: NGINX as a reverse proxy in front of a FastAPI application, both run by Docker Compose.
services:
nginx:
image: nginx:stable-alpine
ports:
- "127.0.0.1:8088:80"
depends_on:
api:
condition: service_healthy
api:
build: ./api
Two details matter here.
Only NGINX publishes a port, and it binds to 127.0.0.1, so the lab is not reachable from the network. The API publishes nothing. From my Mac, the only way to reach FastAPI is through the proxy.
The lab uses port 8088 instead of 8080 on purpose. Burp’s proxy listens on 8080 by default, and I wanted both running without changing either.
What NGINX adds
The whole proxy is one location block.
location / {
proxy_pass http://api:8000;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
add_header X-Request-ID $request_id always;
}
$request_id is the centre of it. NGINX generates sixteen random bytes for every request and exposes them as 32 hexadecimal characters. The configuration uses that one value three times: it sends it upstream as a header, it returns it to the client as a header, and it writes it to the access log.
| Directive | What it does |
|---|---|
proxy_set_header Host $http_host |
Passes the client’s original Host header upstream, port included |
proxy_set_header X-Forwarded-For $remote_addr |
Replaces the header with the address NGINX saw |
proxy_set_header X-Request-ID $request_id |
Replaces the header with NGINX’s own ID |
add_header X-Request-ID $request_id always |
Adds the ID to the response, including error responses |
Where each record is written
The application writes its line inside the handler:
@app.get("/echo")
def echo(request: Request, message: str = "hello") -> dict[str, str]:
request_id = request.headers.get("x-request-id", "")
logger.info("request_id=%s path=%s", request_id, request.url.path)
return {
"service": "fastapi",
"message": message,
"request_id": request_id,
"forwarded_proto": request.headers.get("x-forwarded-proto", ""),
}
NGINX writes its line from a custom format:
log_format lab 'request_id=$request_id "$request" status=$status '
'upstream=$upstream_addr upstream_status=$upstream_status';
Both lines begin with request_id=, so one search finds the pair. The order is worth noticing: FastAPI’s line appears first, because NGINX writes its access log only when the request has finished.
The NGINX line carries two status codes. status is what the client received and upstream_status is what the upstream, FastAPI, returned. They match in these captures. The format is there for the day they do not.
What the configuration decides for you
Reading the block again with an attacker’s questions in mind, three choices stand out.
The client cannot choose its own request ID. proxy_set_header replaces any X-Request-ID the client sends. A caller cannot plant an ID to confuse the logs or collide with another request. The evidence above shows chosen-by-client being replaced.
The client cannot plant entries in X-Forwarded-For. The header is set to $remote_addr, the address of the connection NGINX accepted, and whatever the client sent is discarded. The common alternative, $proxy_add_x_forwarded_for, appends to the client’s value, and an application that trusts the first entry in that list is trusting the client.
This has a limit worth stating. $remote_addr is who connected to NGINX, which is not always the user. Put a load balancer or another proxy in front and every request appears to come from that proxy. The configuration closes one spoofing path; it does not establish the original client address, and with more hops that needs a list of proxies you trust.
This lab does not demonstrate the X-Forwarded-For behaviour directly, because /echo does not return that header. It is the one claim on this page that rests on the configuration and NGINX’s documentation, not on captured output.
A direct call carries whatever ID it was given. The handler reads the X-Request-ID header it receives and defaults to an empty string. In my capture, the direct request sent no such header, and the handler logged request_id= with nothing after it. A caller inside the Compose network that sets the header itself could log any value it likes, so an empty or unfamiliar ID is a signal, not proof.
With Burp in the middle
To watch the same exchange from the client’s side, I open Burp’s own browser and visit the lab through it. Burp then sits between the browser and NGINX, and its HTTP history shows the request as sent and the response as received, with the X-Request-ID header that ties it to both logs.
Requests from my editor do not go through Burp. They go straight to port 8088.
Run it
The whole lab is seven files: the Compose file, the NGINX configuration and the FastAPI app. Download nginx-fastapi.zip, unzip it, and run:
cd nginx-fastapi
docker compose up --build --wait
curl -i 'http://127.0.0.1:8088/echo?message=hello'
docker compose logs --tail=20 nginx api
Look for X-Request-ID in the response headers, then find the same value in the JSON and in both logs. A request to /missing shows how NGINX records an upstream 404. Run docker compose down when you are done.
Questions or corrections? Message me on LinkedIn.