2016-07-27 74 views
1

我無法在我的Flask web應用程序中建立websocket。由nginx代理的網絡套接字通過HTTPS提供給400(壞請求)

在客戶端,我每秒向服務器發送一個「ping」websocket事件。在瀏覽器控制檯,我看到下面的錯誤每秒

POST https://example.com/socket.io/?EIO=3&transport=polling&t=LOkVYzQ&sid=88b5202cf38f40879ddfc6ce36322233 400 (BAD REQUEST) 

GET https://example.com/socket.io/?EIO=3&transport=polling&t=LOkVZLN&sid=5a355bbccb6f4f05bd46379066876955 400 (BAD REQUEST) 

WebSocket connection to 'wss://example.com/socket.io/?EIO=3&transport=websocket&sid=5a355bbccb6f4f05bd46379066876955' failed: WebSocket is closed before the connection is established. 

我有以下nginx.conf

server { 
    listen 80; 
    server_name example.com www.example.com; 
    return 301 https://$server_name$request_uri; 
} 

upstream app_server { 

    # for UNIX domain socket setups 
    server unix:/pathtowebapp/gunicorn.sock fail_timeout=0; 

} 

server { 

    listen 443 ssl; 

    server_name example.com www.example.com; 

    keepalive_timeout 5; 

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on; 
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; 

    charset  utf-8; 
    client_max_body_size 30M; 

    location/{ 
     try_files $uri @proxy_to_app; 
    } 

    location /socket.io { 
     proxy_pass http://app_server; 
     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_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Upgrade websocket; 
     proxy_set_header Connection "upgrade"; 
     proxy_read_timeout 86400; 
     proxy_buffering off; 
     proxy_headers_hash_max_size 1024; 

    } 


    location /static { 
     alias /pathtowebapp/webapp/static; 
    } 

    location @proxy_to_app { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

     # enable this if and only if you use HTTPS 
     proxy_set_header X-Forwarded-Proto https; 

     proxy_set_header X-Forwarded-Proto $scheme; 

     proxy_set_header Host $http_host; 

     # we don't want nginx trying to do something clever with 
     # redirects, we set the Host: header above already. 
     proxy_redirect off; 
     #proxy_buffering off; 
     proxy_pass http://app_server; 

    } 

} 

我一直在尋找所有與前面使用nginx的HTTPS工作的一個的WebSocket的例子gunicorn。

我的網頁加載,雖然websocket連接不成功。

客戶端的WebSocket是使用下面的JavaScript建立:

var socket = io.connect('https://' + document.domain + ':' + location.port + namespace); 

這裏是我的gunicorn.conf

import multiprocessing 

bind = 'unix:/pathtowebapp/gunicorn.sock' 
workers = multiprocessing.cpu_count() * 2 + 1 
worker_class = 'eventlet' 

[編輯]如果我配置nginx的是在Flask-IO documentation和方式只要運行(env)$ python deploy_app.py然後它的工作。但我的印象是,這並不像我之前提到的設置那樣生產理想

回答

2

問題是,您正在使用gunicorn運行多個工人。這不是目前支持的配置,因爲gunicorn中的負載平衡器非常有限,不支持粘性會話。文檔參考:https://flask-socketio.readthedocs.io/en/latest/#gunicorn-web-server

取而代之,運行幾個gunicorn實例,每個實例都有一個worker,然後使用ip_hash方法設置nginx來執行負載平衡,以便會話保持粘性。另外,如果你不知道,如果你運行多個服務器,你還需要運行一個消息隊列,以便進程能夠協調。這在上面的文檔鏈接中也有介紹。

相關問題