NginxЧÀÍÆ÷µÄHTTP/2ÐÒéÖ§³ÖºÍÐÔÄÜÌáÉý¼¼ÇÉÏÈÈÝ
NginxЧÀÍÆ÷µÄHTTP/2ÐÒéÖ§³ÖºÍÐÔÄÜÌáÉý¼¼ÇÉÏÈÈÝ
СÐò£º
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬ÈËÃǶÔÍøÕ¾ËÙÂʵÄÒªÇóÔ½À´Ô½¸ß¡£ÎªÁËÌṩ¸ü¿ìµÄÍøÕ¾ÏìÓ¦ËÙÂʺ͸üºÃµÄÓû§ÌåÑ飬NginxЧÀÍÆ÷µÄHTTP/2ÐÒéÖ§³ÖºÍÐÔÄÜÌáÉý¼¼ÇɱäµÃÖÁ¹ØÖ÷Òª¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginxЧÀÍÆ÷ÒÔÖ§³ÖHTTP/2ÐÒ飬²¢ÌṩһЩÐÔÄÜÌáÉýµÄ¼¼ÇÉ¡£
Ò»¡¢HTTP/2ÐÒé¼ò½é£º
HTTP/2ÐÒéÊÇHTTPÐÒéµÄÏÂÒ»´ú±ê×¼£¬ËüÔÚ´«Êä²ãʹÓöþ½øÖÆÃûÌþÙÐÐÊý¾Ý´«Ê䣬Ïà±È֮ǰµÄHTTP1.xÐÒ飬HTTP/2ÐÒé¾ßÓиüµÍµÄÑӳٺ͸ü¸ßµÄ²¢·¢´«ÊäÄÜÁ¦¡£Í¨¹ýʹÓÃHTTP/2ÐÒ飬¿ÉÒÔÏÔÖøÌá¸ßÍøÕ¾µÄ¼ÓÔØËÙÂʺÍÐÔÄÜ¡£
¶þ¡¢ÉèÖÃNginxЧÀÍÆ÷Ö§³ÖHTTP/2ÐÒ飺
×°ÖÃNginx£º
Ê×ÏÈÐèҪȷ±£ÒѾװÖÃÁËNginxЧÀÍÆ÷¡£ÈôÊÇÉÐδװÖ㬿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃNginxÖ§³ÖHTTP/2ÐÒ飺
ÕÒµ½NginxµÄÉèÖÃÎļþ£¬Ä¬ÈÏ·¾¶Îª/etc/nginx/nginx.conf£¬Ê¹ÓÃÎı¾±à¼Æ÷·¿ª¡£
sudo nano /etc/nginx/nginx.conf
µÇ¼ºó¸´ÖÆ
ÔÚhttp¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
http { ... listen 443 ssl http2; ssl_certificate /etc/ssl/certs/nginx.crt; ssl_certificate_key /etc/ssl/private/nginx.key; ... }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬listen 443 ssl http2ÌåÏÖʹÓÃHTTPSÐæźÍHTTP/2ÐÒé¾ÙÐмàÌý¡£ssl_certificateºÍssl_certificate_keyÖ¸¶¨ÁËSSLÖ¤ÊéºÍ˽ԿµÄ·¾¶¡£È·±£½«×¼È·ÉèÖÃÖ¤ÊéºÍ˽ԿµÄ·¾¶¡£
ÖØÆôNginxЧÀÍÆ÷£º
Íê³ÉÉèÖúó£¬ÐèÖØÆôNginxЧÀÍÆ÷ÒÔʹÉèÖÃÉúЧ¡£
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
Èý¡¢ÐÔÄÜÌáÉýµÄ¼¼ÇÉ£º
³ýÁËÉèÖÃÖ§³ÖHTTP/2ÐÒéÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃһЩ¼¼ÇÉÀ´½øÒ»²½ÌáÉýNginxЧÀÍÆ÷µÄÐÔÄÜ¡£
ÆôÓÃGzipѹËõ£º
¿ªÆôGzipѹËõ¿ÉÒÔïÔÌ´«ÊäÊý¾ÝÁ¿£¬¼ÓËÙÍøÕ¾µÄ¼ÓÔØËÙÂÊ¡£ÔÚNginxÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº
http { ... gzip on; gzip_types text/plain text/css application/json application/javascript text/xml; ... }
µÇ¼ºó¸´ÖÆ
gzip onÌåÏÖÆôÓÃGzipѹËõ¡£gzip_typesÖ¸¶¨ÁËҪѹËõµÄÎļþÀàÐÍ¡£
ÉèÖþ²Ì¬×ÊÔ´»º´æ£º
¹ØÓÚ¾²Ì¬×ÊÔ´Îļþ£¬¿ÉÒÔ¾ÙÐлº´æÒÔïÔÌЧÀÍÆ÷µÄ¸ºÔØ¡£ÔÚNginxÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº
http { ... location ~* .(css|js|png|jpg|jpeg|gif|ico)$ { expires 1y; add_header Cache-Control "public"; } ... }
µÇ¼ºó¸´ÖÆ
Õâ¶ÎÉèÖÃÌåÏÖ¶ÔÒ»ÄêÄÚ²»»á¸Ä±äµÄ¾²Ì¬×ÊÔ´Îļþ¾ÙÐлº´æ¡£
ÆôÓÃKeep-AliveÅþÁ¬£º
Keep-AliveÅþÁ¬Ê¹ä¯ÀÀÆ÷ºÍЧÀÍÆ÷Ö®¼ä¿ÉÒÔ¶à´Î·¢ËÍÇëÇóºÍÏìÓ¦£¬×èÖ¹ÁËÿ´ÎÇëÇ󶼽¨ÉèеÄÅþÁ¬£¬ïÔÌÁËÑÓ³ÙºÍÅþÁ¬½¨ÉèµÄ¿ªÏú¡£ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº
http { ... keepalive_timeout 65; ... }
µÇ¼ºó¸´ÖÆ
keepalive_timeoutÌåÏÖKeep-AliveÅþÁ¬µÄ³¬Ê±Ê±¼ä£¬Ò»Ñùƽ³£ÉèΪ60Ãë¡£
¿¢ÊÂÓ
±¾ÎÄÏÈÈÝÁËÔõÑùÉèÖÃNginxЧÀÍÆ÷ÒÔÖ§³ÖHTTP/2ÐÒ飬²¢ÌṩÁËһЩÐÔÄÜÌáÉýµÄ¼¼ÇÉ£¬°üÀ¨ÆôÓÃGzipѹËõ¡¢ÉèÖþ²Ì¬×ÊÔ´»º´æºÍÆôÓÃKeep-AliveÅþÁ¬¡£Í¨¹ýºÏÀíÉèÖúÍÓÅ»¯£¬¿ÉÒÔÏÔÖøÌáÉýNginxЧÀÍÆ÷µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ£¬Ìṩ¸üºÃµÄÓû§ÌåÑ顣ϣÍû±¾ÎĶÔÄãÃ÷È·ºÍÓ¦ÓÃNginxЧÀÍÆ÷µÄHTTP/2ÐÒéÓëÐÔÄÜÌáÉý¼¼ÇÉÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginxЧÀÍÆ÷µÄHTTP/2ÐÒéÖ§³ÖºÍÐÔÄÜÌáÉý¼¼ÇÉÏÈÈݵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡