配置nginx+tomcat支持websocket


問題情景:

     最近開發新增加一個項目,需要支持https wss協議

   訪問https://test.aa.com  使用nginx反向代理到后端tomcat web應用

   訪問https://test.aa.com/AAASService/websocket  反向代理到后端tomcat的websocket

    一開始以為都是默認支持wss,就按照配置https協議配置了,后來發現行不通,在后端查看tomcat日志如下:

  org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/AAASService/websocket/123456] in DispatcherServlet with name 'dispatcherServlet'

 

   查看nginx日志訪問狀態返回404

 

網上搜索了一圈終於找到原因:

  1.websocket需要tomcat7.0以上才支持

  2.nginx需要配置相應的參數

解決:

  nginx配置:

  

location  / {

        access_log  /usr/local/nginx/log/qrcode.access.log main; error_log /usr/local/nginx/log/qrcode.error.log warn; proxy_pass http://10.20.168.81:9090;  }

location /QRMSService/websocket { access_log /usr/local/nginx/log/qrcode.access.log main; error_log /usr/local/nginx/log/qrcode.error.log warn; proxy_pass http://10.20.168.81:9090/QRMSService/websocket; proxy_set_header Upgrade $http_upgrade; #需要添加這兩行配置,讓nginx響應websocket請求 proxy_set_header Connection "upgrade";

     proxy_connect_timeout 4s; #為了防止socket鏈接很快就斷掉需要按照各自的需求配置如下三個參數
     proxy_read_timeout 7200s;
     proxy_send_timeout 12s;

}

 

重新加載nginx  測試訪問正常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM