Blog

Using Apache as a reverse‑proxy

Written by Metal Toad Staff | Mar 4, 2011 12:00:00 AM
Filed under:

Apache includes the ability to function as a reverse proxy, which means it can be directed to delegate certain requests to another server. I've found two useful applications for mod_proxy recently: mirroring static files from production, and accessing JSON data.

Static files on a Drupal development server

If your files directory is large, it may be impractical to replicate it on a development server. Instead, you can ask Apache to proxy requests for any missing files to the production server.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /sites/default/files
RewriteRule ^(.*)$ http://www.example.com/$1 [P,L]

The "P" flag after a RewriteRule creates a proxy, and "L" indicates that processing should stop after this rule. Note this will prevent ImageCache from generating new derivatives on the development site.

Connecting browsers to 3rd-party JSON data

One of the great things about JSON encoding is the data can easily be consumed directly in a browser. You don't need glue code in your application, or (horrors) an SDK. You do however need to deal with the browser's same-origin policy.

On the FontFuse project, we solved this by creating a simple proxy:

RewriteRule (.*serviceapi/webink/.*) http://www.extensis.com/$1 [P,L]

In production, we're using Varnish to allow caching of common requests, but this simple proxy enables development copies of the site to run without additional services.