> 文章列表 > 解决使用nginx 负载 ws的问题

解决使用nginx 负载 ws的问题

解决使用nginx 负载 ws的问题

背景

前端框架使用的是vite,代理的配置中开启了ws: true 以支持ws的负载
打包完毕后使用nginx 发现无法负载 ws 请求

解决

修改配置文件, 首先添加两个变量, 添加到http下面, 和server 同一级

	map $http_upgrade $connection_upgrade {default       keep-alive;   #默认为keep-alive 可以支持一般http请求'websocket'   upgrade;      #如果为websocket 则n为upgrade可升级的。}

如下所示
解决使用nginx 负载 ws的问题
然后修改 location 添加如下配置, 引用上面的变量

			proxy_set_header   Upgrade  $http_upgrade; #此处配置 上面定义的变量proxy_set_header   Connection $connection_upgrade;

结果如下:
解决使用nginx 负载 ws的问题
重启nginx 即可解决