[Solved]-Django + uwsgi + nginx + SSL

7👍

server {
  listen  80;
  server_name  yourhttphost;
  rewrite ^ https://yourhttpshost$request_uri? permanent; #301 redirect
}
server {
  listen 443;
  server_name  yourhttpshost;
  ........
  the rest
  ........
}

Using “if” in nginx config is a very bad idea!

14👍

I needed a bit more to make Django aware that it should be using https.

In settings.py I added
SECURE_PROXY_SSL_HEADER = (‘HTTP_X_FORWARDED_PROTO’, ‘https’)

And in the nginx configuration

location / {
    proxy_set_header X-Forwarded-Proto https;
    include uwsgi_params;
    uwsgi_param UWSGI_SCHEME https;
    uwsgi_pass_header X_FORWARDED_PROTO;
    uwsgi_pass unix:///path/to/socket;
}

3👍

if ( $scheme = "http" ) {
     rewrite ^/(.*)$   https://$host/ permanent;
}

Leave a comment