Force HTTPS in Nginx for Django -
i have tried this:
- create file named middleware.py (chmod 775, chown www-data) in same directory settings.py
- paste in middleware.py: http://djangosnippets.org/snippets/880/
paste in settings.py:
middleware_classes = ( #... 'djo.middleware.sslredirect', ) ssl_enabled = true ssl_urls = ( r'/admin/', )
i get: internal server error
no errors in development server shown.
suggestions?
found problem. had infinite loop happening , followed site fix it:
basically, add nginx server conf setting under proxy location part of file:
secure_proxy_ssl_header = (‘http_x_forwarded_protocol’, $scheme)
then add django settings file:
middleware_classes = (... 'djo.middleware.securerequiredmiddleware', ) root_urlconf = 'djo.urls' https_support = true secure_required_paths = ( r'/admin/',
then create file in same django directory called "middleware.py" following contents:
from django.http import httpresponsepermanentredirect django.conf import settings class securerequiredmiddleware(object): def __init__(self): self.paths = getattr(settings, 'secure_required_paths') self.enabled = self.paths , getattr(settings, 'https_support') def process_request(self, request): if self.enabled , not request.is_secure(): path in self.paths: if request.get_full_path().startswith(path): request_url = request.build_absolute_uri(request.get_full_path()) secure_url = request_url.replace('http://', 'https://') print self.paths, request_url, secure_url return httpresponsepermanentredirect(secure_url) return none
voilá! redirects base url desired.
Comments
Post a Comment