Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx and proxying WebSockets

I'm trying to proxy WebSocket + HTTP traffic with nginx.

I have read this: http://nginx.org/en/docs/http/websocket.html

My config looks like:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

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

    server {
      listen 80;
      server_name ourapp.com;

      location / {
        proxy_pass http://127.0.0.1:100;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   Upgrade          $http_upgrade;
        proxy_set_header   Connection       $connection_upgrade;
      }
    }
}

I have 2 problems:

1) The connection closes once a minute.

2) I want to run both HTTP and WS on the same port. The application works fine locally, but if I try to put HTTP and WS on the same port and set this nginx proxy, I get this:

WebSocket connection to 'ws://ourapp.com/ws' failed: Unexpected response code: 200

Loading the app (HTTP) seems to work fine, but WebSocket connection fails.

like image 707
Kai Sellgren Avatar asked May 09 '13 19:05

Kai Sellgren


People also ask

Do Websockets work through reverse proxy?

WebSocket over a Reverse Proxy. WebSocket communication can take place over any reverse proxy which is configured to perform forwarding at the transport layer. Some proxies are able to handle WebSocket communication from certain clients at the application layer.

What is WebSocket proxying?

A reverse HTTP proxy over WebSocket is a type of proxy server which uses the WebSocket protocol as a "tunnel" to pass TCP communication from server to client. In Go project, gorilla/websocket is widely used to implement WebSocket. WebSocket is designed to work over HTTP.

How many Websockets can NGINX handle?

The number of connections varied from 1,000 to 50,000, message size from 10 to 4096 bytes, and the frequency of messages from 0.1 to 10 seconds (which we considered low).

What is WebSocket reverse proxy?

A reverse HTTP proxy over WebSocket is a type of proxies, which retrieves resources on behalf on a client from servers and uses the WebSocket protocol as a "tunnel" to pass TCP communication from server to client.


1 Answers

Problem 1: As for the connection dying once a minute, I realized that it's nginx timeout variable. I can either make our app to ping once in a while or increase the timeout. I'm not sure if I should set it as 0, I decided to just ping once a minute and set the timeout to 90 seconds. (keepalive_timeout)

Problem 2: Connectivity issues arose when I used CloudFlare CDN. Disabling CloudFlare acceleration solved the problem.

Alternatively I could create a subdomain and set it as "unaccelerated" and use that for WS.

like image 76
Kai Sellgren Avatar answered Sep 24 '22 18:09

Kai Sellgren