简体   繁体   English

使用mod_jk和Apache httpd时,将自定义错误页面用于Tomcat的404页面

[英]Use custom error page for Tomcat's 404 Page when using mod_jk and Apache httpd

We have setup like this: 我们有这样的设置:

httpd <-> mod_jk (Tomcat)

httpd is configured to forward all request to URL /app* to mod_jk. httpd配置为将所有请求转发到URL / app *到mod_jk。 httpd is configured with custom error pages for HTTP errors 404, 500 etc. httpd配置了针对HTTP错误404、500等的自定义错误页面。

If user enters URL, http://hostname/non-existing-page - then httpd's custom 404 error page is displayed. 如果用户输入URL,则输入http://hostname/non-existing-page page-然后显示httpd的自定义404错误页面。

If user enters URL, http://hostname/app-blabblah - then, Tomcat's 404 error page is displayed. 如果用户输入URL http://hostname/app-blabblah然后,将显示Tomcat的404错误页面。 The application hosted at /app can deal with 404 errors if it is something like /app/non-existentpage. 如果/ app / non-existentpage之类的内容托管在/ app中,则该应用程序可以处理404错误。 But context /app-blablah does not resolve to any war file on Tomcat, and hence results in Tomcat's 404 error page. 但是上下文/ app-blablah不能解析到Tomcat上的任何war文件,因此会导致Tomcat的404错误页面。

We are using stripped down version of Tomcat, its webapps folder has only app.war . 我们正在使用精简版的Tomcat,它的webapps文件夹只有app.war

I have been asked to not display Tomcat 404 error page as it reveals the fact that app is hosted on Tomcat and also displays Tomcat's version. 我被要求不要显示Tomcat 404错误页面,因为它显示了应用程序托管在Tomcat上并显示Tomcat版本的事实。

If anyone can tell me how to: 如果有人可以告诉我如何:

  1. Add custom 404 error page for Tomcat when it has to deal with URLs that represents non-existing context. 当Tomcat必须处理代表不存在的上下文的URL时,为Tomcat添加自定义404错误页面。
  2. Or configure httpd such that if it sees that Tomcat is returning 404 error, it renders its own custom 404 error page. 或配置httpd,以便如果看到Tomcat返回404错误,它将呈现自己的自定义404错误页面。

PS: I am not at liberty to point only /app/* to mod_jk, as we sometimes deploy other wars that start with app for debugging purposes - for eg app-debug.war. PS:我不能随意将/ app / *指向mod_jk,因为我们有时会部署其他以app开头的战争来进行调试-例如app-debug.war。

Update: I am changing mod_jk workers.properties to forward only /app/* to Tomcat. 更新:我将mod_jk worker.properties更改为仅将/ app / *转发到Tomcat。

To answer your question : 要回答您的问题:

  1. Add custom 404 error page for Tomcat when it has to deal with URLs that represents non-existing context. 当Tomcat必须处理代表不存在的上下文的URL时,为Tomcat添加自定义404错误页面。

And respecting your initial constraint : 并尊重您的初始约束:

I am not at liberty to point only /app/* to mod_jk, as we sometimes deploy other wars that start with app for debugging purposes - for eg app-debug.war. 我不能随意将/ app / *指向mod_jk,因为我们有时会部署其他以app开头的战争来进行调试-例如app-debug.war。

The simplest solution is to use a ROOT webapp folder. 最简单的解决方案是使用ROOT webapp文件夹。 However, and to respect your other condition to keep your webapps folder minimalist, you don't have to keep anything in your ROOT webapp folder but your custom error page . 但是,为了尊重您保持webapps文件夹最小化的其他条件, 您无需在ROOT webapp文件夹中保留任何内容,只需自定义错误页面即可

With this configuration, you just have to add in your $CATALINA_BASE/conf/web.xml 使用此配置,您只需添加$ CATALINA_BASE / conf / web.xml

<error-page>
  <error-code>404</error-code>
  <location>/general-error.html</location>
</error-page>

Or anything that fits best what you want. 或任何最适合您想要的东西。 (Here, you can use the same error custom page than your app.war and end users will not see the difference). (在这里,您可以使用与app相同的错误自定义页面。war和最终用户将看不到区别)。


The result is : 结果是:

Situation 1 : http://hostname/non-existing-page 情况1: http://hostname/non-existing-page

  • The httpd error custom page will be displayed 将显示httpd错误自定义页面

Situation 2 : http://hostname/app/non-existing-page 情况2: http://hostname/app/non-existing-page

  • The error custom page of your application app.war will be displayed. 将显示应用程序app.war的错误自定义页面。 ($CATALINA_BASE/conf/web.xml and then $CATALINA_BASE/webapps/app/WEB-INF/web.xml are used) (使用$ CATALINA_BASE / conf / web.xml,然后使用$ CATALINA_BASE / webapps / app / WEB-INF / web.xml)

Situation 3 : http://hostname/app-dummy-url 情况3: http://hostname/app-dummy-url

  • The Tomcat error custom page of your ROOT folder will be displayed. 将显示您的ROOT文件夹的Tomcat错误自定义页面。 (Only $CATALINA_BASE/conf/web.xml is used). (仅使用$ CATALINA_BASE / conf / web.xml)。

In you edit, you deleted your constraint and are now able to : 在编辑中,您删除了约束,现在可以:

chang(e) mod_jk workers.properties to forward only /app/* to Tomcat. chang(e)mod_jk worker.properties仅将/ app / *转发到Tomcat。

Well in this configuration, you will have the same result as the alternative you wanted : 在此配置中,您将得到与所需替代品相同的结果:

  1. Or configure httpd such that if it sees that Tomcat is returning 404 error, it renders its own custom 404 error page. 或配置httpd,以便如果看到Tomcat返回404错误,它将呈现自己的自定义404错误页面。

But if you still want to use debug war file such as you mentioned in your question (app-debug.war), you will need to modify your httpd configuration (worker.properties and probably your VirtualHosts as well) everytime and/or let debug configuration in those files. 但是,如果您仍然想使用问题中提到的调试战争文件(app-debug.war),则每次都需要修改httpd配置(worker.properties可能还包括VirtualHosts)和/或进行调试这些文件中的配置。 As it doesn't seems appropriate for a production environment, using a folder ROOT in Tomcat containing only custom pages seems the best solution here IMO. 由于它似乎不适用于生产环境,因此在IMO中使用Tomcat中仅包含自定义页面的文件夹ROOT似乎是最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM