Thursday, July 26, 2012

Virtual hosting on Grok (or Bluebream) - without a reverse proxy

When I was poking at creating the Grok template for OpenShift, I needed to add virtual hosting for the test application.

However, OpenShift's DIY cartridge does not allow me to modify the reverse proxy settings as they are automatically generated by the cartridge. Naturally, I had to find another way.

Grok virtual hosting with WSGIRewrite

WSGIRewrite is a WSGI middleware which provide a facility for mod_rewrite compatible rewrite rules for WSGI applications. Using this middleware, we can rewrite the URL to force virtual hosting on the application server.

Lets go straight to the meat, here is the howto.

Howto

The first step is of course, to add WSGIRewrite into your buildout. Edit setup.py and add 'WSGIRewrite' into the install_requires list, and re-run buildout.

Afterward, edit your WSGI INI file, (eg: etc/deploy.ini.in), and find a section called pipeline:main, and add rewrite as the first item in the pipeline. The section should appear like this now:

[pipeline:main]
pipeline = rewrite accesslogging evalexception fanstatic grok

At the bottom of the file, add these lines. Replace examplesite with an identifier for the entry, replace app with your application ID, and replace www.example.com with the virtual hosting domain:

[filter:rewrite]
use = egg:WSGIRewrite
rulesets = examplesite-http examplesite-https

[wsgirewrite:examplesite-http]
cond1 = %{HTTP_HOST} ^www.example.com$
cond2 = %{HTTPS} off
rule1 = ^/(.*) app/++vh++http:www.example.com/++/$1

[wsgirewrite:examplesite-https]
cond1 = %{HTTP_HOST} ^www.example.com$
cond2 = %{HTTPS} on
rule1 = ^/(.*) app/++vh++https:www.example.com/++/$1
 
Rerun buildout to regenerate your INI files, and start your paster/wsgi server. Access the server using the configured domain, and it should work.
Post a Comment
Locations of visitors to this page