【问题标题】:How can I display the headers sent in the request that results in an NGINX error page?如何显示导致 NGINX 错误页面的请求中发送的标头?
【发布时间】:2022-01-02 04:15:24
【问题描述】:

我在我的 Nginx location 块中为错误页面设置了一个自定义位置(带有最小的 HTML 文件)。函数`location是:

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;
}

客户端应该发出一个 X-forwarded-for 标头,其 IP 地址作为冒号右侧的值。但是如何在该错误页面上显示错误状态代码和标题?我在一个虚拟盒子上运行一个测试环境。

【问题讨论】:

    标签: nginx x-forwarded-for


    【解决方案1】:

    通常使用请求 HTTP 标头是后端 Web 应用程序作业,即使通过 JavaScript 代码,它们也不适用于 HTML 页面。但是有一个解决方法是在您的 HTML 错误页面中使用 Server Side Includes

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

    现在在您的 HTML 错误页面中,您可以使用 SSI echo 命令输出 nginx 变量:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2012-12-27
      • 1970-01-01
      • 2016-08-16
      • 2018-04-05
      • 2018-01-02
      相关资源
      最近更新 更多