Search code examples
nginxwebsockettornadogoogle-compute-enginegoogle-cloud-platform

WebSocket opening handshake timed out


I'm working on a Google Cloud Compute Engine instance. Ubuntu 12.04. I have a Tornado app installed on the server working on port 8888 and I have nginx configuration as shown below:

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

upstream chat_servers {
    server 127.0.0.1:8888;
}

server {
    listen 80;
    server_name chat.myapp.com;

    access_log /home/ubuntu/logs/nginx_access.log;
    error_log /home/ubuntu/logs/nginx_error.log;

    location /talk/ {
        proxy_set_header X-Real-IP $remote_addr;  # http://wiki.nginx.org/HttpProxyModule
        proxy_set_header Host $host;  # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
        proxy_http_version 1.1;  # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
        # WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass       http://chat_servers;

    }
}

When I try to connect to ws://chat.myapp.com/talk/etc/ via Javascript, Tornado app's open() method on WebSocketHandler gets called and I print the log on the server successfully, but on the client side, the code never enters the onopen() and after some time I get 1006 error code,WebSocket opening handshake timed out`.

This app was working fine on Amazon (AWS) EC2 server with the same configuration but after I moved to Google Cloud, somehow the handshake cannot be done.

Is there any configuration specific to Google Cloud? Or any nginx update on the file?

I am confused and I spent two days on this but couldn't solve the problem.


Solution

  • Default nginx's version on Ubuntu was nginx/1.1.19. I updated it to nginx/1.8.0. The problem is solved.