Nginx Proxy websocket Configuration

Source: Internet
Author: User
Tags nginx server node server

Nginx can only proxy HTTP requests, if you want to implement proxy websocket requirements, you need to add the "Upgrade" field in the request to upgrade the request from HTTP to WebSocket.

The configuration is as follows:

http {    map  $http _upgrade  $connection _upgrade {         default upgrade;         '        close;    }     server {         ...         location  /chat/ {            proxy_pass  Http://backend;            proxy_http_version  1.1;             #以下配置添加代理头部:             proxy_set_header Upgrade  $http _upgrade;             proxy_set_header Connection  $connection _upgrade;        }    } 


Experimental cases:

Configure the Websockeet server, and the client connects to the server side via proxy. The environment is as follows:

WebSocket Service side: 192.168.1.110:8010

nginx:192.168.1.131:8081

1: Use Nodejs to implement a websocket server, monitor 8010 ports, wait for WebSocket link.

Install Nodejs with NPM, and install the WS module. (This is implemented via the official website package)

[Email protected]_168_1_110 ~]# tar-zxvf node-v6.2.0-linux-x64.tar.gz[[email protected]_168_1_110 ~]# ln-s/root/ Node-v6.2.0-linux-x64/bin/node/usr/local/bin/node[[email protected]_168_1_110 ~]# ln-s/root/node-v6.2.0-linux-x64 /bin/node/usr/local/bin/node[[email protected]_168_1_110 ~]# npm install-g ws[[email protected]_168_1_110 ~]# npm Insta Ll-g Wscat

Write the WebSocket Service listener:

[[Email protected]_168_1_110 ~]# vim server.jsconsole.log ("server Started"); var Msg = '; var websocketserver = require (' W S '). 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 ');});

To start the WebSocket server:

[Email protected]_168_1_110 ~]# node Server.jsserver started

To re-open a terminal, use the WSCAT program to test the program connection:

[Email protected]_168_1_110 ~]#/root/node-v6.2.0-linux-x64/lib/node_modules/wscat/bin/wscat--connect ws:// 192.168.1.110:8010connected (Press CTRL + C to quit) > "WebSocket test...< Server received from Client:this I S a websocket test...>

View Service side:

[Email protected]_168_1_110 ~] #node server.jsserver startedreceived from Client:this is a websocket test ...--- -Information received from the client

At this point, the WebSocket environment is completed.


2: Configure Nginx, Agent 110 on the WebSocket link.

[Email protected]_168_1_131 ~]# vim/opt/conf/nginx/nginx.conf......http {... map $http _upgrade $connection _ Upgrade {default upgrade; ' Close;}  .... include vhost/*.conf; [Email protected]_168_1_131 ~]# vim/opt/conf/nginx/vhost/nodejs.confupstream nodejs{server 192.168.1.110:8010;} server {Listen 8010;access_log/opt/logs/nginx/nodejs.log main;location/{proxy_pass http://nodejs;proxy_http_ Version 1.1;proxy_set_header Upgrade $http _upgrade;proxy_set_header Connection $connection _upgrade;}

Reload Nginx configuration:

[Email protected]_168_1_131 ~]# service Nginx Reload

After this configuration, the request to access Port 8010 on the Nginx server will be sent to the 8081 port on the back-end 192.168.1.110.


3: Verify WebSocket Request Agent: (Point WS address to 192.168.1.131:8010)

[Email protected]_168_1_110 ~]#/root/node-v6.2.0-linux-x64/lib/node_modules/wscat/bin/wscat--connect ws:// 192.168.1.131:8010connected (Press CTRL + C to quit) > WebSocket test through nginx proxying...< Server recei Ved from Client:this is a websocket test through Nginx proxying...>

View Service side:

[Email protected]_168_1_110 ~] #node server.jsserver startedreceived from Client:this is a websocket test ... Received from Client:this is a websocket test through Nginx proxying ...

You can see through the proxy, WebSocket also can receive the client's request information normally.

This article from the "Play God Clown" blog, reproduced please contact the author!

Nginx Proxy websocket Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.