Web-server – Serving static content with Apache web server and Tomcat

apache-2.2tomcatweb-server

I've configured Apache web server and Tomcat like this:

I created a new file in apache2/sites-available, named it "myDomain" with this content:

<VirtualHost *:80>

ServerAdmin admin@myDomain.com
ServerName myDomain.com
ServerAlias www.myDomain.com

ProxyPass / ajp://localhost:8009

<Proxy *>
AllowOverride AuthConfig
Order allow,deny
Allow from all
Options -Indexes
</Proxy>

</VirtualHost>

Enabled mod_proxy and myDomain

a2enmod proxy_ajp
a2ensite myDomain

Edited Tomcat's server.xml (inside the Engine tag)

<Host name="myDomain.com" appBase="webapps/myApp">
<Context path="" docBase="."/>
</Host>
<Host name="www.myDomain.com" appBase="webapps/myApp">
<Context path="" docBase="."/>
</Host>

This works great. But I don't like to put static files (html, images, videos etc.) into {tomcat home}/webapps/myApp's subfolders instead I'd like to put them the apache webserver's root WWW directory's subdirectories. And I'd like Apache web server to serve these files alone.

How could I do this? So all incoming request will be forwarded to Tomcat except those that ask for a static file.

Best Answer

You probably want to use ProxyPassMatch. You can use it like:

ProxyPassMatch ^(/.*\.cgi)$ ajp://localhost:8009$1

to proxy all requests ending in .CGI. If you wish to proxy multiple extensions just add more lines or edit the regex. Then you just have to make sure your VirtualHost is setup to serve the remaining image types.