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

╄→尐↘猪︶ㄣ 提交于 2021-02-08 06:44:23

问题


I am trying to host a demo flask application which return "hi there" when run. I have succeeded to host the application with http and https using Uwsgi alone with this configuration. When i try to access my application on localhost with https config, i get

502. Bad Gatewat 
nginx/1.4.6 (Ubuntu)

here goes my deployment.ini (uwsgi configuration file)

[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

I have tried using unix socket also and changin the permissions on it shared-socket=/path/to/my.soc chmod-socket=664

using the first configuration i can launch the application but only with uwsgi, i want to make it work through nginx.

here goes my nginx configuration

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;
}
}

It is working perfectly for https but i dont know what is happening when i try to give https configurations

my log file /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"

I have already spent around 8 hours to sort this out but not able to . Any help is highly appreciated


回答1:


You shouldn't configure anything related to https in uWSGI when there is nginx in front of it.

https settings in uWSGI are only for direct connection to uWSGI server. This will activate built-in http/https server so nginx is no longer needed. More on that, uWSGI won't talk in uwsgi protocol anymore on specified port, and that protocol is expected by nginx in current configuration.

Change your uWSGI settings to:

[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

And it will work fine. Don't worry about securing connection between nginx or uWSGI. If it's local connection (loopback or secured local network) it won't be an issue.



来源:https://stackoverflow.com/questions/36155284/nginx-with-uwsgi-and-flask-shows-bad-request-for-https-connections-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!