反向代理拒绝连接后的Asyncio Autobahn

时间:2015-12-03 17:20:23

标签: python-3.x reverse-proxy haproxy python-asyncio autobahn

我正在使用Autobahn与Asyncio构建一个与我的烧瓶应用程序分开的轻量级套接字服务器。我把它全部工作但是为了相应地路由流量我将这两个服务器放在HAProxy之后。我成功地向服务器发送请求,但随后连接关闭并且服务器报告:

WebSocket connection closed: connection was closed uncleanly (port 9001 in HTTP Host header 'localhost:9001' does not match server listening port 4000)

因此,标题与服务器期望的不匹配。有没有办法改变这个?

我在Python 3.4中使用Autobahn-python版本0.10.9。这是我的服务器代码:

from autobahn.asyncio.websocket import WebSocketServerProtocol, \
    WebSocketServerFactory
import asyncio
import json


class SimpleServer(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onOpen(self):
        print("WebSocket connection open.")

    @asyncio.coroutine
    def onMessage(self, payload, isBinary):
        if not isBinary:
            self.sendMessage(payload, isBinary)
        else:
            self.sendMessage(payload, isBinary)

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {0}".format(reason))


if __name__ == '__main__':

    factory = WebSocketServerFactory(u"ws://127.0.0.1:4000", debug=False)
    factory.protocol = SimpleServer

    loop = asyncio.get_event_loop()
    coro = loop.create_server(factory, '127.0.0.1', 4000)
    server = loop.run_until_complete(coro)

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.close()
        loop.close()

HAProxy是版本1.4.18,配置是:

global
    log 127.0.0.1›  local0
    log 127.0.0.1›  local1 notice
    maxconn 4096
    user root
    group sudo                                                                                                                                                                       
    debug
    #quiet

defaults
    log  global
    mode   http
    option  httplog
    option  dontlognull
    retries  3
    option  redispatch
    maxconn  2000
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000

frontend public
  bind *:9001
  acl is_websocket hdr(Upgrade) -i WebSocket
  use_backend ws if is_websocket
  default_backend www 

backend www 
  timeout server 30s 
  server www1 127.0.0.1:3000

backend ws
  timeout server 600s
  server ws1 127.0.0.1:4000

我正在运行Ubuntu 12.04。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

对于坚持这一点的人来说,通过改变

来解决问题
factory = WebSocketServerFactory(u"ws://127.0.0.1:4000", debug=False)

为:

factory = WebSocketServerFactory()

显然,在工厂中指定网址会导致高速公路对标题进行检查。