2014-04-25 11 views
1

我在nginx後面使用Play Framework。在PlayFramework中使用帶有反向代理的webSocketURL()

nginx配置文件基本上看起來像在http://www.playframework.com/documentation/2.2.x/HTTPServer, 中找到的那個,但是爲了啓用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()返回開始URL以 「WS://」。 由於nginx將HTTP靜默轉換爲HTTPS,因此無法建立websocket連接。

它在沒有反向代理的本地環境中工作。

解決這個問題的最佳方法是什麼? 我目前的解決方法是禁用SSL訪問http。

+0

您的網頁最初是從HTTPS頁面提供的嗎?如果是,那麼websocket將嘗試通過'wss:///'安全協議進行連接。你將需要實現安全的websockets,我在這裏列出:http://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss – crockpotveggies

+0

我還會補充說最新的NGINX支持開箱即用的SSL TCP。另外,出於安全原因,總是使用'wss://'好主意:) – crockpotveggies

回答

1

好的,我發現webSocketURL()可以採取一個參數。

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

這解決了我的問題。我不知道它在哪裏被記錄,但它的工作。