【问题标题】:Asyncio Autobahn behind reverse proxy rejecting connection反向代理背后的 Asyncio Autobahn 拒绝连接
【发布时间】:2016-03-08 10:32:43
【问题描述】:

我正在使用 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)

因此,标头与服务器期望的不匹配。有什么办法可以改变吗?

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

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。感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    任何人都坚持这一点,它已通过更改修复

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

    到:

    factory = WebSocketServerFactory()
    

    显然,在 Factory 中指定 url 将导致 Autobahn 对标头进行检查。

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2021-12-16
      • 2016-08-12
      • 2015-02-02
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      相关资源
      最近更新 更多