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
1 |
$ yum install nodejs 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.
1 |
npm install ws wscat |
2-3.Create a simple server
1 2 3 4 5 6 7 8 9 10 11 |
$ vim server.js console.log("Server started"); var Msg = ''; var WebSocketServer = require('ws').Server , wss = new WebSocketServer({port: 8010}); wss.on('connection', function(ws) { ws.on('message', function(message) { console.log('Received from client: %s', message); ws.send('Server received from client: ' + message); }); }); |
Running the server
1 2 |
$ node server.js Server started |
Make sure the server is started normally
1 2 |
$ netstat -tlunp|grep 8010 tcp6 0 0 :::8010 :::* LISTEN 23864/nodejs |
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.
1 2 3 4 5 6 |
$ cd /root/node_modules/wscat/bin/ $ ./wscat --connect ws://127.0.0.1:8010 connected (press CTRL+C to quit) > Hello < Server received from client: Hello > Welcome to www.hi-linux.com < Server received from client: Welcome to www.hi-linux.com |
3.Use Nginx to reverse proxy WebSocket
Install Nginx
1 |
$ yum -y install nginx |
Configuring Nginx Websocket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$ vim /usr/local/nginx/conf/nginx.conf # Add the following configuration in the http area to ensure that Nginx can handle normal http requests. http{ map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream websocket { #ip_hash; server localhost:8010; server localhost:8011; } # add the following configuration in the server area, Location refers to the path used for websocket connections. server { listen 80; server_name localhost; access_log /var/log/nginx/yourdomain.log; location / { proxy_pass http://websocket; proxy_read_timeout 300s; 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } } |
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.
1 2 |
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; |
The key part here is that the HTTP request has the following header:
1 2 |
Upgrade: websocket Connection: Upgrade |
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.
1 2 3 |
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: upgrade |
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
1 |
/etc/init.d/nginx start |
Try to access the WebSocket service through Nginx
1 2 3 4 5 6 |
$ cd /root/node_modules/wscat/bin/ $ ./wscat --connect ws://192.168.2.210 connected (press CTRL+C to quit) > Hello Nginx < Server received from client: Hello Nginx > Welcome to www.hi-linux.com < Server received from client: Welcome to www.hi-linux.com |
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 :http://www.nginxer.com/records/detailed-explanation-of-nginx-websocket-configuration/