Search code examples
djangouwsgidotcloud

Django + uwsgi + nginx + SSL


I am using Django on DotCloud which uses Django on top of uwsgi + nginx. I am trying to redirect all http traffic to https which is leading to a redirect loop. I am using the following http configuration

if ($http_x_forwarded_port != 443) { rewrite ^ https://$http_host/; }

It seems that Django doesn't understand that it is operating on https and the header is not preserved. It redirects https://url.com/ to http://url.com/accounts/login/ which is redirecting again and again leading to a redirect loop. I am not really an expert in nginx and do not understand it well enough. What can I be doing wrong?

In short how do I run redirect http to https in django running on top of uswsgi and nginx.


Solution

  • 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!