1. 程式人生 > >springboot + websocket + linux伺服器(nginx)404問題解決

springboot + websocket + linux伺服器(nginx)404問題解決

問題描述:

最近基於springboot做一個微信公眾號,內嵌一個小的購物商城系統,使用者下單後,使用websocket往商家管理系統傳送訊息。在本地測試沒有任何問題,但是部署到centos伺服器上之後一直報錯404。總結了網上很多解決方法都不行,網上討論的都是說tomcat版本太低,因為websocket需要tomcat7.0以上才支援。但是我是用springboot+maven搭建的專案,使用的是springboot內嵌的tomcat伺服器,我查看了maven,發現tomcat版本是8.0+。那麼就不是tomcat版本問題了。

解決思路:

排除了tomcat問題,jdk版本也是1.8+,websocket部署到伺服器上還是404,網上還有人說是tomcat的jar包和專案jar衝突,可是我springboot專案使用的內嵌tomcat,於是我利用Maven Helper(非常好用的idea外掛)查看了下依賴發現並沒有衝突。卡在這裡很久,我特地看了下websocket請求格式:
Websocket握手格式:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com

這請求類似http協議,裡面多了陌生的內容是:

Upgrade: websocket
Connection: Upgrade

當時一臉懵逼,這是什麼玩意啊?然後我就百度了下,原來這個就是Websocket的核心,他會告訴Apache、Nginx等伺服器我發起的是websocket請求,不是http!下面的三個引數Sec-WebSocket-Key、Sec-WebSocket-Protocol、Sec-WebSocket-Version作用大概就是驗證請求確實是websocket,同時指定協議版本吧。
這時候我恍然大悟!我的專案使用了nginx做了轉發,那麼會不會是因為我沒有配置nginx響應websocket請求呢?

答案是肯定的!

配置nginx反向代理響應webSocket請求

需要在代理的請求配置中加入下面的配置:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

nginx配置如下:

server {
 listen       80;
 server_name  localhost;
 #charset koi8-r;

 location / {
     root   /usr/java/myproject/sell;
     index  index.html index.htm;
 }

 location /sell/ {
     proxy_pass http://127.0.0.1:8081/sell/;
} location /sell/webSocket { proxy_pass http://127.0.0.1:8081/sell/webSocket; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }

這段配置就是把所有的/sell下的請求轉到8081埠處理,把/sell/webSocket請求轉發到指定的請求介面,發現404錯誤消失,webSocket服務也可以訪問到。但是出現了一個新的問題就是,webSocket連線後沒有保持連線,大約一分鐘之後就會斷開,原因是因為在第一次請求後到第二次請求到來的時間超過了預設的最大時間,超時了。這裡提供一個解決思路:
需要配置的三個核心引數:

proxy_connect_timeout 4s; 
proxy_read_timeout 7200s; 
proxy_send_timeout 12s; 

proxy_read_timeout是伺服器對連線等待的最大時間,也就是說,當你webSocket使用nginx轉發的時候,用上面的配置來說,如果72000秒內沒有通訊,依然是會斷開的,你可以按照需求來設定。

比如說,我這裡設定了7200s(2小時),那麼如果我2小時內有通訊,或者2小時內有心跳的話,是可以保持連線不中斷的。

我這裡之所以這麼設定,是考慮到了具體的業務情況,2小時比較合適。最終的配置如下:

server {
 listen       80;
 server_name  localhost;
 #charset koi8-r;

 location / {
     root   /usr/java/myproject/sell;
     index  index.html index.htm;
 }

 location /sell/ {
     proxy_pass http://127.0.0.1:8081/sell/;
 }

location /sell/webSocket {
    proxy_pass http://127.0.0.1:8081/sell/webSocket; 
    proxy_connect_timeout 4s; 
    proxy_read_timeout 7200s; 
    proxy_send_timeout 12s; 
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

問題解決,坑爹啊!