說明
用beego + nginx 在ubuntu上搭建了一個聊天服務器,使用websocket進行聊天交互, 測試沒問題,但是放到nginx上之后,訪問網站 發現不能進行websocket操作,提示400的問題
解決方法
在nginx配置文件 加上這幾句話
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
...
location /chat/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
原因
since the “Upgrade” is a hop-by-hop header, it is not passed from a client to proxied server. 鏈接地址。
另附 常用的HTTP請求頭與響應頭
注意:
設置完后 默認有一個連接超時時間60s,相當于我的聊天程序 如果某個人在60s秒內 沒有說話 websocket 連接 就會自動斷開!
因此 在nginx上需要配置[proxy_read_timeout]
proxy_read_timeout 360s; //6分鐘時間
最后reload nginx