3

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 e.g. app-debug.war.

Update: I am changing mod_jk workers.properties to forward only /app/* to Tomcat.

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • Please check this link https://stackoverflow.com/questions/13914575/how-to-build-server-level-custom-error-page-in-tomcat and this one https://stackoverflow.com/questions/15987212/custom-error-page-in-tomcat-7-for-error-code-500 Cheers – Devdas Jun 08 '17 at 17:22
  • 1
    @Devdas Thanks for the link. As I mentioned in my post, any wrong URL inside "app" context shows our custom error page. But if context name itself is wrong, say "app-blahblah", then, request will not reach the "app.war", and will be handled by Tomcat - and when Tomcat cannot find any context matching that - it reports 404 page - a standard Tomcat 404 error page. I need to modify this error page to custom page. – Wand Maker Jun 08 '17 at 18:33
  • you can only do that with "mod_proxy", just rid of mod_jk and use mod_proxy with ProxyErrorOverride directive. – Daniel Ferradal Jun 11 '17 at 20:31
  • 1
    can you add a ROOT.war that only shows the error ? – Testo Testini Jun 11 '17 at 23:19

2 Answers2

6

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

Fab
  • 893
  • 1
  • 13
  • 22
  • Thanks for the answer. Our product has custom scripts to start Tomcat, which seems to clean Webapps directory of all directories as part of restart. I found a reference to ErrorReportValve which could have allowed me to hide the server info in Tomcat error page, but the our Tomcat is a version older to the one in which the flag was added to this class. I tried modifying global web.xml, the one which sits in Tomcat's conf folder to add ErrorPage directives - it results in blank content on browser - I was hoping that it can be redirected to httpd's error pages /error/http404.html. – Wand Maker Jun 15 '17 at 19:45
  • What's your Tomcat version ? I just realize that it wasn't mention anywhere / Nobody asks. (Also, Which Apache version ?) – Fab Jun 16 '17 at 07:02
  • 7.0.53 is Tomcat version – Wand Maker Jun 16 '17 at 07:51
  • Using a custom ErrorReportValve to show your error page should work with a Tomcat 7. (See [this link](http://robupcraft.com/tomcat-valves/)). On a side note, when you modify your web.xml of your $CATALIBA_BASE/conf/ folder (as I suggest in my answer), it seeks your custom-error-url.html page in $CATALINA_BASE/webapps/ROOT. So you got a blank page because nothing is found here. If you can't modify your restart script (that wipes out your webapps folder including ROOT), using a custom Valve may be the solution for you indeed. – Fab Jun 16 '17 at 08:47
0

If you have integrated tomcat with apache and using mod_jk for it then use below step for 404 and 401 custom error page.

All steps are mention in below link.

https://devopstech2020.blogspot.com/2021/07/use-custom-error-page-for-tomcats-404.html

Birendra Rawat
  • 1,307
  • 1
  • 8
  • 8
  • Please cite at least the essential part of the solution provided in the link in your answer. Since the linked post uses images you'll have to type a line or two as text (e.g. the `use_server_errors`). – Piotr P. Karwasz Jul 10 '21 at 10:20
  • I have already added this part in link Add use_server_errors=400,500. But thanks I will explore it more. – Birendra Rawat Jul 12 '21 at 04:00