首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Gunicorn/Django/Nginx应用程序中使用SSL时的混合内容错误

在Gunicorn/Django/Nginx应用程序中使用SSL时的混合内容错误
EN

Stack Overflow用户
提问于 2019-03-03 00:43:40
回答 1查看 1.6K关注 0票数 0

我正在尝试为超台的一个实例配置HTTPS,该实例使用Gunicorn和Nginx进行路由。我已经安装了一个证书,并且(我想)在服务器上工作。不过,将浏览器指向应用程序会在火狐上“阻止加载混合活动内容”http://localhost/api,并在Chrome上显示"WebSocket连接到'ws://localhost/ws‘失败:连接建立错误: net::ERR_CONNECTION_REFUSED“。这个应用程序的文档几乎不存在,我已经花了无数个小时试图让它工作起来。我在问题上向开发人员提交了一个GitHub文件,但我对答案并不满意。这是我的Nginx配置:

代码语言:javascript
复制
server {
    listen 80;
    listen 443 ssl;

    server_name my_server_name;
    ssl on;
    ssl_certificate /path/to/my/cert.pem;
    ssl_certificate_key /path/to/my/key/key.pem;

    location /ws {
        proxy_pass http://localhost:5100;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout 3600;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
     }  
    location /api {
        proxy_pass http://localhost:5000;
        proxy_set_header Host localhost;
        expires epoch;

        sub_filter_once off;
        sub_filter_types application/json;
        sub_filter 'http://localhost' 'http://$host';
    }  
    location /contentapi {
        proxy_pass http://localhost:5400;
        proxy_set_header Host localhost;
        expires epoch;
    }  
    location /.well-known {
        root /var/tmp;
    }
    location / {
        root /opt/superdesk/client/dist;

        # TODO: use "config.js:server" for user installations
        sub_filter_once off;
        sub_filter_types application/javascript;
        sub_filter 'http://localhost' 'http://$host';
        sub_filter 'ws://localhost/ws' 'ws://$host/ws';
    }
    location /mail {
        alias /var/log/superdesk/mail/;
        default_type text/plain;
        autoindex on;
        autoindex_exact_size off;
    }
}

这是我第一次使用nginx/gunicorn/django应用程序,我完全迷失了方向。有人能为我指明正确的方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-06 18:17:49

对于任何试图设置Superdesk并有相同问题的人,我终于找到了正确的配置。

首先,下面是处理HTTPS请求和将HTTP请求重定向到HTTPS的Nginx配置:

代码语言:javascript
复制
server {
    listen                      443 ssl http2;
    listen                      [::]:443 ssl http2;
    server_name                 my.domain.com;

    ssl on;
    ssl_certificate             /path/to/my/cert.pem;
    ssl_certificate_key         /path/to/my/key.pem;
    ssl_protocols               TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

    location /ws {
    proxy_pass http://localhost:5100;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_read_timeout 3600;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

location /api {
    proxy_pass http://localhost:5000;
    proxy_set_header Host my.domain.com;
    expires epoch;

    sub_filter_once off;
    sub_filter_types application/json;
    sub_filter 'http://localhost' 'https://$host';
}

location /contentapi {
    proxy_pass http://localhost:5400;
    proxy_set_header Host my.domain.com;
    expires epoch;
}

location /.well-known {
    root /var/tmp;
}
location / {
    root /opt/superdesk/client/dist;

    # TODO: use "config.js:server" for user installations
    sub_filter_once off;
    sub_filter_types application/javascript;
    sub_filter 'http://localhost' 'https://$host';
    sub_filter 'ws://localhost/ws' 'wss://$host/ws';
}
location /mail {
    alias /var/log/superdesk/mail/;
    default_type text/plain;
    autoindex on;
    autoindex_exact_size off;
}

}

server {
    listen                      80;
    listen                      [::]:80;
    server_name                 my.domain.com;
    return                      301 https://$host$request_uri;
}

我在配置中缺少的内容:

proxy_set_header字段必须设置为proxy_set_header Host <my_domain name>,而在sub_filter字段中,它是必须设置使用HTTPS的第二个参数。

必须配置的特定于超级办公桌的东西:

在/opt/superdesk/activate.sh中,将HOST_SSL设置为HOST_SSL=${HOST_SSL:-s}。这将确保通过邮件发送的链接(如密码、rest电子邮件)以HTTPS的形式发送。

回顾起来似乎很简单,但是哇,用有限的Nginx知识很难弄清楚……

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54964520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档