4

Hi I am trying to configure nginx as reverse proxy for websockets. I configure my server as following:

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;
    }

}

but I get an error from client like following

WebSocket connection to 'ws://www.application.com/ws' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'

I am probably doing some configuration wrong but I could not see it.

Request headers for client is following

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

When I do a normal direct connection, My connection just works. Here is the working request header.

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
7
  • what happens if you just try proxy_pass http://127.0.0.1:8765/; Commented Jul 2, 2013 at 13:53
  • @rednaw I am getting the same exception. I changed the config in the question with current one. Commented Jul 2, 2013 at 14:01
  • Is your client also setting the Connection header to Upgrade? Commented Jul 2, 2013 at 14:16
  • @rednaw. Yes It seems like it has connection:Upgrade header. I will try to get diff of working direct connection header and proxied header. Commented Jul 2, 2013 at 15:04
  • @rednaw looks like I am missing Sec-WebSocket-Key: header. I guess I should find a way to mass those headers too. Commented Jul 2, 2013 at 15:08

1 Answer 1

21

This issue is related with the nginx version. Pleas check nginx -v, to check your version. The followings params are supported after the 1.4 version.

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

If you are using ubuntu you can install a newer version with this steps:

First remove the old version (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 

Then install a new version (https://launchpad.net/~nginx/+archive/development):

sudo add-apt-repository ppa:nginx/development
sudo apt-get update
sudo apt-get install nginx
Sign up to request clarification or add additional context in comments.

2 Comments

I had that problem too but I solve this by upgrading my nginx server (~$ nginx -v) (nginx version: nginx/1.5.1)
The solution I had for this problem was connecting direct port for websocket. So my site is on port:80 but my websocket connection goes to port:8765 looks like websocket does not have a cross domain restriction afterall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.