Using Apache as a reverse‑proxy
by Dylan Tack, Lead Development Architect
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.
Comments
Proxy module
Posted by mikeytown2 on . [Reply]
I've been using this on our dev environment
http://drupal.org/project/stage_file_proxy
Nice trick with apache though :)
The examples you have posted
Posted by Ashok Modi on . [Reply]
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 http://httpd.apache.org/docs/2.2/mod/mod_proxy.html and an example at http://confluence.atlassian.com/display/DOC/Using+Apache+with+mod_proxy
A bit of both
Posted by Dylan Tack on . [Reply]
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.
My mistake
Posted by Ashok Modi on . [Reply]
Wow, I feel so silly for missing that. You're absolutely right!
awesome, now the next step...
Posted by Chris Wells on . [Reply]
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} /sites/default/files RewriteRule ^(.*)$ <a href="http://www.example.com/">http://www.example.com/</a>$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.
New module
Posted by mikeytown2 on . [Reply]
We use this module to do very similar things. http://drupal.org/project/files_proxy. 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.
Add new comment