【问题标题】:Using Apache HTTPD to proxy requests and when in error set status 200 and the error document使用 Apache HTTPD 代理请求和错误时设置状态 200 和错误文档
【发布时间】:2016-07-21 19:32:13
【问题描述】:

我正在使用 Apache HTTPD 2.4.12 将请求代理到远程服务器。如果远程主机返回错误代码,例如 502,那么我需要它来处理它并返回状态为 200 的 custom 错误页面。这是针对无法处理的应用程序问题的解决方法非 200 错误很好,需要特定的有效负载。

我只能让它返回状态为 502(没有 RewriteRule)的自定义错误页面或状态设置为 200 的标准 OK 页面。这是我的 httpd.conf 的一部分,它复制了后者:

<Location /dm>
   ProxyPass http://url.of/dm/
   ProxyPassReverse http://url.of/dm/
   ErrorDocument 502 /error/error.html
</Location>

RewriteEngine on
RewriteCond %{REQUEST_URI} /error/error.html    
RewriteRule ^(.*)$ /error/error.html [R=200,L]

或者有比 Apache HTTPD 更好的替代品吗?如果可能的话,我真的很想使用 Apache,因为它已经安装在系统上。

【问题讨论】:

    标签: apache mod-rewrite mod-proxy


    【解决方案1】:

    我在我们的 Apache 实例上安装了 mod_lua,所以通过在 httpd.conf 中进行设置来解决这个问题:

    <Files *.lua>
        SetHandler lua-script
    </Files>
    
    <Location /dm>
       ProxyPass http://url.of/dm/
       ProxyPassReverse http://url.of/dm/
       ErrorDocument 502 /error/error.lua 
    </Location>
    

    然后创建脚本/error/error.lua:

    function handle(r)
        r.content_type = "text/html" -- set the output to text/html
        r:puts("My custom error payload")
        r.status = 200
        return apache2.OK
    end
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 2015-11-08
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多