ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄѹËõÏ¢ÕùѹËõ
ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄѹËõÏ¢ÕùѹËõ
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Æ书ЧǿʢÇÒÎÞа¡£ÔÚ´¦ÀíHTTPÇëÇóʱ£¬¿ÉÒÔʹÓÃNginxÌṩµÄgzipºÍgunzipÄ£¿é¶ÔÇëÇó¾ÙÐÐѹËõÏ¢ÕùѹËõ£¬ÒÔ¼õСÊý¾Ý´«ÊäÁ¿£¬Ìá¸ßÇëÇóÏìÓ¦ËÙÂÊ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄѹËõÏ¢ÕùѹËõµÄÏêϸ°ì·¨£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
ÉèÖÃgzipÄ£¿é
Ê×ÏÈÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÆôÓÃgzipÄ£¿é¡£·¿ªÄãµÄNginxÉèÖÃÎļþ£¨Í¨³£Î»ÓÚ/etc/nginx/nginx.conf»ò/usr/local/nginx/conf/nginx.conf£©£¬ÕÒµ½http¿é£¬Ìí¼ÓÒÔÏÂÉèÖãº
http { gzip on; gzip_types text/plain text/css application/javascript; gzip_min_length 1024; gzip_comp_level 1; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖõļÄÒåÈçÏ£º
gzip on; ÌåÏÖÆôÓÃgzipÄ£¿é¡£
gzip_types ÌåÏÖÐèҪѹËõµÄÎļþÀàÐÍ¡£ÕâÀïÎÒÃÇÑ¡ÔñÁËÎı¾Îļþ¡¢CSSÎļþºÍJavaScriptÎļþ¡£Äã¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇó¾ÙÐÐÉèÖá£
gzip_min_length ÌåÏÖÖ»ÓÐÎļþ³¤¶ÈÁè¼ÝÕâ¸öÖµµÄÎļþ²Å»á¾ÙÐÐѹËõ¡£
gzip_comp_level ÌåÏÖѹËõ¼¶±ð£¬¼¶±ðÔ½¸ß£¬Ñ¹ËõЧ¹ûÔ½ºÃ£¬µ«Õ¼ÓõÄCPU×ÊÔ´Ò²Ô½¶à¡£
ÉèÖÃgunzipÄ£¿é
ÈôÊÇÄãÐèÒª¶ÔÎüÊÕµ½µÄÇëÇó¾ÙÐнâѹËõ£¬»¹ÐèÒªÆôÓÃgunzipÄ£¿é¡£ÔÚͳһ¸öÉèÖÃÎļþÖÐÕÒµ½http¿é£¬Ìí¼ÓÒÔÏÂÉèÖãº
µã»÷ÏÂÔØ¡°àËÃûÌÃѹËõ¾Þ½³¡±£»
http { gunzip on; }
µÇ¼ºó¸´ÖÆ
Ó¦ÓÃÉèÖò¢ÖØÆôNginx
ÉúÑÄNginxÉèÖÃÎļþºó£¬Ö´ÐÐÒÔÏÂÏÂÁîÓ¦ÓÃÉèÖò¢ÖØÆôNginxЧÀÍ£º
sudo service nginx reload
µÇ¼ºó¸´ÖÆ
ÇëÈ·±£Äã¾ß±¸×ã¹»µÄȨÏÞ¡£
Ñé֤ѹËõÏ¢ÕùѹËõ
ÏÖÔÚÄãÒѾÉèÖúÃÁËNginxµÄѹËõÏ¢ÕùѹËõÄ£¿é¡£Äã¿ÉÒÔͨ¹ýÏòNginx·¢ËÍHTTPÇëÇóÀ´ÑéÖ¤ÕâЩ¹¦Ð§ÊÇ·ñÉúЧ¡£
Ê×ÏÈ£¬Ê¹ÓÃcurl¹¤¾ß·¢ËÍÒ»¸öGETÇëÇóµ½ÉèÖõÄNginxЧÀÍ£º
curl -H "Accept-Encoding: gzip, deflate" -I http://localhost
µÇ¼ºó¸´ÖÆ
ÏÂÁîÖеÄ-H “Accept-Encoding: gzip, deflate”ÌåÏÖ¸æËßЧÀÍÆ÷¿ÉÒÔ½ÓÊÜgzip»òdeflateµÄѹËõ·½·¨¡£-IÌåÏÖÖ»»ñÈ¡ÏìӦͷÐÅÏ¢£¬¶ø²»ÐèÒª»ñÈ¡ÍêÕûµÄÏìÓ¦Ìå¡£
ÈôÊÇÒ»ÇÐÕý³££¬Ä㽫»áÔÚÏìӦͷÖп´µ½ÀàËÆÏÂÃæµÄÐÅÏ¢£º
Content-Encoding: gzip
µÇ¼ºó¸´ÖÆ
ÕâÌåÏÖЧÀÍÆ÷ÒѾ½«ÏìÓ¦ÌåѹËõ³ÉgzipÃûÌþÙÐд«Êä¡£
½ÓÏÂÀ´£¬Äã¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî·¢ËÍÒ»¸ö½âѹËõµÄÇëÇó£º
curl --compressed -I http://localhost
µÇ¼ºó¸´ÖÆ
ÏÂÁîÖеĖcompressedÌåÏÖ¸æËßЧÀÍÆ÷ÎüÊÕµ½µÄÏìÓ¦ÒѾ±»Ñ¹Ëõ£¬ÐèÒª¾ÙÐнâѹËõ¡£
ÈôÊÇÒ»ÇÐÕý³££¬Ä㽫»áÔÚÏìӦͷÖп´µ½ÀàËÆÏÂÃæµÄÐÅÏ¢£º
Content-Encoding:
µÇ¼ºó¸´ÖÆ
ÕâÌåÏÖЧÀÍÆ÷ÒѾ½«ÏìÓ¦Ìå½âѹËõ£¬²¢ÒÔÔʼÃûÌþÙÐд«Êä¡£
ͨ¹ýÒÔÉϵİ취ºÍÑéÖ¤£¬ÄãÒѾÀÖ³ÉÉèÖÃÁËNginxµÄgzipºÍgunzipÄ£¿é£¬ÊµÏÖÁËHTTPÇëÇóµÄѹËõÏ¢ÕùѹËõ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄѹËõÏ¢ÕùѹËõ£¬²¢ÌṩÁËÏà¹ØµÄÉèÖúÍÏÂÁîʾÀý¡£Ê¹ÓÃNginxµÄgzipºÍgunzipÄ£¿éÄܹ»×ÊÖúÄã¼õСÊý¾Ý´«ÊäÁ¿£¬Ìá¸ßÇëÇóµÄÏìÓ¦ËÙÂÊ¡£Í¨¹ýºÏÀíÉèÖúÍʹÓ㬿ÉÒÔÌáÉýWebÓ¦ÓõÄÐÔÄܺÍÓû§ÌåÑé¡£ÈôÊÇÄãÔÚʹÓÃÀú³ÌÖÐÓöµ½ÎÊÌ⣬¿ÉÒԲο¼NginxµÄ¹Ù·½Îĵµ»ò×·Çó×ÊÖú¡£Ï£Íû±¾ÎÄÄÚÈݶÔÄãÓÐËù×ÊÖú£¡
²Î¿¼Á´½Ó£º
Nginx¹Ù·½Îĵµ£ºhttp://nginx.org/en/docs/
GzipÄ£¿éÉèÖÃÖ¸ÄÏ£ºhttps://nginx.org/en/docs/http/ngx_http_gzip_module.html
GunzipÄ£¿éÉèÖÃÖ¸ÄÏ£ºhttps://nginx.org/en/docs/http/ngx_http_gunzip_module.html
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄѹËõÏ¢ÕùѹËõµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡