【问题标题】:Using webSocketURL() with reverse proxy in PlayFramework在 Play Framework 中使用带有反向代理的 webSocket URL()
【发布时间】:2014-06-10 14:15:33
【问题描述】:

我在 nginx 后面使用 Play Framework。

nginx 配置文件基本上和http://www.playframework.com/documentation/2.2.x/HTTPServer 中的一样, 但编辑 location 指令如下以启用 websocket。

location / {
  proxy_buffering off;
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   X-Scheme $scheme;
  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header   Host $http_host;

  proxy_http_version 1.1;
  proxy_pass  http://playframework;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";

}

在下面的 JavaScirpt 代码中,

var ws = new WebSocket(jsRoutes.controllers.MyController().MyMethod().webSocketURL())

webSocketURL() 返回以“ws://”开头的 URL。 由于 nginx 静默将 HTTP 转换为 HTTPS,因此无法建立 websocket 连接。

它可以在没有反向代理的本地环境中工作。

解决此问题的最佳方法是什么? 我当前的解决方法是禁用 SSL 以访问 http。

【问题讨论】:

  • 您的网页最初是从 HTTPS 页面提供的吗?如果是,则 websocket 将尝试通过wss:/// 安全协议进行连接。你需要实现安全的 websockets,我在这里概述了:stackoverflow.com/questions/12102110/…
  • 我还要补充一点,最新的 NGINX 支持开箱即用的 SSL TCP。此外,出于安全原因,无论如何使用wss:// 总是一个好主意:)

标签: playframework websocket playframework-2.2


【解决方案1】:

好的,我发现webSocketURL() 可以接受一个参数。

var isSerure = location.protocol === "https:";
var wsUrl = jsRoutes.controllers.MyController.myMethod().webSocketURL(isSecure);

这解决了我的问题。我不知道它记录在哪里,但它确实有效。

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2013-07-13
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多