Home > other >  How can I display the headers sent in the request that results in an NGINX error page?
How can I display the headers sent in the request that results in an NGINX error page?

Time:11-30

I have set a custom location (with a minimal HTML file) for the error page in my Nginx location block. The function `location is:

location / {
    #try_files $uri $uri/ =404;
    proxy_set_header X-Real-IP $remote_addr;
    error_page 400 404 500 502 503 504 /test_error.html;
}

The client should be emitting an X-forwarded-for header with an IP address as the value on the right side of the colon. But how can I have the error status code, and the headers, on that error page? I'm running a test environment on a virtual box.

CodePudding user response:

Usually it is a backend web application job to work with the request HTTP headers, they are not available for HTML page even via JavaScript code. But there is a workaround using Server Side Includes with your HTML error page:

location = /test_error.html {
    internal;
    ssi on;
    root /path/to/html;
    # or 'alias /path/to/html/test_error.html;'
}

Now in your HTML error page you can use SSI echo commands to output nginx variables:

<head>
   ...
</head>
<body>
    <p>HTTP status is <!--# echo var="status" --></p>
    <p>X-Forwarded-For header value is <!--# echo var="http_x_forwarded_for" --></p>
</body>
  • Related