Redirect tomcat to maintenance page when down

2019-04-07 21:46发布

I'm using tomcat6 with mod_jk setup(both running at port 80) on ubuntu9.10 and 8.10 servers. I deploy war files under /usr/share/tomcat/webapps. During deployment, as I restart the tomcat, I will get the following page when the tomcat application is accessed on the browser:

Service Temporarily Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.2.11 (Ubuntu) mod_jk/1.2.15 Server at 192.168.2.54 Port 80

How could I redirect this page to some other self created maintenance page while the tomcat server is down?.

2条回答
萌系小妹纸
2楼-- · 2019-04-07 22:11

You can setup custom error page in Apache for error code 503.

http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

ErrorDocument 503 /maintance.html
查看更多
你好瞎i
3楼-- · 2019-04-07 22:13

If you're using mod_jk and tomcat connectors this is expected behavior. If you use something like

ErrorDocument 503 "foo"

you will see 'foo' rendered on the page or

ErrorDocument 503 "http://www.somedomain.com"

which will direct you to somedomain.com successfully. But if you use something like

ErrorDocument 503 /maintenance.html

Apache won't be able to find [DocumentRoot]/maintenance.html since it's looking within the context of the tomcat connector. You need to unmount your connector and tell Apache to serve static content from another location.

This is a good guide to get you started with mod_jk. Custom Error Pages with Apache and Tomcat Connectors

edit: Here's the solution I employed to get our custom 503 pages to render properly.

First, all of our custom error pages are prefixed with the error code since it's likely our web app won't contain files with these status codes as the root of the file name. So for using your example, I would have something like the following three files in a directory called 'custom_errors':

/503_maintenance.html
/503_maintenance.css
/503_corp_logo.png

This makes it easy to exclude any files related to custom error pages from the jk mount. In our vhost file, then we set the error document location and alias

#Alias the location of your custom error page files
Alias           /error/ /var/apache2/2.2/htdocs/custom_errors
ErrorDocument   503     /error/503_maintenance.html

#mount the core tomcat application
JkMount     /*      myWorker

#set the 503 code if myWorker is unavailable 
#and exclude the 503 pages from the tomcat/jboss application
JkMount     /*      myWorker;use_server_errors=503
JkUnMount   /503*   myWorker

This basically tells Apache and mod_jk not to mount any files with the 503 prefix under the context of the tomcat connector and will look locally for those files instead. If you don't want to use a location relative to DocumentRoot, you can use and Alias like I did.

查看更多
登录 后发表回答