第一步:配置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请求