關于Nginx+websocket 400問題

說明

用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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 第一章 Nginx簡介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 32,803評論 24 1,002
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 之前在網上搜索到的很多文章在描述 Nginx + Tomcat 啟用 HTTPS 支持的時候,都必須在 Nginx...
    Lynner閱讀 3,149評論 1 5
  • Welcome to websocket-sharp! websocket-sharp supports: RFC...
    mtian999閱讀 10,005評論 0 1
  • Toolbar的初始化 在toolbar 的初始化工程中,我們通常會使用一下方式: 而對于標題的初始化: tool...
    Liuuuuuuzi閱讀 984評論 0 1