Take the 2-minute tour ×

I have a Django website where some of the URLs need to be served over HTTPS and some over a normal connection.

It's running on Apache and using WSGI. Here's the config:

<VirtualHost example.org:80>
    ServerName example.org
    DocumentRoot /var/www/html/mysite

    WSGIDaemonProcess mysite
    WSGIProcessGroup mysite

    WSGIScriptAlias / /path/to/mysite/conferencemanager.wsgi    

<VirtualHost *:443>
    ServerName example.org
    DocumentRoot /var/www/html/mysite

    WSGIProcessGroup mysite

    SSLEngine on
    SSLCertificateFile /etc/httpd/certs/aace.org.crt
    SSLCertificateKeyFile /etc/httpd/certs/aace.org.key
    SSLCertificateChainFile /etc/httpd/certs/gd_bundle.crt

    WSGIScriptAlias / /path/to/mysite/conferencemanager_secure.wsgi     

When I restart the server, the first site that gets called -- https or http -- appears to select which WSGI script alias gets used.

I just need a few settings to be different for the secure server, which is why I'm using a different WSGI script. Alternatively, it there's a way to change settings in the settings.py file based on whether the connection is secure or not, that would also work.



The reason I want different settings is because I'm using a media server on the non-secure site:

MEDIA_URL = 'http://xmpl.org/media/'
STATIC_URL = 'http://xmpl.org/static/'

However, for the secure version rather than setting up another SSL certificate just for my media, I'm using the same server:

MEDIA_URL = '/media/'
STATIC_URL = '/static/'

So I need the values to change at the settings level. I suppose if necessary I can write a context processor that overrides the values for STATIC_URL and MEDIA_URL if the server is secure.

share|improve this question
What settings do you need to be different? As @hwjp points out, you should be doing checks within your view handler if behaviour needs to be different. There is a way of forcing the separation you want, but want to see if why you even think it is valid in the first place is correct before answering. –  Graham Dumpleton Sep 16 '12 at 5:18
See my updated question. Also, I'd like flexibility in the future to add additional settings I might need. For example, using a separate error handler. –  Jordan Reiter Sep 18 '12 at 15:50

1 Answer 1

Could you use a different approach? Instead of using two different settings files, you could check each request to see whether it was made with https or not...

def view(request, param)
    if request.is_secure():


if re-writing all your views is too much hassle, you could probably create a middleware class that would apply some kind of state to all your views - something like:

from django.conf import settings
class HttpsDetectingMiddleware(object):
    def process_request(self, request):
       if request.is_secure():
           settings.MY_CONFIG = 'secure'
            settings.MY_CONFIG = 'insecure'
        return request

and then add your middleware in settings.py...


share|improve this answer
Can you even change the values in the settings variable? Seems like it would be read only. –  Jordan Reiter Oct 9 '12 at 20:47
yep, it's no problem, yay Python. Efects may be a little unpredictable, since you're changing a global variable at run-time... So, for example, middleware that's run before your context processor might not pick up on the change, but since (I imagine) the things you want to affect are downstream (ie the template renderer, to show different urls for static files), it should work fine. –  hwjp Oct 10 '12 at 13:31

Your Answer


