I was debugging an issue and really needed to know what values the nginx variables point to in production.

While asking Claude what might went wrong, it generates an interesting suggestion:

“You can try to debug by adding a location block that echoes back the headers:

location /test-headers {
    add_header Content-Type text/plain;
    return 200 "Remote Address: $remote_addr\nX-Forwarded-For: $proxy_add_x_forwarded_for\nX-Real-IP: $http_x_real_ip\n";

Then access http://your_domain.com/test-headers to see what values are being set.”

Here is what that block of code means:

  • add_header Content-Type text/plain; tells nginx to set the Content-Type header in the response to text/plain
  • return 200 "Remote Address: $remote_addr\nX-Forwarded-For: $proxy_add_x_forwarded_for\nX-Real-IP: $http_x_real_ip\n"; tells nginx to return a 200 status code with the text that follows as the response body.
    • $http_x_real_ip returns the http header x-real-ip from the request,
    • $proxy_add_x_forwarded_for is equal to $http_x_forwarded_for,$remote_addr, i.e. the value of the x-forwarded-for header if it exists with the IP address of the last hop.
    • $remote_addr variable is set when nginx’s http_realip_module is used.