Nginx not proxying websockets properly

nginxsocket

Using the development version of nginx (1.3.12). In my relevant file under sites-enabled:

upstream twisted {
    server 127.0.0.1:8088;
}

server {  

    listen   80; ## listen for ipv4  

    server_name  *.clurn.co.uk clurn.co.uk;  

    access_log  /var/log/nginx/clurn.co.uk.access.log;  

    location / {  
            root   /var/www/clurn.co.uk;  
            index  index.html index.htm;  

    }  

    location /websocket {  
            proxy_pass http://twisted;  
            proxy_http_version 1.1;  
            proxy_set_header Upgrade $http_upgrade;  
            proxy_set_header Connection "Upgrade";  
    }  

    location /websocket/server {  
            deny all;  
    }  

    [...snip...]
}

Only difference from the example on nginx's site is the capital u on upgrade, which seemed to be necessary for the Autobahn and/or txWS instances to recognise the websockets correctly (I can't remember which one it was atm).

Now, if I access the websockets via 127.0.0.1:8088 on the server, then it appears to work, but if I access it via 127.0.0.1/websockets it doesn't, so there has to be something wrong with the marshalling of the packets via nginx.

If I run netcat -l -p 8088 instead of the Twisted websocket backend, I can see that the requests are identical if accessed via nginx or directly (originally they weren't, but changing the "upgrade" to "Upgrade" made the header line Connection: Upgrade the same), so this part is proxying correctly.

However, if I telnet into 127.0.0.1:8088 and 127.0.0.1/websocket, copying and pasting the correct request, I get a response with the direct 127.0.0.1:8088, and do not with the 127.0.0.1/websocket. What am I doing wrong, and how can I set it up to allow websockets on both sides?

Best Answer

I think the problem is buffering. Set:

proxy_buffering off;

inside the websocket location config.