Forum Moderators: phranque
I'm using Apache 2.0.53 and Tomcat 5.5.7. The current setup is that Apache is serving static content and dynamic content requests are sent to Tomcat using mod_jk. There are two links (signup and login) on the static pages, and these links get processed by Tomcat.
Since Tomcat needs to go down for maintenance now and then, I'd like a way to tell Apache that during that period, show an 'undergoing maintenance' page for any requests for either the login or signup pages. I'm not sure how to set that up so that it's configurable by setting a param or something instead of Apache redirecting only if it does x number of retries to reach a Tomcat instance (causes a performance issue..)
Any pointers are very much appreciated!
Thanks!
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(signup¦login)$
RewriteCond /var/www/conf/MAINTENANCE -f
RewriteRule ^/(.*) /path/to/maintenancepage.html [L]
This checks to make sure the request is for the signup or login pages (adjust the regex as needed); if the request *is* for one of those pages *and* a file called /var/www/conf/MAINTENANCE exists (adjust path accordingly; it doesn't need to be in the apache document root), then the request will be transparently rewritten to a maintenance page. If you have PHP installed (or want to implement a CGI script), you could tweak the RewriteRule to be something like:
RewriteRule ^/(.*) /path/to/maintenancepage.php?src=$1 [L]
Finally, you could tweak the rewriterule so that it returns a redirect to the maintenance page instead doing it all behind the scenes:
RewriteRule ^/(.*) /path/to/maintenancepage.html [L,R]
Does this help?
I guess the real key is #1: being able to tell Apache to do the rewriting without having to recycle Apache (since that would kill existing user sessions).
Thanks for your time/input.
An offshoot of your idea would be using a cgi to set some kind of memory-resident value which the rewrite code would check instead of a filesystem check. That would be much better performance wise. Do you know of any type of in-memory variables/flags/etc that Apache uses and that would be accessible by your rewrite code?
Thanks for your time/input!
Now that I think about it, this recipe would be a bit better:
RewriteEngine on
RewriteCond /var/www/conf/MAINTENANCE -f
RewriteRule ^/(signup¦login)$ /path/to/maintenancepage.html [L]
You *could* do this with a CGI, however the performance penalty for running such a script would likely far outstrip the penalty of the the single stat() call that mod_rewrite would make to check for the existence of the MAINTENANCE file; if your CGI didn't make use of an embedded interpreter of some kind, I can pretty much guarantee you that penalty incurred by a single stat() of a local file (and *do* be sure that the file is on the local filesystem) will be nothing compared to the fork()/exec()/compile-script-to-bytecode penalty you'd get by making use of an external interpreter.
To test this, I ran a perl script to make a stat call, timed the stat call in the script and the full execution time from outside the script. The script run time was .023 seconds. The time to execute the stat() called was .014 *milliseconds*. That answers that. =)
Now that I think about it, all inbound traffic will go through a firewall - maybe I should explore configuring it as a type of layer 7 switch..? Maybe iptables can do that, though it's probably a bad idea to mix security with request rerouting..
Thanks again.
Regarding setting a var/flag - I was thinking of setting it in Apache's mem, not in an HTTP session. That would make more sense because Apache should not check a session for the flag, it should check it's own internal mem space for the flag.
*nod* There /are/ webservers out there that allow one to do this sort of thing, but Apache (vanilla Apache, anyway) isn't set up for this sort of thing. One could implement such a feature as a module (or in perl, if one's using mod_perl), but the rewrite solution is going to be less overhead than either of those, by a long shot. =)
Implementing something like this *could* be done in IPtables (I think; my iptables-fu is only medium-strength); the fact that you only want the maintenance page to appear for requests for two URLs means that IPTables (or firewall) will need to check the contents of incoming HTTP packets; this may or may not be a performance improvement over the mod_rewrite solution. In my mind, this is a job for the application, since we're dealing with things in layer-7, but I'm sure there are networking devices that could do this.
One final note; if the signup/login pages are HTTPS, the network-based solution probably won't work at all, since the firewall/network layer would have no way to decrypt the incoming HTTPS traffic. There *are* exceptions to this rule; some network devices terminate the HTTPS connection (the idea being they can open an HTTP connection to the back-end realserver and send the data in the clear, effectively offloading the expensive SSL operations from the webserver), but in order to do this, the device needs the SSL certificate in use by your site. You may or may not be set up for this sort of thing.