015_NGINX作為WebSocket Proxy的設定

來源:互聯網
上載者:User

標籤:png   develop   pass   請求   通過   upgrade   proxy   tin   end   

產研那邊有通過nginx代理進行長串連的需求,咱們都知道預設nginx只支援短串連的,使用長串連需要單獨配置

一、

       websocket協議提供建立一種支援在server和client之前雙向通訊的web應用。作為HTML5的一部分,websock使比它之前可用的方式中提供一個種更加簡單的方法。大部分現在的瀏覽器都支援websock,包括chrome,Firefox,Internet瀏覽器,Opera,和Safari,和越來越多的伺服器應用程式框架也開始支援websock了。

       The WebSocket protocol is different from the HTTP protocol,但是websocket握手是相容http的,用TTP Upgrade方式更新從HTTP到websock的串連。This allows WebSocket applications to more easily fit into existing infrastructures.例如,websock應用能使用標準的http連接埠80和443進行通訊。

       A WebSocket application keeps a long?running connection open between the client and the server, facilitating the development of real?time applications. 這個 HTTP Upgrade 機制通過使用" the Upgrade and Connection headers." 來更新從HTTP到websocket的串連。There are some challenges that a reverse proxy server faces in supporting WebSocket. 一個是websocket是一個中hop-by-hop(逐跳)協議, so when a proxy server intercepts an Upgrade request from a client it needs to send its own Upgrade request to the backend server, including the appropriate headers. 還有,websocket串連是長串連,相反使用http時典型的短串連,即反向 Proxyserver需要在它們在閒置時候需要保持這些串連保持open,而不是關閉它們。

       NGINX supports WebSocket by allowing a tunnel to be set up between a client and a backend server. 對於nginx發送upgrade請求從client到後端的server, the Upgrade and Connectionheaders must be set explicitly, as in this example:

location /wsapp/ {    proxy_pass http://wsbackend;    proxy_http_version 1.1;    proxy_set_header Upgrade $http_upgrade;    proxy_set_header Connection "upgrade";}

        Once this is done, NGINX deals with this as a WebSocket connection.

 二、舉個栗子。

參考:https://www.nginx.com/blog/websocket-nginx/

 

015_NGINX作為WebSocket Proxy的設定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.