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 theContent-Typeheader in the response totext/plainreturn 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_ipreturns the http headerx-real-ipfrom the request,$proxy_add_x_forwarded_foris equal to$http_x_forwarded_for,$remote_addr, i.e. the value of thex-forwarded-forheader if it exists with the IP address of the last hop.$remote_addrvariable is set when nginx’s http_realip_module is used.