简体   繁体   中英

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 is configured with custom error pages for HTTP errors 404, 500 etc.

If user enters URL, http://hostname/non-existing-page - then httpd's custom 404 error page is displayed.

If user enters URL, http://hostname/app-blabblah - then, Tomcat's 404 error page is displayed. The application hosted at /app can deal with 404 errors if it is something like /app/non-existentpage. But context /app-blablah does not resolve to any war file on Tomcat, and hence results in Tomcat's 404 error page.

We are using stripped down version of Tomcat, its webapps folder has only 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.

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.
  2. Or configure httpd such that if it sees that Tomcat is returning 404 error, it renders its own custom 404 error page.

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.

Update: I am changing mod_jk workers.properties to forward only /app/* to 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.

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.

The simplest solution is to use a ROOT webapp folder. 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 .

With this configuration, you just have to add in your $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).


The result is :

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

  • The httpd error custom page will be displayed

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

  • The error custom page of your application app.war will be displayed. ($CATALINA_BASE/conf/web.xml and then $CATALINA_BASE/webapps/app/WEB-INF/web.xml are used)

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

  • The Tomcat error custom page of your ROOT folder will be displayed. (Only $CATALINA_BASE/conf/web.xml is used).

In you edit, you deleted your constraint and are now able to :

chang(e) mod_jk workers.properties to forward only /app/* to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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