Forum OpenACS Q&A: HOWTO: serve pre-compressed /resource/ files directly from your apache proxy

I thought others might be interested in this. This howto assumes you are already using Apache as your frontend proxy. No special Apache modules are necessary.

Firstly assume your apache config looks something like:

<VirtualHost 192.168.70.222>
    ServerName mydomain.com
    RewriteEngine on
    RewriteRule ^/(.*)$ http://127.0.0.1:8000/$1 [P,L]
    ProxyPassReverse  / http://127.0.0.1:8000/
</VirtualHost>

First lets bypass AOLServer for our /resources/ urls. No point invoking two servers for non-permissioned static files:

<VirtualHost 192.168.70.222>
    ServerName mydomain.com
    RewriteEngine on
    RewriteRule ^/resources/([^/]+)/(.*) /your_openacs_dir/packages/$1/www/resources/$2 [L]
    RewriteRule ^/(.*)$ http://127.0.0.1:8000/$1 [P,L]
    ProxyPassReverse  / http://127.0.0.1:8000/
</VirtualHost>

Ok, that works - now lets prepare our css and js files by pre-compressing them:

for file in `find openacs-4 -name '*.css' -or -name '*.js'`; do cat $file | gzip > ${file}gz; done

Now lets serve those files if the browser can handle it:

<VirtualHost 192.168.70.222>
    ServerName mydomain.com
    RewriteEngine on
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule (.*)\.css$ $1\.cssgz

    AddType "text/css;charset=UTF-8" .cssgz
    AddEncoding gzip .cssgz

    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule (.*)\.js$ $1\.jsgz

    AddType "text/javascript;charset=UTF-8" .jsgz
    AddEncoding gzip .jsgz

    RewriteRule ^/resources/([^/]+)/(.*) /your_openacs_dir/packages/$1/www/resources/$2 [L]
    RewriteRule ^/(.*)$ http://127.0.0.1:8000/$1 [P,L]
    ProxyPassReverse  / http://127.0.0.1:8000/
</VirtualHost>

Note that this will break in a number of cases eg. if you have .vuh files that create css or js files for urls ending in .js or .css. If you have such a setup I'm sure you can figure it out ;)