HTTP 的進化 - 理解真實的 HTTP 是如何工做的html
原文: https://medium.com/platform-engineer/evolution-of-http-69cfe6531ba0web
自從在 1989-1991 年間被 CERN(譯註:即「歐洲核子研究組織」的原稱 - Conseil Européen pour la Recherche Nucléaire)的 Tim Berners-Lee 發明出來之後,HTTP(超文本傳輸協議) 就一直是萬維網的基礎傳輸協議。在 C/S 計算模型中,HTTP 起到了一個「請求/響應」協議的做用。HTTP 標準由 IETF(因特網工程任務組) 和 W3C(萬維網聯盟)負責開發,並以一系列註釋請求(RFCs - Requests for Comments)的形式被髮布。HTTP 有 4 個版本:HTTP/0.九、HTTP/1.0、HTTP/1.1 以及 HTTP/2.0。當前一般使用 HTTP/1.1 版本,將來會是 HTTP/2.0。瀏覽器
GET
$> telnet ashenlive.com 80
(鏈接1創建 - TCP 三次握手)
Connected to xxx.xxx.xxx.xxx
(請求)
GET /my-page.html
(超文本響應)
<HTML>
A very simple HTML page
</HTML>
(鏈接1關閉 - 斷掉 TCP)
複製代碼
流行的 web 服務器(Apache, Nginx)始終支持 HTTP/0.9。能夠打開一個 Telnet 會話訪問 google.com 試試。緩存
GET
, HEAD
, POST
(鏈接1創建 - TCP 三次握手)
Connected to xxx.xxx.xxx.xxx
(請求)
GET /my-page.html HTTP/1.0
User-Agent: NCSA_Mosaic/2.0 (Windows 3.1)
(響應)
HTTP/1.0 200 OK
Content-Type: text/html
Content-Length: 137582
Expires: Thu, 01 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 1 May 1996 12:45:26 GMT
Server: Apache 0.84
<HTML>
A page with an image
<IMG SRC="/myimage.gif">
</HTML>
(鏈接1關閉 - 斷掉 TCP)
------------------------------------------
(鏈接2創建 - TCP 三次握手)
Connected to xxx.xxx.xxx.xxx
(請求)
GET /myimage.gif HTTP/1.0
User-Agent: NCSA_Mosaic/2.0 (Windows 3.1)
(響應)
HTTP/1.0 200 OK
Content-Type: text/gif
Content-Length: 137582
Expires: Thu, 01 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 1 May 1996 12:45:26 GMT
Server: Apache 0.84
[image content]
(鏈接2關閉 - 斷掉 TCP)
複製代碼
HTTP/0.9 和 HTTP/1.0 都須要爲每次請求創建一個新的鏈接(並在收到對應的響應後當即關閉該鏈接)。每次新鏈接創建時,都要經歷一遍 TCP 三次握手。考慮到更好的性能,應該着重減小這些 C/S 之間的往返通訊。HTTP/1.1 用持久化鏈接解決了這個問題。安全
GET
, HEAD
, POST
, PUT
, DELETE
, TRACE
, OPTIONS
(鏈接1創建 - TCP 三次握手)
Connected to xxx.xxx.xxx.xxx
(請求1)
GET /en-US/docs/Glossary/Simple_header HTTP/1.1
Host: developer.mozilla.org
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://developer.mozilla.org/en-US/docs/Glossary/Simple_header
(響應1)
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Wed, 20 Jul 2016 10:55:30 GMT
Etag: "547fa7e369ef56031dd3bff2ace9fc0832eb251a"
Keep-Alive: timeout=5, max=1000
Last-Modified: Tue, 19 Jul 2016 00:59:33 GMT
Server: Apache
Transfer-Encoding: chunked
Vary: Cookie, Accept-Encoding
[content]
(請求2)
GET /static/img/header-background.png HTTP/1.1
Host: developer.cdn.mozilla.net
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://developer.mozilla.org/en-US/docs/Glossary/Simple_header
(響應2)
HTTP/1.1 200 OK
Age: 9578461
Cache-Control: public, max-age=315360000
Connection: keep-alive
Content-Length: 3077
Content-Type: image/png
Date: Thu, 31 Mar 2016 13:34:46 GMT
Last-Modified: Wed, 21 Oct 2015 18:27:50 GMT
Server: Apache
[image content of 3077 bytes]
(鏈接1關閉 - 斷掉 TCP)
複製代碼
Connection: Keep-Alive
頭部在 HTTP/1.1 以前就存在,但在 HTTP/1.1 中該頭部被廢棄了,由於持久化鏈接變成了默認的行爲(譯註:除非用Connection: Close
顯式關閉)。Keep-Alive
頭部能夠被用於在主機之間定義持久鏈接通訊策略(好比使一個鏈接在某個事件發生以前都保持激活)。這奠基了持久化、鏈接複用、管道化及其餘不少現代 web 通訊協議中被增強能力的基礎。Keep-Alive
頭部。同時,主機能夠增長 timeout
參數以設置一個超時,或用 max
參數限制每一個鏈接的最大請求數。HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Aug 2016 15:23:13 GMT
Keep-Alive: timeout=5, max=1000
Last-Modified: Mon, 25 Jul 2016 04:32:39 GMT
Server: Apache
[body]
複製代碼
Keep-Alive
頭部,HTTP 管道化實現了多個鏈接,以及其餘更多改進。Upgrade
頭部,即可用一個經常使用的協議起步,好比 HTTP/1.1,而後讓該鏈接切換到加強的協議類型,如 HTTP/2.0 或 WebSockets。max
參數了。升級後的協議爲 timeout
參數提供了新的策略(若是沒有特別定義,使用基礎協議默認的 timeout 值)。全部以上說起的特性,當今都已經被主要的服務器和瀏覽器所使用了。但像 HTTP/2.0、Server Side Events (SSE) 以及 WebSocket 這樣的現代加強功能,則改變了傳統 HTTP 的工做方式。性能優化
長按二維碼或搜索 fewelife 關注咱們哦bash