Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nginx tornado上的websocket连接立即关闭_Javascript_Nginx_Websocket_Tornado - Fatal编程技术网

Javascript nginx tornado上的websocket连接立即关闭

Javascript nginx tornado上的websocket连接立即关闭,javascript,nginx,websocket,tornado,Javascript,Nginx,Websocket,Tornado,我真的很想用nginx配置tornado websocket连接配置,最后我遇到了一个问题,但我无法解决。我的配置有错误吗? 这是我的websocket.py: class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", ChatSocketHandler) ] settings = dict(

我真的很想用nginx配置tornado websocket连接配置,最后我遇到了一个问题,但我无法解决。我的配置有错误吗? 这是我的websocket.py:

class Application(tornado.web.Application):


   def __init__(self):

       handlers = [
            (r"/", ChatSocketHandler)
       ]
       settings = dict(

        cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
        template_path=os.path.join(os.path.dirname(__file__), 
          "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        xsrf_cookies=False,
        debug = True
    )
    super(Application, self).__init__(handlers, **settings)

class ChatSocketHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
       return True
    def open(self, *args, **kwargs):
       print('connection opened')

    def on_close(self):
         print('connection closed')
    def on_message(self, message):
         print(message)
         self.write_message(message)
这是我的客户端脚本:

<script>

  let url = "ws://104.131.115.151:8888/";
  let socket = new WebSocket(url);
  socket.onmessage = function(event) {
      console.log(event);
  };
  socket.onopen = function () {
     console.log('opend');
     socket.send('Hello World')
  };
  socket.onclose = function () {
      console.log('closed');
  };

</script>
在没有nginx配置的本地计算机上运行时:

opened
MessageEvent{...}
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
 }
upstream pythonserver {

    server 127.0.0.1:8888;

}
server{
         listen 80;
         server_name 209.97.139.107;

         location /chatsocket {

              proxy_pass http://pythonserver;
              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_buffers 8 32k;
              proxy_buffer_size 64k;
              proxy_read_timeout 86400s;
              proxy_send_timeout 86400s;
              keepalive_timeout 90;
              proxy_cache off;
              proxy_buffering off;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "Upgrade";
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
        location / {
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
}

这是我的nginx配置:

opened
MessageEvent{...}
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
 }
upstream pythonserver {

    server 127.0.0.1:8888;

}
server{
         listen 80;
         server_name 209.97.139.107;

         location /chatsocket {

              proxy_pass http://pythonserver;
              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_buffers 8 32k;
              proxy_buffer_size 64k;
              proxy_read_timeout 86400s;
              proxy_send_timeout 86400s;
              keepalive_timeout 90;
              proxy_cache off;
              proxy_buffering off;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "Upgrade";
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
        location / {
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
}

另外,我正在使用云和我的服务器url,很可能您的nginx配置不完整

当浏览器尝试连接到websocket时,它会发送
升级
连接
http头。这会告诉服务器升级到websocket的连接

问题(也是一个常见的问题)似乎是浏览器正在将升级头发送到nginx,但nginx没有将这些头发送回Tornado。要成功连接websocket,升级头必须到达Tornado后端

解决方案:

解决方案是告诉Nginx将升级头发送回Tornado,以便完成websocket连接

您的配置应如下所示:

location / {
    # ... other variables ...

    # variables for websocket
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

重新加载您的Nginx服务器,它应该可以工作。

我也尝试过,但无法工作。请参见下面的我的Nginx配置。