第一步:配置 nginx 配置文件,支持转发 websocket 请求

主要在 location 配置端增加以下配置项

     proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";

完整 location 配置如下:

http://api-gateway:9090,为网关服务地址

location /data-development/ {
         proxy_pass http://api-gateway:9090;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
      }

第二步:api-gateway 网关增加转发 websocket 请求

(其中一段配置转发 wobsocket 请求,另外一段配置转发 http 请求)

在route路由属性配置下,增加以下配置
- id: data-development-web-websocket #  data-development-websocket服务路由
  uri: lb:ws://data-development-web
  order: 1
  predicates:
    - Path=/data-development/ws/**
- id: data-development-web #  data-development服务路由
  uri: lb://data-development-web # websocket所在的服务
  order: 2
  predicates:  
    - Path=/data-development/**

然后重启网关,再重启 nginx 即可。

解释:
第一步的配置是解决 nginx 转发 websocket 请求
第二步的配置是解决 springcloud-gateway 转发 websocket 请求


作者:成伟平cwp

来源链接:

https://blog.csdn.net/pingweicheng/article/details/114947578

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐