Search code examples
apachemod-rewritereverse-proxyhttpd.confmod-proxy

How to server static files + proxy context


I am wondering how to configure my httpd server to serves the following pages:

My need is to serve static content located in my /var/www/static when url is /context/static and to proxy the remaining to a tomcat server

In this order:

/context/static/* --> files served by httpd
/context/*        --> resources served by tomcat

I have tried to rewrite /context/static/* to a folder pointing to my /var/www/static and added the ProxyPath directive for the remaining but I can't get it working.

What are the best practices and how to achieve that ?

Thanks in advance


Solution

  • Well, in fact it is quiet easy...

    Having such folders configured:

    /var/www/static/
                   |-  css/*
                   |-  js/*
                    \  medias/*
    

    The following httpd configuration will redirect static/* to the /var/www and the rest will be proxied

    # first rewrite for statics
    RewriteEngine On
    RewriteRule ^/context/static/(.+)$ /static/$1
    
    # then proxy remaining...
    ProxyPass               /context  http://127.0.0.1:8080/context
    ProxyPassReverse        /context  http://127.0.0.1:8080/context