![在 Apache 中的不同 VirtualHosts 上显示自定义错误页面](https://linux22.com/image/578104/%E5%9C%A8%20Apache%20%E4%B8%AD%E7%9A%84%E4%B8%8D%E5%90%8C%20VirtualHosts%20%E4%B8%8A%E6%98%BE%E7%A4%BA%E8%87%AA%E5%AE%9A%E4%B9%89%E9%94%99%E8%AF%AF%E9%A1%B5%E9%9D%A2.png)
我有一个反向代理 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>
你还需要确保你有合适的名称虚拟主机定义。