springclould nginx转发 websocket400报错问题
之前一直找原因一直围绕着nginx转发的问题
说头信息没设置全
然后nginx配置文件上加了这些
#http块加以下几行: map $http_upgrade $connection_upgrade { default upgrade; '' close; }
#sever块加以下几行:
location / {
proxy_pass http://localhost:8001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
配置完发现还是没有效果,
最后发现自己项目用的是微服务spingCloud
还得在ZUUL网关配置一个websocket过滤器
package com.power.bos.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class WebSocketFilter extends ZuulFilter { @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 4; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext context = RequestContext.getCurrentContext(); HttpServletRequest request = context.getRequest(); String upgradeHeader = request.getHeader("Upgrade"); context.getResponse().setHeader("X-Frame-Options", "SAMEORIGIN"); if (null == upgradeHeader) { upgradeHeader = request.getHeader("upgrade"); } if (null != upgradeHeader && "websocket".equalsIgnoreCase(upgradeHeader)) { context.addZuulRequestHeader("connection", "Upgrade"); // context.addZuulRequestHeader("X-Frame-Options", "SAMEORIGIN"); } return null; } }
就解决了上图的两个问题了
红色那块是针对第二个报错的
最后解决
2018.7.9
后来发现这种方式还是不好,连接有延迟,要等个几秒才连接成功,这样严重影响用户体验,后来还是弃用了