Java SpringBoot websocket的NGINX代理返回400,原因是;升级标题:Null";,为什么?

Java SpringBoot websocket的NGINX代理返回400,原因是;升级标题:Null";,为什么?,java,spring-boot,nginx,websocket,Java,Spring Boot,Nginx,Websocket,我正在运行默认的SpringBoot嵌入式Tomcat应用服务器。我从这个服务器提供RESTAPI和websocket端点 在此之前,将设置具有以下配置的NGINX代理: include /usr/local/etc/nginx/sites-enabled/*; http { map $http_upgrade $connection_upgrade { default Upgrade; '' close; } server { listen 80 d

我正在运行默认的SpringBoot嵌入式Tomcat应用服务器。我从这个服务器提供RESTAPI和websocket端点

在此之前,将设置具有以下配置的NGINX代理:

include /usr/local/etc/nginx/sites-enabled/*;

http {
 map $http_upgrade $connection_upgrade {
  default Upgrade;
  ''      close;
 }

 server {
  listen       80 default_server;
  server_name  SERVER_NAME;
  root /var/www;



  underscores_in_headers on;


  location / {
   index index.html;
   try_files $uri /index.html;

   auth_basic "Restricted Content";
   auth_basic_user_file /etc/nginx/.htpasswd;
  }

  location /api/ {
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header Host $http_host;
   proxy_http_version 1.1;
   proxy_pass http://SERVER_URL:9090/;
  }



  location /websocket {
   proxy_pass http://SERVER_URL:9090/websocket;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection $connection_upgrade;
  }



}
}
events{}
  • NGINX服务器从/位置为web应用程序提供服务
  • 它从/API/位置代理REST API
  • 它通过/websocket位置代理Spring boot websocket
Web应用程序和restapi工作正常。 但是websocket连接不起作用。当我尝试连接到套接字端点时,tomcat应用程序服务器上会抛出以下错误

 2018-07-17 11:53:05.516 ERROR 5 --- [nio-9090-exec-1] o.s.w.s.s.s.DefaultHandshakeHandler      : Handshake failed due to invalid Upgrade header: null
因此,从HTTP升级到WS-protocoll的升级头, 未传递到应用程序服务器。应通过以下方式解决这一问题:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
但它不起作用。如果我试图绕过NGINX直接连接到websocket地址,就会成功建立连接。 我的问题是:

  • 我的NGINX配置中是否缺少某些内容
  • 有人能发现另一个可能导致上述行为的错误吗

请随时询问有关系统的更多信息。

您确定配置的语法吗?在我找到的文档中:proxy\u set\u头连接“升级”;对不起,我找到了另一个。。。现在我很困惑:)是的,我也直接用“升级”字符串尝试过。它们在这里显示的方式,使用地图升级,应该不会有什么不同,我在文档中都看到了;代理设置头连接“升级”你好,何贵松,我添加了你的建议。现在400错误消失了。但是连接在使用502坏网关很长一段时间后超时,从应用程序服务器日志中可以看出,请求从未到达那里。