nginx代理websocket配置

简介:

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

    配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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模块。(这里通过官网程序包实现)

1
2
3
4
5
[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服务监听程序:

1
2
3
4
5
6
7
8
9
10
11
[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服务端:

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

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

1
2
3
4
5
[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 ...
>

      查看服务端:

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

     至此,websocket环境搭建完成。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[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配置:

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
2
3
4
5
[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...
>

     查看服务端:

1
2
3
4
[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也能正常接收到客户端的请求信息。










本文转自 icenycmh 51CTO博客,原文链接:http://blog.51cto.com/icenycmh/1839566,如需转载请自行联系原作者
目录
相关文章
|
19天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
24 0
|
18天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
78 0
|
21小时前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
1天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
4 0
|
11天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
18 0
|
13天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
25 0
|
21天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0
|
22天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
18 0
|
22天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
27 3
|
23天前
|
前端开发 应用服务中间件 网络安全
http转为https,ssl证书安装及nginx配置
http转为https,ssl证书安装及nginx配置
36 1