I want to be sure that for some URL of my website, SSL will be use. I saw a lot of answer already on SO.
Force redirect to SSL for all pages apart from one
So I think I will use mod_rewrite
.
My question is more about how to configure the Virtual Host to run my Django Application over HTTP
and over HTTPS
without problems. I am using WSGI
.
Is it a problem to just duplicate the configuration over *:443
and over *:80
?
What should I do to have the best configuration?
Besides using mod_rewrite, you can also use Django to control the SSL redirects.
Here's a modified version of a middleware from the Satchmo Project. I tend to like this method better than mod_rewrite as it's easier to manage.
To use it, pass 'SSL':True into your url conf:
Here's the middleware code:
We used some simple middleware to check urls against a list of base urls that must be in HTTPS mode, all others are forced to HTTP mode. The big caveat here is that any POST data can be lost unless you take extra care (it didn't matter in our case). We were doing this on join pages that required credit card numbers and the like so as soon as they were in that pipeline we forced them into HTTPS.
Here's a view decorator that you can apply to the views that should have HTTPS.
If by WSGI you actually mean Apache/mod_wsgi, then although mounted WSGI applications normally get run in their own sub interpreters, the 80/443 split is a special case and even though in different VirtualHost so long as mount point for WSGIScriptAlias, and the ServerName are the same, they will be merged.
This will happen for daemon mode as well, but with daemon mode you need to define only a single daemon process group in first VirtualHost definition and then just refer to that from both with WSGIProcessGroup.
The WSGIProcessGroup can only reach across like to that VirtualHost for same ServerName.
Django provides a is_secure() method for determining when request came via HTTPS which derives from WSGI variable with request called 'wsgi.url_scheme' which is set by mod_wsgi.
So, you would have one single Django WSGI script file and settings file. You just need to duplicate application mounting as decsribed in Apache/mod_wsgi configuration.