Mark Lin
Mark Lin

Reputation: 37

A websocket via nginx proxy?

Hi I have a question ~ I want to use nginx proxy to distribute websocket connect to other port. like this .

client ====> proxy =====> ws server

but my question is the websocket tunnel is build on the client and ws server or client and proxy and ws server.

client === proxy ==== ws server

Or

client ===== ws server

Thanks ~~~

Upvotes: 1

Views: 1150

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

You don't skip proxy as such. The proxy makes the connection on behalf of you. See the below excerpt from https://www.nginx.com/blog/websocket-nginx/

A WebSocket application keeps a long‑running connection open between the client and the server, facilitating the development of real‑time applications. The HTTP Upgrade mechanism used to upgrade the connection from HTTP to WebSocket uses the Upgrade and Connection headers. There are some challenges that a reverse proxy server faces in supporting WebSocket. One is that WebSocket is a hop‑by‑hop protocol, so when a proxy server intercepts an Upgrade request from a client it needs to send its own Upgrade request to the backend server, including the appropriate headers. Also, since WebSocket connections are long lived, as opposed to the typical short‑lived connections used by HTTP, the reverse proxy needs to allow these connections to remain open, rather than closing them because they seem to be idle.

Edit-1 - Connections Comparisons between Nginx and direct NodeJS

So finally I did some test to make sure. Started a socket.io app on 3000 and nginx on 80 to forward to request to the Socket.io

Nginx

When I access the app using http://IP/ in two browser windows, the open connection are as below

$ lsof -i
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    4443 vagrant   11u  IPv6  25730      0t0  TCP *:3000 (LISTEN)
node    4443 vagrant   13u  IPv6  28591      0t0  TCP localhost:3000->localhost:42698 (ESTABLISHED)
node    4443 vagrant   14u  IPv6  28626      0t0  TCP localhost:3000->localhost:42712 (ESTABLISHED)
nginx   5144 vagrant    6u  IPv4  28402      0t0  TCP *:http (LISTEN)
nginx   5144 vagrant   13u  IPv4  28589      0t0  TCP 192.168.33.100:http->192.168.33.1:64799 (ESTABLISHED)
nginx   5144 vagrant   14u  IPv4  28590      0t0  TCP localhost:42698->localhost:3000 (ESTABLISHED)
nginx   5144 vagrant   15u  IPv4  28625      0t0  TCP localhost:42712->localhost:3000 (ESTABLISHED)
nginx   5144 vagrant   16u  IPv4  28624      0t0  TCP 192.168.33.100:http->192.168.33.1:64826 (ESTABLISHED)

Now after closing both the tabs

$ lsof
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    4443 vagrant   11u  IPv6  25730      0t0  TCP *:3000 (LISTEN)
nginx   5144 vagrant    6u  IPv4  28402      0t0  TCP *:http (LISTEN)

NodeJS

Opening two browser tabs with direct connection to NodeJS http://<IP>:3000. Then the results are as below

$ lsof -i
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    4443 vagrant   11u  IPv6  25730      0t0  TCP *:3000 (LISTEN)
node    4443 vagrant   13u  IPv6  30014      0t0  TCP 192.168.33.100:3000->192.168.33.1:52550 (ESTABLISHED)
node    4443 vagrant   14u  IPv6  30015      0t0  TCP 192.168.33.100:3000->192.168.33.1:52551 (ESTABLISHED)
node    4443 vagrant   15u  IPv6  30016      0t0  TCP 192.168.33.100:3000->192.168.33.1:52552 (ESTABLISHED)
node    4443 vagrant   16u  IPv6  30017      0t0  TCP 192.168.33.100:3000->192.168.33.1:52553 (ESTABLISHED)
node    4443 vagrant   17u  IPv6  30018      0t0  TCP 192.168.33.100:3000->192.168.33.1:52554 (ESTABLISHED)
node    4443 vagrant   18u  IPv6  30020      0t0  TCP 192.168.33.100:3000->192.168.33.1:52556 (ESTABLISHED)
node    4443 vagrant   19u  IPv6  30024      0t0  TCP 192.168.33.100:3000->192.168.33.1:52578 (ESTABLISHED)
nginx   5144 vagrant    6u  IPv4  28402      0t0  TCP *:http (LISTEN)

This may look a bit strange, but this is because the connections used to open the site are also kept alive for few seconds. So after waiting for sometime

$ lsof -i
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    4443 vagrant   11u  IPv6  25730      0t0  TCP *:3000 (LISTEN)
node    4443 vagrant   18u  IPv6  30020      0t0  TCP 192.168.33.100:3000->192.168.33.1:52556 (ESTABLISHED)
node    4443 vagrant   19u  IPv6  30024      0t0  TCP 192.168.33.100:3000->192.168.33.1:52578 (ESTABLISHED)
nginx   5144 vagrant    6u  IPv4  28402      0t0  TCP *:http (LISTEN)

As you can see no. of connections will be always be higher when you use Nginx

Upvotes: 1

Related Questions