2013-07-02 197 views
2

嗨我想配置nginx作爲websockets的反向代理。配置我的服務器如下:nginx websocket反向代理配置

server { 
    listen  80; 
    server_name www.mydomain.com; 

    access_log off; 
    #error_log off; 

    location/{ 
     proxy_pass   http://127.0.0.1:8765; 
     proxy_redirect  off; 

     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "Upgrade"; 
     proxy_set_header Host $http_host; 

     proxy_buffering off; 
    } 

} 

,但我得到一個錯誤,從客戶端像下面

WebSocket連接到「WS://www.application.com/ws」失敗:WebSocket的握手過程中的錯誤: '連接'標題值不是'升級'

我可能做了一些配置錯誤,但我看不到它。

客戶端請求頭是繼

GET ws://www.talkybee.com/ws HTTP/1.1 
Pragma: no-cache 
Origin: http://www.talkybee.com 
Host: www.talkybee.com 
Sec-WebSocket-Key: Ol+O1IdaLEsHxxWRBt2oqg== 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 
Upgrade: websocket 
Sec-WebSocket-Extensions: x-webkit-deflate-frame 
Cache-Control: no-cache 
Connection: Upgrade 
Sec-WebSocket-Version: 13 

當我做一個正常的直接連接,我的連接只是工作。這是工作請求頭。

Cache-Control:no-cache 
Connection:Upgrade 
Host:www.talkybee.com:8765 
Origin:http://www.talkybee.com:8765 
Pragma:no-cache 
Sec-WebSocket-Extensions:x-webkit-deflate-frame 
Sec-WebSocket-Key:Y026b/84aUkMxVb0MaKE2A== 
Sec-WebSocket-Version:13 
Upgrade:websocket 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 
+0

如果您只是嘗試'proxy_pass http://127.0.0.1:8765 /;會發生什麼情況' – rednaw

+0

@rednaw我得到同樣的例外。我改變了當前問題中的配置。 – yilmazhuseyin

+0

你的客戶是否也將'Connection'頭設置爲'Upgrade'? – rednaw

回答

20

此問題與nginx版本有關。請檢查nginx -v,檢查您的版本。 1.4版本後支持以下參數。

# WebSocket support (nginx 1.4) 
proxy_http_version 1.1; 
proxy_set_header Upgrade $http_upgrade; 
proxy_set_header Connection "upgrade"; 

如果您在使用Ubuntu,你可以用這個步驟安裝新版本:

首先刪除舊的版本(https://askubuntu.com/questions/235347/what-is-the-best-way-to-uninstall-nginx):

sudo apt-get remove nginx 
sudo apt-get purge nginx 
sudo apt-get autoremove 

然後安裝新版本(https://launchpad.net/~nginx/+archive/development) :

sudo add-apt-repository ppa:nginx/development 
sudo apt-get update 
sudo apt-get install nginx 
+0

我也有這個問題,但我通過升級我的nginx服務器(〜$ nginx -v) (nginx版本:nginx/1.5.1)解決此問題 – yilmazhuseyin

+0

我對這個問題的解決方案是連接websocket的直接端口。所以我的網站是在端口:80,但我的websocket連接到端口:8765看起來像websocket沒有跨域限制畢竟。 – yilmazhuseyin