当前位置: 移动技术网 > 网络运营>服务器>nginx > 详解nginx websocket配置

详解nginx websocket配置

2019年04月17日  | 移动技术网网络运营  | 我要评论

一·什么是websocket

websocket协议相比较于http协议成功握手后可以多次进行通讯,直到连接被关闭。但是websocket中的握手和http中的握手兼容,它使用http中的upgrade协议头将连接从http升级到websocket。这使得websocket程序可以更容易的使用现已存在的基础设施。

websocket工作在http的80和443端口并使用前缀ws://或者wss://进行协议标注,在建立连接时使用http/1.1的101状态码进行协议切换,当前标准不支持两个客户端之间不借助http直接建立websocket连接。

二.创建基于node的websocket服务

安装node.js和npm

$ yum install nodejs npm

安装ws和wscat模块

ws是nodejs的websocket实现,我们借助它来搭建简单的websocket echo server。

wscat是一个可执行的websocket客户端,用来调试websocket服务是否正常。

npm install ws wscat

创建一个简单的服务端

$ 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);
  });
 });

运行服务端

$ node server.js
 server started

验证服务端是否正常启动

$ netstat -tlunp|grep 8010
tcp6    0   0 :::8010         :::*          listen   23864/nodejs

使用wscat做为客户端测试

wscat命令默认安装当前用户目录node_modules/wscat/目录,我这里的位置是/root/node_modules/wscat/bin/wscat

输入任意内容进行测试,得到相同返回则说明运行正常。

$ 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

三.使用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: websocket
connection: upgrade

这两个字段表示请求服务器升级协议为websocket。服务器处理完请求后,响应如下报文# 状态码为101 

http/1.1 101 switching protocols
upgrade: websocket
connection: 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.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

测试成功,ok

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网