带有 uwsgi 和 flask 的 nginx 仅显示对 https 连接的错误请求

nginx with uwsgi and flask shows bad request for https connections only

我正在尝试托管一个演示烧瓶应用程序,该应用程序 return "hi there" 当 运行 时。我已经通过此配置单独使用 Uwsgi 成功地通过 http 和 https 托管应用程序。 当我尝试使用 https 配置在本地主机上访问我的应用程序时,我得到

502. Bad Gatewat 
nginx/1.4.6 (Ubuntu)

这是我的 deployment.ini(uwsgi 配置文件)

[uwsgi]
shared-socket = 127.0.0.1:8443
master = true
processes = 5
daemonize = /path/to/my/mylog.log
wsgi-file=/path/to/my/demo.py
callable=app
https= =0,ca.crt,ca.key,HIGH

我也尝试过使用 unix 套接字并更改其权限 共享套接字=/path/to/my.soc chmod-socket=664

使用第一个配置我可以启动应用程序但只能使用 uwsgi,我想让它通过 nginx 工作。

这是我的 nginx 配置

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;
    error_log /path/to/nginx_err.log;
    listen 443 ssl default_server;

    server_name localhost;

    ssl_certificate path/to/ca.crt;
    ssl_certificate_key path/to/ca.key;
    location / {
            uwsgi_pass  127.0.0.1:8443;
            #uwsgi_pass   unix:///path/to/my.soc;
            include   uwsgi_params;
}
}

它在 https 上运行完美,但我不知道当我尝试提供 https 配置时发生了什么

我的日志文件/var/log/nginx/error.log

2016/03/22 15:58:07 [error] 12926#0: *2 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /user HTTP/1.1", upstream: "uwsgi://127.0.0.1:8443", host: "127.0.0.1"

我已经花了大约 8 个小时来解决这个问题,但还是做不到。 非常感谢任何帮助

当前面有nginx时,你不应该在uWSGI中配置任何与https相关的东西。

uWSGI 中的 https 设置仅用于直接连接到 uWSGI 服务器。这将激活内置 http/https 服务器,因此不再需要 nginx。更重要的是,uWSGI 将不再在指定端口上使用 uwsgi 协议进行通信,并且 nginx 在当前配置中需要该协议。

将您的 uWSGI 设置更改为:

[uwsgi]
socket = 127.0.0.1:8443
master = true
processes = 5
daemonize = /path/to/my/mylog.log
wsgi-file=/path/to/my/demo.py
callable=app

它会很好用。不要担心保护 nginx 或 uWSGI 之间的连接。如果是本地连接(环回或安全本地网络),则不会有问题。