Running Drupal Secure Pages behind a proxy
by Dylan Tack, Lead Development Architect
If you plan to use the securepages module behind a proxy that terminates SSL, there are some additional server configuration steps you need to take.
In order to detect what the protocol is in use, securepages tests the value of $_SERVER['HTTPS']. Out of the box, this merely reflects the immediate connection to your proxy. If this protocol differs from that used by the original client, then securepages can't work (the most likely outcome is a redirect loop).
To resolve this, you'll need to ask your proxy to send the X-Forwarded-Proto header. While you're free to use any header label you choose, X-Forwarded-Proto seems to have become the de facto standard.
Configure your web server
There are several possible approaches here; which one you choose is largely a matter of taste.
Apache SetEnvIf
I personally like this approach specifically because it doesn't require any changes to the application code, and makes the SSL proxy appear completely transparent to Drupal. The SetEnvIf directive can be placed in httpd.conf, a vhost configuration file, or in your .htaccess file.
SetEnvIf X_FORWARDED_PROTO https HTTPS=on
Modify $_SERVER directly
This approach involves directly modifying $_SERVER['HTTPS'] in settings.php. Since this file is loaded very early in the request (during DRUPAL_BOOTSTRAP_CONFIGURATION), any changes here will kick in before securepages initializes.
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $_SERVER['HTTPS'] = 'on'; }
Configure your proxy
What happens next depends entirely on what proxy software you have. Here are some some hints for several popular proxies (we use f5 load balancers at Metal Toad; the others have been collected from around the web but not tested):
f5
Inside an iRule:
when HTTP_REQUEST {
if {([TCP::local_port] == 80) and !( [HTTP::header "X-Forwarded-Proto"] eq "http") }{
HTTP::header insert X-Forwarded-Proto "http"
} elseif {([TCP::local_port] == 443) and !( [HTTP::header "X-Forwarded-Proto"] eq "https") } {
HTTP::header insert X-Forwarded-Proto "https"
}
}Pound
Inside a ListenHTTP / ListenHTTPS directive:
ListenHttp HeadRemove "X-Forwarded-Proto" AddHeader "X-Forwarded-Proto: http" ... ListenHttps HeadRemove "X-Forwarded-Proto" AddHeader "X-Forwarded-Proto: https" ...
Apache
Inside a virtual host config:
<VirtualHost default:80> RequestHeader set X-Forwarded-Proto "http" ... </VirtualHost> <VirtualHost default:443> RequestHeader set X-Forwarded-Proto "https" ... </VirtualHost>
nginx
Inside a location directive:
proxy_set_header X-Forwarded-Proto $scheme;
A note about forged headers
When using a proxy it's generally good advice to keep your web servers firewalled from the greater internet, and make sure headers such as this can't be forged. I've attempted to give examples that always override any value set by a mischief-making client.
That said, in this particular case I don't think a forged X-Forwarded-Proto creates any new vulnerabilities. While it might at first seem to make SSL stripping easier, the unfortunate reality is that SSL stripping will succeed either way.
Comments
Thanks!
Posted by Caleb on . [Reply]
We're on a tight deadline to get our new configuration online - and this tip on how to configure an F5 to work with securepages was a blessing. Even the guys at the datacenter were surprised it worked. :) Thank you, thank you!
Follow up
Posted by Caleb on . [Reply]
A follow up after a week with our F5:
After running it all through my head a couple times I realized that since we terminate SSL on the F5, we didn't even need mod_ssl or securepages for our front servers anymore. The F5 handles everything - the SSL termination (thus replacing mod_ssl), and our host was willing to write some http classes on the F5 itself to redirect certain URI's to https for us so we don't need securepages anymore either.
This was very helpful, but
Posted by Will on . [Reply]
This was very helpful, but for our server we had to use a slightly different directive for Apache:
Note the dashes as opposed to underscores.
Thank you!
Posted by Sergei on . [Reply]
Thank you!
Thanks Dylan and Will!
Posted by Ken on . [Reply]
I followed this advice (with Will's change) using Lighttpd 1.4.28 as a proxy for Apache 2.2.20. Lighttpd didn't require any configuration to set X-Forwarded-Proto - it seems to do it out of the box - which is nice as I'm using Lighttpd to serve static content.
Dylan,
Posted by TimDaMan on . [Reply]
Dylan,
thanks for the info! I spent a few weeks trying to get everything working right behind our load-balance until I can across your directions. I simplified a bit though since the LB is run by a separate group. I simply put
$_SERVER['HTTPS'] = 'on';
into my settings.php which took care of what ailed me (users seeing http URLs in response to https requests).
--Tim
SetEnvIf X-Forwarded-Proto https HTTPS=on
Posted by Crouse on . [Reply]
Wills comments were a lifesaver !!
SetEnvIf X-Forwarded-Proto https HTTPS=on
Worked like a charm :) Thanks Will and Dylan!.
Can't Drupal live in http if its proxy is negotiating ssl for it
Posted by Bronius Motekaitis on . [Reply]
Hi - Why can't Drupal live in http (non-secure) behind the firewall if its load balancer proxy is negotiating SSL for the site?
yes
Posted by Dylan Tack on . [Reply]
Yes it can – that's the subject of this post.
So why does Drupal (or any app) need to know HTTPS vs HTTP?
Posted by Bronius Motekaitis on . [Reply]
Thanks for your reply, Dylan. I guess what has always eluded me all these years, then, is why is it that the application behind the reverse proxy (Drupal in this case) ever needs to know how the request came in and that it is operating in anything other than http?
Variable output
Posted by Dylan Tack on . [Reply]
In the configuration described above, the application (Drupal) is making the decisions about when to redirect visitors to HTTP or HTTPS. The responses might also vary in other ways depending on the protocol (for example on this web site the CDN URL changes on HTTPS). Implementing this logic in the application is often advantageous because the app has a lot more knowledge about the the request than the proxy. Keeping all the logic contained in the app also makes it more portable.
Oh yes of course!
Posted by Bronius Motekaitis on . [Reply]
Ok super -- that makes sense. The application (Drupal) can receive and respond in http for all requests, but it is telling the proxy "by the way make this next one https for the client.. You and I are cool, though." The proxy would otherwise make all requests http (or https).
Thanks!
Add new comment