Thursday, May 22, 2014

FIX: Django, uwsgi, Apache, ProxyPass, host is localhost and using ProxyPreserveHost

I just spent some time trying to figure out how to set our Django application.

Our setup is:
  Apache (handles HTTPS and client certificates) is front-end proxy to uwsgi
  uwsgi is used to run Django application in the backend.

The problem we faced is that our redirects were broken as links build by django used 'localhost' instead of name of the virtual host. After looking through solutions, and finding some of the issues like:


I looked into django handling of get_host() and tried to understand what's happening.
Long story short, Apache passed HOST header as 'localhost', this led to search of apache documentation, as I was thinking that I reset headers using 
   RequestHeader set X-FORWARDED-PROTOCOL ssl
   RequestHeader set X-FORWARDED-SSL on
   RequestHeader set X-FORWARDED-HOST *hostname*
   RequestHeader set HTTP_HOST *hostname*
   RequestHeader set HOST *hostname*
but apparently, apache does not pass these through the proxy... so... drumroll, saving grace is:
and side note, ProxyPreserveHost needs to be on the VirtualHost level or higher.

I hope this short post will save you some time :)