標籤: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 Connection
headers 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的設定