【问题标题】:Trouble getting SSL to work with django + nginx + wsgi无法让 SSL 与 django + nginx + wsgi 一起工作
【发布时间】:2011-01-07 05:15:03
【问题描述】:

我已经关注了 Django + nginx + wsgi + ssl 的几个示例,但我无法让它们工作。我只是在浏览器中收到一个错误,而不是无法连接。

我在主机之外运行两个网站。除了 ip 地址、服务器名称和目录之外,配置文件是相同的。

当两者都不使用 SSL 时,它们可以正常工作。当我尝试使用其中一个在 443 上收听时,我无法连接到其中一个。

我的配置文件如下,如有任何建议,我们将不胜感激。

server{
listen xxx.xxx.xxx.xxx:80;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

location / { 
    proxy_pass  http://127.0.0.1:8080; 
    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;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90; 
    proxy_send_timeout      90; 
    proxy_read_timeout      90; 
    proxy_buffers           32 4k; 
}   

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}   

location /admin-media/ {
    alias /home/django/django_admin_media/;
}   
}

server{
listen xxx.xxx.xxx.xxx:443;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

ssl on; 
ssl_certificate sub.domain.com.crt;
ssl_certificate_key sub.domain.com.key;
ssl_prefer_server_ciphers   on; 


location / { 
    proxy_pass              http://127.0.0.1:8080; 
    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;
    proxy_set_header        X-Forwarded-Protocol    https;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;
}

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}

location /admin-media/ {
    alias /home/django/django_admin_media/;
}
}


<VirtualHost *:8080>
ServerName xxx.xxx.xxx.xxx
ServerAlias xxx.xxx.xxx.xxx

LogLevel warn
ErrorLog /home/django/logs/apache_customerdb_error.log
CustomLog /home/django/logs/apache_customerdb_access.log combined

WSGIScriptAlias / /home/django/customerdb/apache/django.wsgi
WSGIDaemonProcess customerdb_wsgi processes=4 threads=5
WSGIProcessGroup customerdb_wsgi

SetEnvIf X-Forwarded-Protocol "^https$" HTTPS=on

</VirtualHost>

UDPATE:主机上存在两个站点(在不同的 IP 上)是问题所在。如果我删除另一个网站,上面的设置大部分都可以工作。这样做也带来了另一个问题:chrome 不接受该网站作为安全说某些内容未加密。

【问题讨论】:

    标签: ssl nginx mod-wsgi


    【解决方案1】:

    [这实际上应该是评论...]

    你也应该设置

    proxy_set_header X-Forwarded-Protocol $scheme

    在连接安全时向 Django 指示,否则您的 https 链接将被重定向到 http,这是不好的。

    这将设置http,实际为httphttps,实际为https

    【讨论】:

    【解决方案2】:

    我更改了监听 80 的服务器以重写为 https 删除了所有其他指令。

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 2014-05-15
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多