Detailed explanation of Nginx Websocket configuration

V Records 3,622 Views No comment

1.What is websocket

The difference between the WebSocket protocol and the HTTP protocol is that WebSocket can communicate many times after a successful handshake until the connection is shut down. WebSocket’s handshake is compatible with an HTTP handshake, which use the upgrade protocol header from HTTP to WebSocket. This makes it easier for WebSocket programs to use existing infrastructure.

WebSocket works on ports 80 and 443 of HTTP and use the prefix ws:// or WSS :// to label the protocol. It use  the 101 status code of HTTP/1.1 to switch the protocol when establishing the connection. The current protocol standard does not support the direct establishment of WebSocket connection between two clients without HTTP.

2.Creating WebSocket services based on Node
2-1.Install node.js and NPM

2-2.Install ws and wscat modules
Ws is the WebSocket implementation of nodejs, which we use to build a simple WebSocket Echo Server.
The Wscat is an executable WebSocket client that is used to debug WebSocket services.

2-3.Create a simple server

Running the server

Make sure the server is started normally

Use wscat as a client test
The wscat command will install the current user directory in the node_modules/wscat/ by default, my directory location is /root/node_modules/wscat/bin/wscat .

Enter any content to test, if you get the same return results, it means work fine.

3.Use Nginx to reverse proxy WebSocket
Install Nginx

Configuring Nginx Websocket

The most important thing is to add the following two lines to the configuration of the reverse proxy. The other parts are no different from the normal HTTP reverse proxy.

The key part here is that the HTTP request has the following header:

These two fields indicate that the request server upgrade protocol is WebSocket.
After the server processes the request, the server response is as follows: The status# code is 101.

Tell the client that the protocol has been successfully switched and upgrade to the Websocket protocol. After the handshake is successful, the roles of the Server and the client will be equivalent, just like a normal Socket, capable of two-way communication. Instead of HTTP interaction, the data frame protocol of WebSocket is started to implement data exchange.

Here you can use the map command to combine the variables into new variables. It will decide whether to pass the Connection header to the source station according to whether the connection header from the client has an Upgrade header. This method is more elegant than directly transferring the upgrade.

By default, the connection will be closed 60 seconds after no data transfer, the proxy_read_timeout parameter can be extended this time or the source station will continue to send a ping frame to keep the connection and confirm that the connection is still in use.

Start nginx

Try to access the WebSocket service through Nginx

Test success.

This article was first published by V on 2018-08-29 and can be reprinted with permission, but please be sure to indicate the original link address of the article :

Leave a Reply

Your email address will not be published. Required fields are marked *