Pergunta

I'm using Play Framework behind nginx.

nginx configuration file basically looks like the one found in http://www.playframework.com/documentation/2.2.x/HTTPServer, but edited location directive as follows in order to enable 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";

}

In following JavaScirpt code,

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

webSocketURL() returns URL that start with "ws://". Since nginx silently convert HTTP to HTTPS, websocket connection cannot be established.

It works in local environment which has no reverse proxy.

What is the best approach to fix this? My current workaround is disabling SSL for access to http.

Foi útil?

Solução

Ok, I found that webSocketURL() can take one argument.

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

This solved my problem. I don't know where it is documented, but it worked.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top