nginx代理websocket配置

╰半夏微凉° 2022-03-09 02:51 1013阅读 0赞

思梦PHP 2019-02-26 00:01:00

nginx正常只能代理http请求,如果想实现代理websocket的需求,需在请求中加入”**Upgrade**”字段,使请求从http升级为websocket。

配置如下:

  1. http {
  2. map $http_upgrade $connection_upgrade { default upgrade;
  3. '' close;
  4. }
  5. server {
  6. ...
  7. location /chat/ {
  8. proxy_pass http://backend;
  9. proxy_http_version 1.1;
  10. #以下配置添加代理头部: proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade;
  11. }
  12. }

实验案例:

配置websockeet服务器,客户端通过代理连接服务器端。环境如下:

websocket服务端:192.168.1.110:8010

nginx :192.168.1.131:8081

1:利用nodejs实现一个websocket服务端,监听8010端口,等待websocket链接。

安装nodejs与npm,并安装ws模块。(这里通过官网程序包实现)

  1. [root@192_168_1_110 ~]# tar -zxvf node-v6.2.0-linux-x64.tar.gz
  2. [root@192_168_1_110 ~]# ln -s /root/node-v6.2.0-linux-x64/bin/node /usr/local/bin/node
  3. [root@192_168_1_110 ~]# ln -s /root/node-v6.2.0-linux-x64/bin/node /usr/local/bin/node
  4. [root@192_168_1_110 ~]# npm install -g ws
  5. [root@192_168_1_110 ~]# npm install -g wscat

编写websocket服务监听程序:

  1. [root@192_168_1_110 ~]# vim server.js
  2. console.log("Server started");
  3. var Msg = '';
  4. var WebSocketServer = require('ws').Server
  5. , wss = new WebSocketServer({port: 8010});
  6. wss.on('connection', function(ws) {
  7. ws.on('message', function(message) {
  8. console.log('Received from client: %s', message);
  9. ws.send('Server received from client: ' + message);
  10. });
  11. });

启动websocket服务端:

  1. [root@192_168_1_110 ~]# node server.js
  2. Server started

重新开启一个终端,使用wscat程序测试程序连接:

  1. [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
  2. connected (press CTRL+C to quit)
  3. > This is a websocket test...
  4. < Server received from client: This is a websocket test...
  5. >

查看服务端:

  1. [root@192_168_1_110 ~]#node server.js
  2. Server started
  3. Received from client: This is a websocket test... ----从客户端接收到的信息

至此,websocket环境搭建完成。

2:配置nginx,代理110上的websocket链接。

  1. [root@192_168_1_131 ~]# vim /opt/conf/nginx/nginx.conf
  2. ......
  3. http {
  4. ......
  5. map $http_upgrade $connection_upgrade {
  6. default upgrade;
  7. '' close;
  8. }
  9. .......
  10. include vhost/*.conf;
  11. [root@192_168_1_131 ~]# vim /opt/conf/nginx/vhost/nodejs.conf
  12. upstream nodejs{
  13. server 192.168.1.110:8010;
  14. }
  15. server {
  16. listen 8010;
  17. access_log /opt/logs/nginx/nodejs.log main;
  18. location / {
  19. proxy_pass http://nodejs;
  20. proxy_http_version 1.1;
  21. proxy_set_header Upgrade $http_upgrade;
  22. proxy_set_header Connection $connection_upgrade;
  23. }
  24. }

重新加载nginx配置:

  1. [root@192_168_1_131 ~]# service nginx reload

这样配置过后,在nginx服务器上访问8010端口的请求,将会全部发送到后端192.168.1.110的8081端口上。

3:验证websocket请求代理:(把ws地址指向192.168.1.131:8010)

  1. [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
  2. connected (press CTRL+C to quit)
  3. > This is a websocket test through nginx proxying...
  4. < Server received from client: This is a websocket test through nginx proxying...
  5. >

查看服务端:

  1. [root@192_168_1_110 ~]#node server.js
  2. Server started
  3. Received from client: This is a websocket test...
  4. Received from client: This is a websocket test through nginx proxying...

可以看到通过代理,websocket也能正常接收到客户端的请求信息。

本文转载地址:http://blog.51cto.com/icenycmh/1839566

作者icenycmh的原创作品

发表评论

表情:
评论列表 (有 0 条评论,1013人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx反向代理WebSocket

            WebSocket协议相比较于HTTP协议成功握手后可以多次进行通讯,直到连接被关闭。但是WebSocket中的握手和HTTP中的握手兼容, 它使用HTTP中