ÆÊÎöNginxµÄHTTP³¤ÅþÁ¬ºÍHTTP/2¶à·¸´ÓõĵײãʵÏÖÔÀí¼°ÓÅÊÆ
ÎÊÌ⣺nginxµÄhttp³¤ÅþÁ¬ºÍhttp/2¶à·¸´ÓõĵײãʵÏÖÔÀí¼°ÓÅÊÆ
ͻ񻣼
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆäÔÚÍøÂç´«ÊäÀú³ÌÖнÓÄÉÁËHTTP³¤ÅþÁ¬ºÍHTTP/2¶à·¸´ÓõÄÊÖÒÕ£¬ÒÔÌáÉýÐÔÄܺÍЧÂÊ¡£±¾ÎĽ«¶ÔNginxµÄHTTP³¤ÅþÁ¬ºÍHTTP/2¶à·¸´ÓõĵײãʵÏÖÔÀí¾ÙÐÐÆÊÎö£¬²¢Õ¹Ê¾Ïà¹ØµÄ´úÂëʾÀý¡£
Ò»¡¢HTTP³¤ÅþÁ¬µÄµ×²ãʵÏÖÔÀí
1.1 ʲôÊÇHTTP³¤ÅþÁ¬
HTTP³¤ÅþÁ¬ÊÇÖ¸¿Í»§¶ËÓëЧÀÍÆ÷Ö®¼ä½¨ÉèÒ»Ìõ³¤ÆÚµÄTCPÅþÁ¬£¬ÔÚÒ»¸öTCPÅþÁ¬ÖпÉÒÔ´«Êä¶à¸öHTTPÇëÇóºÍÏìÓ¦£¬´Ó¶ø×èÖ¹ÁËƵÈÔ½¨ÉèºÍ¹Ø±ÕÅþÁ¬µÄ¿ªÏú¡£
1.2 HTTP³¤ÅþÁ¬µÄʵÏÖÔÀí
Nginxͨ¹ýÉèÖÃHTTPÍ·²¿µÄ”Connection”×Ö¶ÎΪ”keep-alive”À´ÊµÏÖHTTP³¤ÅþÁ¬¡£µ±¿Í»§¶Ë·¢ËÍÒ»¸ö´øÓДConnection: keep-alive”Í·²¿µÄHTTPÇëÇóʱ£¬NginxÔÚÏìÓ¦ÖÐÒ²»á¼á³ÖÕâ¸öÅþÁ¬·¿ª£¬Ö±µ½µÖ´ïÒ»¶¨µÄʱ¼ä»òÇëÇóÊýÄ¿ÉÏÏ޲Żá¹Ø±ÕÅþÁ¬¡£
1.3 HTTP³¤ÅþÁ¬µÄÓÅÊÆ
ïÔÌÁËÅþÁ¬½¨ÉèºÍ¹Ø±ÕµÄ¿ªÏú£¬ÌáÉýÁËÐÔÄÜ£»
¿ÉÒÔ¸´ÓÃTCPÅþÁ¬£¬ïÔÌÁËÍøÂç´«ÊäµÄÏûºÄ£»
Ìá¸ßÁËЧÀÍÆ÷²¢·¢´¦ÀíÄÜÁ¦£¬½µµÍÁËЧÀÍÆ÷µÄ¸ºÔØ¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£¬Õ¹Ê¾ÁËÔõÑùÆôÓÃHTTP³¤ÅþÁ¬£º
http { keepalive_timeout 65; keepalive_requests 100; server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Connection "keep-alive"; } } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢HTTP/2¶à·¸´ÓõĵײãʵÏÖÔÀí
2.1 ʲôÊÇHTTP/2¶à·¸´ÓÃ
HTTP/2¶à·¸´ÓÃÊÇÖ¸ÔÚµ¥¸öTCPÅþÁ¬ÉÏͬʱ¾ÙÐжà¸öHTTPÇëÇóºÍÏìÓ¦µÄ´«Ê䣬´Ó¶ø×èÖ¹ÁËHTTP/1.xÖеĶÓÍ·ÛÕ±ÕÎÊÌâ¡£
2.2 HTTP/2¶à·¸´ÓõÄʵÏÖÔÀí
Nginxͨ¹ýʹÓöþ½øÖÆÖ¡ºÍÁ÷µÄ¿´·¨À´ÊµÏÖHTTP/2µÄ¶à·¸´Óá£ÔÚÒ»¸öTCPÅþÁ¬ÖУ¬Ã¿¸öHTTPÇëÇó±»·ÖÅÉÒ»¸öΨһµÄÁ÷±êʶ·û£¬²¢Í¨¹ýÖ¡µÄ·½·¨¾ÙÐд«Êä¡£Nginx¿ÉÒÔͬʱ´¦Àí¶à¸öÁ÷£¬ÎÞÐèÆÚ´ýÇ°Ò»¸öÇëÇóµÄÏìÓ¦·µ»Ø¡£
2.3 HTTP/2¶à·¸´ÓõÄÓÅÊÆ
½â¾öÁËHTTP/1.xÖеĶÓÍ·ÛÕ±ÕÎÊÌ⣬ÌáÉýÁË´«ÊäЧÂÊ£»
¿ÉÒÔ¸üºÃµØʹÓÃÍøÂç´ø¿í£¬ïÔÌÁË×ÊÔ´µÄÆÌÕÅ£»
Ìá¸ßÁ˲¢·¢´¦ÀíÄÜÁ¦£¬½µµÍÁËЧÀÍÆ÷µÄ¸ºÔØ¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£¬Õ¹Ê¾ÁËÔõÑùÆôÓÃHTTP/2¶à·¸´Óãº
http { listen 443 http2; server_name example.com; location / { proxy_pass http://backend; proxy_http_version 2.0; } }
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
NginxµÄHTTP³¤ÅþÁ¬ºÍHTTP/2¶à·¸´ÓÃÊÖÒÕ¶¼ÊÇΪÁËÌáÉýЧÀÍÆ÷ÐÔÄܺÍЧÂʶøÉè¼ÆµÄ¡£HTTP³¤ÅþÁ¬ïÔÌÁËÅþÁ¬½¨ÉèºÍ¹Ø±ÕµÄ¿ªÏú£¬Ìá¸ßÁËЧÀÍÆ÷²¢·¢´¦ÀíÄÜÁ¦£»HTTP/2¶à·¸´Óýâ¾öÁËHTTP/1.xµÄ¶ÓÍ·ÛÕ±ÕÎÊÌ⣬ÌáÉýÁË´«ÊäЧÂʲ¢ïÔÌÁË×ÊÔ´ÆÌÕÅ¡£ºÏÀíʹÓÃÕâЩÊÖÒÕ¿ÉÒÔÈÃWebÓ¦Óøü¿ìµØÏìÓ¦¿Í»§¶ËÇëÇó£¬ÌáÉýÓû§ÌåÑé¡£
²Î¿¼×ÊÁÏ£º
Nginx¹Ù·½Îĵµ£ºhttps://nginx.org/
¡¶Nginx¸ßÐÔÄÜWebЧÀÍÆ÷Ïê½â¡·
ÒÔÉϾÍÊÇÆÊÎöNginxµÄHTTP³¤ÅþÁ¬ºÍHTTP/2¶à·¸´ÓõĵײãʵÏÖÔÀí¼°ÓÅÊƵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡