姜戈 + uwsgi + nginx + SSL协议

发布于 2024-11-26 05:13:50 字数 506 浏览 1 评论 0原文

我在 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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-12-03 05:13:50

我需要更多的信息来让 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;
}

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;
}
萌无敌 2024-12-03 05:13:50
server {
  listen  80;
  server_name  yourhttphost;
  rewrite ^ https://yourhttpshost$request_uri? permanent; #301 redirect
}
server {
  listen 443;
  server_name  yourhttpshost;
  ........
  the rest
  ........
}

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

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!

雪若未夕 2024-12-03 05:13:50
if ( $scheme = "http" ) {
     rewrite ^/(.*)$   https://$host/ permanent;
}
if ( $scheme = "http" ) {
     rewrite ^/(.*)$   https://$host/ permanent;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文