【问题标题】:Traefik configuration to allow websocketTraefik 配置以允许 websocket
【发布时间】:2023-04-03 08:59:01
【问题描述】:

我有一个由以下服务组成的应用:

  • tls 终止(基于 https-portal)
  • 反向代理(nginx)
  • 其他服务(Web、api、wss、...)

所有的流量都是这样的:

https-portal -> nginx -> 其他服务

我正在用 Traefik 替换 https-portal,但我无法让 websocket 正确通过。

以下是我的 docker-compose.yml 文件的摘录。进入 Traefik 的流量被转发到反向代理的专用端口。这将执行一些检查,然后将请求转发到适当的服务(api、web 前端、websocket 服务器......)

  # Proxy
  proxy:
    image: mynginxproxy
    build: ../../../development/proxy
    restart: always
    labels:
      - traefik.web.frontend.rule=Host:app.dev
      - traefik.web.port=8000
      - traefik.wss.frontend.rule=Host:wss.app.dev
      - traefik.wss.protocol=ws
      - traefik.wss.port=9002
      - traefik.api.frontend.rule=Host:api.app.dev
      - traefik.api.port=8002
      - ... // other labels

  # Traefik
  traefik:
    image: traefik
    command: --web --docker --docker.domain=app.dev --logLevel=DEBUG
    labels:
      - "traefik.enable=false"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /dev/null:/traefik.toml

当 web 前端提供服务时,在 app.dev 域上,尝试在 ws://wss.app.dev 上建立 websocket 连接,但它无法连接到底层 websocket 服务器。该错误似乎与设置为 app.dev 的 Origin 标头有关,其中目标服务器为 wss.app.dev。

日志表单traefik:

traefik_1        | time="2017-08-02T13:55:05Z" level=warning msg="Error while upgrading connection : websocket: 'Origin' header value not allowed"

我不确定我在这里缺少什么,因为这可以通过 https-portal 正确通过。 Traefik 中是否需要任何其他选项?

【问题讨论】:

  • 很有趣,我不确定我是否有答案,但我将 traefik 作为反向代理运行,而不仅仅是 https 门户,我的 websocket 无需在 traefik 中进行任何额外配置即可工作。虽然我没有在 docker-compose 中为我的服务定义端口,并且只使用 nginx 来提供静态内容。不确定这是否真的有帮助......但我知道它以某种方式是可能的。希望有人有答案,但如果你在这里没有得到答案,可能值得在 Traefik 的 github 页面上提问。祝你好运!
  • 你的 traefik 版本是多少?
  • 感谢@ArthurWeborg。我使用的是 1.3.4,但我还没有尝试过 1.3.5

标签: websocket proxy traefik


【解决方案1】:

WebSocket 无需额外配置。

traefik.wss.protocol=httptraefik.wss.protocol=https

我们在 1.3.2 和 1.3.8 之前的 WebSocket 存在一个错误。

我建议您使用 1.4.0 或至少 1.3.8

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2012-09-19
    • 2020-11-01
    • 2019-08-27
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多