Nginx支持websocket的配置详解

发布时间 2023-08-17 09:19:20作者: 甜菜波波

 

一、对wss与nginx代理wss的理解:

1、wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https(http+SSL)。

2、利用nginx代理wss【通讯原理及流程】

  • 客户端发起wss连接连到nginx
  • nginx将wss协议的数据转换成ws协议数据并转发到Workerman的websocket协议端口
  • Workerman收到数据后做业务逻辑处理
  • Workerman给客户端发送消息时,则是相反的过程,数据经过nginx/转换成wss协议然后发给客户端

二、Nginx 支持websocket的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
      listen   80;
      server_name 域名;
      location / {
        proxy_pass   http://127.0.0.1:8080/; // 代理转发地址
     proxy_http_version 1.1;
        proxy_read_timeout   3600s; // 超时设置
        // 启用支持websocket连接
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
      }
      location /upload { // 静态资源地址
            root   /mnt/resources;       
      }
}
 

重要的是这两行,它表明是websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接。

1
2
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
 

proxy_read_timeout; 表明连接成功以后等待服务器响应的时候,如果不配置默认为60s;

proxy_http_version 1.1; 表明使用http版本为1.1  

到此这篇关于Nginx支持websocket的配置的文章就介绍到这了,更多相关Nginx websocket配置内容请搜索站圈网以前的文章或继续浏览下面的相关文章希望大家以后多多支持站圈网!

转自:http://www.zhano.cn/Linux/68736.html