cool tech graphics

Using Apache as a reverse‑proxy

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 ^(.*)$$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/.*)$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.

Date posted: March 4, 2011


The examples you have posted are for mod_rewrite (which enables you to do url rewriting you have above) instead of mod_proxy and enables you to send off a request to some other service (much like using nginx as a proxy to apache, you use apache as a proxy to nginx or something like tomcat/jetty/whatnot). There is a lot more info at and an example at

Look carefully, under the #rewriteflags section, and you'll see that mod_proxy is indeed invoked by the [P] flag. A RewriteRule is often the easiest way to get at this functionality.

Wow, I feel so silly for missing that. You're absolutely right!

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

How can we change that last line so we don't need to hardcode it and make it site-specific in the .htaccess?

Is there a way to pick the domain based on a lookup table based on the original domain or something? That would be epic. Then you it would "just work" for all the sites hosted using a single config + VirtualDocumentRoot.

We use this module to do very similar things. It's used as a files folder router. Can be setup to 302 if the file is missing or download the file locally. It will also forward the request to a different host on a multisite setup. If it doesn't do what you need it to, let me know! I can probably make it happen. We use this on our default site on production and on all of our dev sites.

Great idea, i was tired of replicating entire codebase+files to development server.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Metal Toad is an Advanced AWS Consulting Partner. Learn more about our AWS Managed Services

Have questions?