网站首页 > 技术教程 正文
nginx正常只能代理http请求,如果想实现代理websocket的需求,需在请求中加入"Upgrade"字段,使请求从http升级为websocket。
配置如下:
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; } }
实验案例:
配置websockeet服务器,客户端通过代理连接服务器端。环境如下:
websocket服务端:192.168.1.110:8010
nginx :192.168.1.131:8081
1:利用nodejs实现一个websocket服务端,监听8010端口,等待websocket链接。
安装nodejs与npm,并安装ws模块。(这里通过官网程序包实现)
[root@192_168_1_110 ~]# tar -zxvf node-v6.2.0-linux-x64.tar.gz [root@192_168_1_110 ~]# ln -s /root/node-v6.2.0-linux-x64/bin/node /usr/local/bin/node [root@192_168_1_110 ~]# ln -s /root/node-v6.2.0-linux-x64/bin/node /usr/local/bin/node [root@192_168_1_110 ~]# npm install -g ws [root@192_168_1_110 ~]# npm install -g wscat
编写websocket服务监听程序:
[root@192_168_1_110 ~]# 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); }); });
启动websocket服务端:
[root@192_168_1_110 ~]# node server.js Server started
重新开启一个终端,使用wscat程序测试程序连接:
[root@192_168_1_110 ~]# /root/node-v6.2.0-linux-x64/lib/node_modules/wscat/bin/wscat --connect ws://192.168.1.110:8010 connected (press CTRL+C to quit) > This is a websocket test... < Server received from client: This is a websocket test... >
查看服务端:
[root@192_168_1_110 ~]#node server.js Server started Received from client: This is a websocket test... ----从客户端接收到的信息
至此,websocket环境搭建完成。
2:配置nginx,代理110上的websocket链接。
[root@192_168_1_131 ~]# vim /opt/conf/nginx/nginx.conf ...... http { ...... map $http_upgrade $connection_upgrade { default upgrade; '' close; } ....... include vhost/*.conf; [root@192_168_1_131 ~]# vim /opt/conf/nginx/vhost/nodejs.conf upstream 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; } }
重新加载nginx配置:
[root@192_168_1_131 ~]# service nginx reload
这样配置过后,在nginx服务器上访问8010端口的请求,将会全部发送到后端192.168.1.110的8081端口上。
3:验证websocket请求代理:(把ws地址指向192.168.1.131:8010)
[root@192_168_1_110 ~]# /root/node-v6.2.0-linux-x64/lib/node_modules/wscat/bin/wscat --connect ws://192.168.1.131:8010 connected (press CTRL+C to quit) > This is a websocket test through nginx proxying... < Server received from client: This is a websocket test through nginx proxying... >
查看服务端:
[root@192_168_1_110 ~]#node server.js Server started Received from client: This is a websocket test... Received from client: This is a websocket test through nginx proxying...
可以看到通过代理,websocket也能正常接收到客户端的请求信息。
本文转载地址:http://blog.51cto.com/icenycmh/1839566
作者icenycmh的原创作品
猜你喜欢
- 2024-10-10 可视化代理神器,动动手指轻松配置Nginx
- 2024-10-10 Nginx反向代理及参数配置(nginx反向代理原理及应用)
- 2024-10-10 经验分享:Nginx学习之反向代理WebSocket配置实例
- 2024-10-10 Nginx总结(五)如何配置nginx和tomcat实现反向代理
- 2024-10-10 Nginx代理WebSockets配置详解(nginx socks代理)
- 2024-09-11 写给前端同学的Nginx配置指南(前端 nginx)
- 2024-09-11 「Nginx」-Nginx主要配置速览(nginx常用配置)
- 2024-09-11 玩转Nginx你要知道这些配置(收藏篇)
- 2024-09-11 nginx配置详解(nginx基本配置)
- 2024-09-11 Nginx-常用配置详解(nginx简单配置)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)