【问题标题】:Django Admin hangs with nginx port 443Django Admin 挂起 nginx 端口 443
【发布时间】:2015-12-20 21:51:01
【问题描述】:

我想用 nginx 设置 django,用 https 设置 gunicorn。但是,如果我想去 django 的管理员,它会在加载时挂起。

我使用以下命令调用应用程序: gunicorn core.wsgi:application --bind 127.0.0.1:8181 --certfile=/etc/nginx/ssl/ssl-bundle.crt --keyfile=/etc/nginx/ssl/hoi.key

我已经在我的 settings.py 中:

  • CSRF_COOKIE_SECURE = 真
  • SESSION_COOKIE_SECURE = 真
  • SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

这是我的 nginx 配置:

server{
listen   80;
listen 443 ssl;
server_name test.hello.com;
access_log /var/log/nginx/neat.access.log;
error_log /var/log/nginx/neat.error.log;
root /var/www/Test/;
ssl on; 
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/hoi.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
location / 
{
    add_header Access-Control-Allow-Origin "*";
    proxy_pass https://127.0.0.1:8181;          
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
}
}

【问题讨论】:

    标签: python django ssl nginx gunicorn


    【解决方案1】:

    您正在告知 django 如何检测请求是否通过 https 完成,但您的 nginx 文件中没有设置正确的标头。尝试添加:

    proxy_set_header X-Forwarded-Proto $scheme;
    

    进入 nginx 配置中的位置部分。

    此外,您不必在 gunicorn 和 nginx 之间建立安全连接。他们在 localhost 中进行通信,并且没有任何东西可以潜在地嗅探该通信(除了您自己服务器上的东西,但那些能够嗅探 localhost 连接的人也可以访问您的证书并解密 https)。如果您想将该连接设置为安全的(因为例如不仅您可以访问该服务器),请使用 unix 套接字和适当的访问权限。

    【讨论】:

    • 你的评论我明白了。现在它起作用了!非常感谢。你帮帮我,因为我几乎被困了 3 天。大大的竖起大拇指! :-)
    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2016-03-05
    • 2021-07-31
    • 2018-01-25
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多