In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to configure websocket in nginx, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What is websocket?
The WebSocket protocol can communicate many times after a successful handshake compared to the HTTP protocol until the connection is closed. But the handshake in WebSocket is compatible with the handshake in HTTP, which uses the Upgrade protocol header in HTTP to upgrade the connection 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 uses the prefix ws:// or wss:// to mark the protocol. When establishing the connection, it uses the 101status code of HTTP/1.1 to switch the protocol. The current standard does not support the direct establishment of Websocket connection between two clients without the help of HTTP.
two。 Create a Node-based WebSocket service
Install node.js and npm
$yum install nodejs npm
Install ws and wscat modul
Ws is the WebSocket implementation of nodejs, and we use it to build a simple WebSocket Echo Server.
Wscat is an executable WebSocket client used to debug whether the WebSocket service is working.
Npm install ws wscat
Create a simple server
$vim server.jsconsole.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:% slots, message); ws.send ('Server received from client:' + message);});})
Running server
$node server.js Server started
Verify that the server starts normally
$netstat-tlunp | grep 8010tcp6 0 0: 8010:: * LISTEN 23864/nodejs
Use wscat as a client test
The wscat command installs the current user directory node_modules/wscat/ directory by default, and my location here is / root/node_modules/wscat/bin/wscat
Enter anything to test, and the same return indicates that it is working properly.
$cd / root/node_modules/wscat/bin/$. / wscat-- connect ws://127.0.0.1:8010connected (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 三.使用Nginx对WebSocket进行反向代理 安装Nginx yum -y install nginx 配置Nginx Websocket $ vim /usr/local/nginx/conf/nginx.conf# 在http上下文中增加如下配置,确保Nginx能处理正常http请求。http{ map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream websocket { #ip_hash; server localhost:8010; server localhost:8011; }# 以下配置是在server上下文中添加,location指用于websocket连接的path。 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;}}} 最重要的就是在反向代理的配置中增加了如下两行,其它的部分和普通的HTTP反向代理没有任何差别。 proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade; 这里面的关键部分在于HTTP的请求中多了如下头部: Upgrade: websocketConnection: Upgrade 这两个字段表示请求服务器升级协议为WebSocket。服务器处理完请求后,响应如下报文# 状态码为101 HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: upgrade 告诉客户端已成功切换协议,升级为Websocket协议。握手成功之后,服务器端和客户端便角色对等,就像普通的Socket一样,能够双向通信。不再进行HTTP的交互,而是开始WebSocket的数据帧协议实现数据交换。 这里使用map指令可以将变量组合成为新的变量,会根据客户端传来的连接中是否带有Upgrade头来决定是否给源站传递Connection头,这样做的方法比直接全部传递upgrade更加优雅。 默认情况下,连接将会在无数据传输60秒后关闭,proxy_read_timeout参数可以延长这个时间或者源站通过定期发送ping帧以保持连接并确认连接是否还在使用。 启动nginx /etc/init.d/nginx start 试通过Nginx访问WebSocket服务 $ cd /root/node_modules/wscat/bin/$ ./wscat --connect ws://192.168.2.210connected (press CTRL+C to quit)>Hello Nginx
< Server received from client: Hello Nginx>Welcome to www.hi-linux.com < Server received from client: the above is how to configure websocket in nginx. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.