Tuesday, January 15, 2013

Serving static content from Apache instead of tomcat with just modjk

Is apache faster than tomcat serving static content? It was true some years ago, I am not that sure the statement is true nowadays.

However if you use apache as load balancer with modjk there will be overhead sending all those packets to each tomcat server and getting the response back. So actually with such environment static content should be served by Apache unless of course you have security constraints that are already implemented in tomcat which affect that static content.

On the other hand what happens when you go on maintenance mode for let us say a deployment that is taking beyond a couple of seconds (very common)? Would you leave the default HTTP 503 error to be sent back to the user? Bad User eXperience I would say.

All you need to do is play with two directives in your virtual host to make sure certain requests do not go to tomcat but stay in Apache. Here are the two statements which make sure a directory called html containing (you guessed it) static markup is rendered from Apache. We use it to make sure our customized HTTP 503 error page is correctly rendered.

<VirtualHost  sample.com:443>
...
 ErrorDocument 503 /html/error/503.html
 SetEnvIf Request_URI "/html/*" no-jk
 Alias /html /var/myapp/html
...
You might want a more complicated page respecting the look and feel of your website. You already have images and CSS and you of course will not repeat yourself so you won't copy and paste. You can share your current images and CSS adding more "SetEnvIf Request_URI" and "Alias" directives like the above:

<VirtualHost  sample.com:443>
...
 ErrorDocument 503 /html/error/503.html
 SetEnvIf Request_URI "/html/*" no-jk
 Alias /html /var/myapp/html
 SetEnvIf Request_URI "/css/*" no-jk
 Alias /css /var/myapp/css
 SetEnvIf Request_URI "/images/*" no-jk
 Alias /images /var/myapp/images
...
Your deployment script should of course account for deploying what is necessary to apache. Your resources stay in your project like usual and of course they can be used from plain tomcat in case you (for example) develop in a machine without Apache (which I do not recommend, did I say I believe the development environment should be as close as possible to production environment?) or from Apache as explained.

There are other important tasks the sysadmin will need to perform (or you still call it the webmaster? :) like for example communicating users about future hours of downterm for maintenance purpose. I can't picture someone doing all this manually really. At a minimum I would expect POB recipes ready to be run in remote apache servers to ensure configuration files and resources are properly changed and reloaded as needed. Remoto-IT can help with that.

No comments:

Followers