python - Django Admin 使用 nginx 端口 443 挂起

标签 python django ssl nginx gunicorn

我想用 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;
}
}

最佳答案

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

proxy_set_header X-Forwarded-Proto $scheme;

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

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

关于python - Django Admin 使用 nginx 端口 443 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32741932/

相关文章:

python - 如何在Python中检测字符串,无论大写、小写、有/没有空格?

python - 将每一行HTML表格读入python列表

javascript - 使用 Django 的下划线/ Backbone 模板

tomcat - 由于 "The filename, directory name, or volume label syntax is incorrect",Spring 引导无法在 tomcat 7 上加载 keystore 类型 JKS

python - 如何处理与坐标的碰撞? (在 python 中)

python - 如果列包含存储在另一个数据框中的字符串,则计算该列的总和

python - Django 模型表单 : Seeding a FK related field in the form

python - Rails 与 Django 部署

apache - SSL 证书不存在或为空 - WAMP

c# - 忽略无效的 SSL 证书