Nginx reverse proxy , Apache Drupal and Boost

jayatdrupal's picture

A nice way to add performance to your drupal site is by adding Nginx in front of your web server to act as a reverse proxy. I have summarized my configuration which puts Nginx in front of an existing Apache / drupal site. Not only can it serve images and other static files, but I have made the tweaks to the configuration file to serve up the boost cached files directly from the Nginx front end.

My testing shows it to be very, very quick. The best thing, is that it can be tested on a production server with no impact.

Nginx Configuration piece is below. For full details, see the write up here:

location / {
index index.html index.htm;

root /var/www/yourdocroot;

adding boost

set $boost "";
set $boost_query "_";

if ( $request_method = GET ) {
set $boost G;

if ($http_cookie !~ "DRUPAL_UID") {
set $boost "${boost}D";

if ($query_string = "") {
set $boost "${boost}Q";

if ( -f /var/www/yourdocroot/cache/normal/$host$request_uri$boost_query.html ) {
set $boost "${boost}F";

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if ($boost = GDQF){
rewrite ^.*$ /cache/normal/$host/$request_uri$boost_query.html break;
if ($boost !~ GDQF) {


Thanks for sharing

perusio's picture

It seems that some reverse thinking of the variety that apache's mod_rewrite promotes is slipping through and it shows in your config. Why not use try_files and avoid all that "procedural" stuff? AFAIK all of the configs indicated in the group use try_files and are tuned to the way nginx does things instead of retrofitting a mod_rewrite Yoda style config :)

Nginx config for Drupal

halcyonCorsair's picture

See here:

For a more nginx way of doing things with try_files, etc.

Also see:

That config

perusio's picture

is a bit outdated: for instance Boost can have query args. Also there's no constraining of PHP files and there unecessary regex locations that can be better served with exact locations. Just for the record exact locations are stored in a hash table and regexes are processed sequentially. Whichever matches first is the one that gets served. Hence for each request nginx must parse the config (in memory) and find the corresponding regex.

In the nginx group there are are other configs referenced at the top of the group page that avoid altogether the usage of regexes for matching drupal specific locations.

thanks for the advice

jayatdrupal's picture

The link was a great example (and clean for maintenance). I have modified it to perform reverse proxying back to apache instead of using fast-cgi.