12

我在 DotCloud 上使用 Django,它在 uwsgi + nginx 之上使用 Django。我正在尝试将所有 http 流量重定向到导致重定向循环的 https。我正在使用以下 http 配置

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

似乎Django不明白它是在https上运行的,并且没有保留标头。它将 https://url.com/重定向到http://url.com/accounts/login/,该重定向一次又一次地导致重定向循环。我不是 nginx 方面的专家,也不太了解它。我做错了什么?

简而言之,我如何在运行在 uswsgi 和 nginx 之上的 django 中将 http 重定向到 https。

4

3 回答 3

13

我需要更多让 Django 意识到它应该使用 https。

在 settings.py 我添加了 SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

并且在nginx配置中

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;
}
于 2013-02-15T10:59:06.160 回答
7
server {
  listen  80;
  server_name  yourhttphost;
  rewrite ^ https://yourhttpshost$request_uri? permanent; #301 redirect
}
server {
  listen 443;
  server_name  yourhttpshost;
  ........
  the rest
  ........
}

在 nginx 配置中使用“if”是一个非常糟糕的主意!

于 2011-10-08T10:24:27.040 回答
3
if ( $scheme = "http" ) {
     rewrite ^/(.*)$   https://$host/ permanent;
}
于 2011-08-22T11:17:52.537 回答