在 Apache 中的不同 VirtualHosts 上显示自定义错误页面

在 Apache 中的不同 VirtualHosts 上显示自定义错误页面

我有一个反向代理 Apache,它将请求移至 Tomcat servlet。Apache 中的虚拟主机上的配置如下:

<VirtualHost 10.10.10.10:80>
ProxyPass /Site1/ServLet1 http://1.1.1.1/Site1/ServLet1
ProxyPassReverse /Site1/ServLet1 http://1.1.1.1//Site1/ServLet1

ProxyPass /Site2/ServLet2 http://2.2.2.2/Site2/ServLet2
ProxyPassReverse /Site2/ServLet2 http://2.2.2.2/Site1/ServLet1
</VirtualHost>

本质上,如果涉及10.10.10.10和请求/Site1/ServLet1,则将其路由到/Site1/ServLet1

如果我添加

<VirtualHost 10.10.10.10:80>
ProxyPass /Site1/ServLet1 http://1.1.1.1/Site1/ServLet1
ProxyPassReverse /Site1/ServLet1 http://1.1.1.1//Site1/ServLet1
ErrorDocument 404 /customerrors/site1/404.html

ProxyPass /Site2/ServLet2 http://2.2.2.2/Site2/ServLet2
ProxyPassReverse /Site2/ServLet2 http://2.2.2.2/Site1/ServLet1
</VirtualHost>

所以它将显示一个自定义错误site1(我设置了ErrorDocument),它将被提供给两者。

我怎样才能让每个站点都有不同的 404 错误页面并保持这种配置?

感谢

编辑:

如果我根据以下评论修改配置,例如:

<Location /Site1/ServLet1/>
ProxyPass http://1.1.1.1/Site1/ServLet1
ProxyPassReverse http://1.1.1.1/Site1/ServLet1
ErrorDocument 404 /customerrors/site1/404.html
</Location>

然后我仍然可以进入,http://1.1.1.1/Site1/ServLet1但不会显示任何错误页面

答案1

我不确定您所说的“不同的 VirtualHosts”是什么意思,因为它们在同一个 VirtualHosts 中......但我认为您会想要做这样的事情(如果可以的话,考虑将ProxyPass语句也移到块中):<Location>

<VirtualHost 10.10.10.10:80>
    ProxyPass /Site1/ServLet1 http://1.1.1.1/Site1/ServLet1
    ProxyPassReverse /Site1/ServLet1 http://1.1.1.1/Site1/ServLet1

    <Location /Site1>
        ErrorDocument 404 /customerrors/site1/404.html
    </Location>

    ProxyPass /Site2/ServLet2 http://2.2.2.2/Site2/ServLet2
    ProxyPassReverse /Site2/ServLet2 http://2.2.2.2/Site2/ServLet2

    <Location /Site2>
        ErrorDocument 404 /customerrors/site2/404.html
    </Location>
</VirtualHost>

编辑:

要使Proxy语句驻留在位置块中:

<VirtualHost 10.10.10.10:80>
    <Location /Site1>
        ErrorDocument 404 /customerrors/site1/404.html
    </Location>
    <Location /Site1/ServLet1>
        ProxyPass http://1.1.1.1/Site1/ServLet1
        ProxyPassReverse http://1.1.1.1/Site1/ServLet1
    </Location>

    <Location /Site2>
        ErrorDocument 404 /customerrors/site2/404.html
    </Location>    
    <Location /Site2/ServLet2>
        ProxyPass http://2.2.2.2/Site2/ServLet2
        ProxyPassReverse http://2.2.2.2/Site2/ServLet2
    </Location>
</VirtualHost>

答案2

使每个 vhost 都有自己的 VirtualHost 定义。

<VirtualHost 10.10.10.10:80>
    Servername site1.tld
    ProxyPass /Site1/ServLet1 http://1.1.1.1/Site1/ServLet1
    ProxyPassReverse /Site1/ServLet1 http://1.1.1.1//Site1/ServLet1
    ErrorDocument 404 /customerrors/site2/404.html
</VirtualHost>

<VirtualHost 10.10.10.10:80>
    ServerName site2.tld
    ProxyPass /Site2/ServLet2 http://2.2.2.2/Site2/ServLet2
    ProxyPassReverse /Site2/ServLet2 http://2.2.2.2/Site1/ServLet1
    ErrorDocument 404 /customerrors/site2/404.html
</VirtualHost>

你还需要确保你有合适的名称虚拟主机定义。

相关内容