Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssl with django on AWS

Tags:

ssl

django

I have SSL certification at the ELB level for my site hosted on Amazon. I used the following site to setup a middle ware to forward all http requests to https:

http://djangosnippets.org/snippets/2472/

It's working great. But here's my question. EACH request is getting forwarded, so I notice a slight lag when clicking links, etc. Nothing extreme. But is there a way to force django to do everything via https? When I have code to HttpResponse and HttpResponseRedirect, how can I have it default to https instead of http? I tried to search for this and was unsuccessful...

I know it's possible if I type https://www... for each URL for redirect and on the links for the pages, but I wanted to avoid doing it that way if possible.

like image 897
KVISH Avatar asked Sep 17 '25 16:09

KVISH


2 Answers

Looking at the middleware you posted, it is doing exactly what you mentioned you did not want to manually do i.e append https to every incoming http request from your domain. I would recommend you offload this job to the front-end server (Either nginx or apache) .

Example with

  • Nginx

  • Apache

like image 173
Pratik Mandrekar Avatar answered Sep 20 '25 10:09

Pratik Mandrekar


When Django builds absolute URIs to redirect to, it checks request.is_secure to decide what protocol scheme it should be using (http, https, or ftp).

Django defaults to doing this based on the protocol used for the request, but as you've identified, when behind an LB or proxy this can be wrong due to SSL termination at the LB/proxy level.

You can configure Django to detect this exact scenario using the SECURE_PROXY_SSL_HEADER setting.

like image 27
lukewarm Avatar answered Sep 20 '25 11:09

lukewarm