Nginx·´ÏòÊðÀí»º´æÉèÖã¬ÌáÉýÍøÕ¾»á¼ûËÙÂÊ
nginx·´ÏòÊðÀí»º´æÉèÖã¬ÌáÉýÍøÕ¾»á¼ûËÙÂÊ
СÐò£º
ÔÚ»¥ÁªÍøʱ´ú£¬ÍøÕ¾µÄ»á¼ûËÙÂÊÊÇÖÁ¹ØÖ÷ÒªµÄ¡£Ò»¸ö¼ÓÔØ»ºÂýµÄÍøÕ¾»áÈÃÓû§¸ÐÓ¦²»ÄÍÐÄ£¬²¢ÇÒ¿ÉÄܵ¼ÖÂÓû§Á÷ʧ¡£ÎªÁËÌáÉýÍøÕ¾µÄ»á¼ûËÙÂÊ£¬Ò»ÖÖ³£¼ûµÄ·½·¨ÊÇͨ¹ýʹÓ÷´ÏòÊðÀí»º´æÀ´ïÔÌЧÀÍÆ÷µÄ¸ºÔز¢¼ÓËÙÒ³ÃæµÄ¼ÓÔØ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÉèÖ÷´ÏòÊðÀí»º´æ£¬´Ó¶øÌáÉýÍøÕ¾µÄ»á¼ûËÙÂÊ¡£
Ò»¡¢Ê²Ã´ÊÇNginx·´ÏòÊðÀí»º´æ£¿
NginxÊÇÒ»¿îÇáÁ¿¼¶µÄHTTP·´ÏòÊðÀíЧÀÍÆ÷£¬Ëü¿ÉÒÔ½«¿Í»§¶ËµÄÇëÇóת·¢¸øºó¶ËµÄÓ¦ÓÃЧÀÍÆ÷£¬²¢½«Æä·µ»ØЧ¹û»º´æÏÂÀ´¡£µ±ÏÂÒ»´ÎÓÐÏàͬÇëÇóµÖ´ïʱ£¬Nginx¿ÉÒÔÖ±½Ó·µ»Ø»º´æÖеÄЧ¹û£¬¶øÎÞÐèÔÙ´ÎÇëÇóÓ¦ÓÃЧÀÍÆ÷£¬´Ó¶ø¼ÓËÙÒ³ÃæµÄ¼ÓÔØËÙÂÊ¡£
¶þ¡¢Nginx·´ÏòÊðÀí»º´æµÄÉèÖð취£º
×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£ÏêϸµÄ×°Öð취¿ÉÒԲο¼Nginx¹Ù·½Îĵµ»òÕßʹÓðü¹ÜÀíÆ÷¾ÙÐÐ×°Öá£
ÉèÖÃNginx
·¿ªNginxµÄÉèÖÃÎļþ£¬ÔÚserver¿éÖÐÌí¼ÓÈçÏ´úÂë¶Î£º
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄproxy_cache_pathÖ¸ÁîÓÃÓÚÉèÖûº´æµÄ·¾¶ºÍÈÝÁ¿¡£path/to/cacheÊÇ»º´æµÄ´æ´¢Â·¾¶£¬my_cacheÊÇ»º´æÇøµÄÃû³Æ£¬10mÖ¸¶¨ÁË»º´æÇøµÄ¾Þϸ£¬10gÌåÏÖÕû¸ö»º´æµÄ×î´óÈÝÁ¿Îª10GB£¬inactive=60mÌåÏÖ»º´æµÄÄÚÈÝÔÚ60·ÖÖÓÄÚûÓб»»á¼û¾ÍÓâÆÚ¡£
ÔÚlocation¿éÖÐÌí¼ÓÈçÏ´úÂë¶Î£º
proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄproxy_cacheÖ¸ÁîÓÃÓÚÆôÓûº´æ¹¦Ð§£¬proxy_cache_validÖ¸ÁîÓÃÓÚÉèÖûº´æµÄÓÐÓÃʱ¼ä¡£ÉÏÃæµÄʾÀýÖУ¬¹ØÓÚHTTP״̬ÂëΪ200ºÍ302µÄÏìÓ¦£¬»º´æÓÐÓÃʱ¼äΪ10·ÖÖÓ£»¹ØÓÚHTTP״̬ÂëΪ404µÄÏìÓ¦£¬»º´æÓÐÓÃʱ¼äΪ1·ÖÖÓ¡£
ÉèÖûº´æ¹æÔò
³ýÁËÉèÖûº´æ·¾¶»ººÍ´æÓÐÓÃʱ¼ä£¬ÎÒÃÇ»¹¿ÉÒÔÉèÖÃһЩ»º´æ¹æÔò£¬ÒÔ¾öÒéÄÄЩÇëÇóÐèÒª±»»º´æ¡£ÔÚlocation¿éÖÐÌí¼ÓÈçÏ´úÂë¶Î£º
proxy_cache_key $host$uri$is_args$args; proxy_cache_bypass $http_cache_control; proxy_no_cache $http_pragma $http_authorization;
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄʾÀýÖУ¬proxy_cache_keyÓÃÓÚÉèÖûº´æµÄkey£¬ÕâÀïʹÓÃÁËÇëÇóµÄhost¡¢uriºÍ²ÎÊý×÷Ϊkey¡£proxy_cache_bypassÓÃÓÚÈƹý»º´æ£¬ÕâÀïͨ¹ý¼ì²éHTTPÇëÇóÍ·ÖеÄCache-Control×Ö¶ÎʵÏÖÁ˴˹¦Ð§¡£proxy_no_cacheÓÃÓÚÉèÖÃÍêÈ«½ûÓûº´æµÄÌõ¼þ£¬ÕâÀïͨ¹ý¼ì²éHTTPÇëÇóÍ·ÖеÄPragmaºÍAuthorization×Ö¶ÎʵÏÖÁ˴˹¦Ð§¡£
ÖØÆôNginx
Íê³ÉÒÔÉÏÉèÖúó£¬ÉúÑIJ¢¹Ø±ÕÉèÖÃÎļþ¡£È»ºóʹÓÃÏÂÁîÖØÆôNginx£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
Èý¡¢Nginx·´ÏòÊðÀí»º´æµÄʹÓó¡¾°
Nginx·´ÏòÊðÀí»º´æÊÊÓÃÓÚÄÇЩÄÚÈÝÏà¶ÔÎȹÌÇÒ²»¾³£¸üеÄÍøÕ¾¡£ÀýÈ磬¾²Ì¬ÍøÒ³¡¢Í¼Æ¬¡¢CSSºÍJavaScript×ÊÔ´µÈ¶¼¿ÉÒÔ¾ÙÐлº´æ£¬ÒÔïÔ̶Ժó¶ËЧÀÍÆ÷µÄÇëÇó£¬ÌáÉýÍøÕ¾µÄ¼ÓÔØËÙÂÊ¡£
ÐèҪעÖصÄÊÇ£¬Ò»Ð©¶¯Ì¬ÄÚÈÝÀýÈçÓû§µÇ¼ÐÅÏ¢»ò¸öÐÔ»¯ÄÚÈÝ£¬Ôò²»ÊʺϾÙÐлº´æ¡£
½áÂÛ£º
NginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§¿ÉÒÔÓÐÓõؼÓËÙÍøÕ¾µÄ»á¼ûËÙÂÊ¡£Í¨¹ýÉèÖÃNginx·´ÏòÊðÀí»º´æ£¬ÎÒÃÇ¿ÉÒÔïÔ̶Ժó¶ËЧÀÍÆ÷µÄÇëÇó¸ºÔØ£¬ÌáÉýÓû§µÄ»á¼ûÌåÑ顣Ȼ¶ø£¬ºÏÀíµØÉèÖûº´æ¹æÔòÊÇÖÁ¹ØÖ÷ÒªµÄ£¬ÒÔ°ü¹Ü»º´æÄÚÈݵÄʵʱÐÔºÍÒ»ÖÂÐÔ¡£
²Î¿¼ÎÄÏ×£º
Nginx Documentation. (https://nginx.org/en/docs/)
Tuning Nginx for Performance. (https://www.nginx.com/blog/tuning-nginx/)
ͨ¹ýÒÔÉÏ°ì·¨£¬Äã¿ÉÒÔʹÓÃNginxµÄ·´ÏòÊðÀí»º´æÉèÖÃÀ´ÌáÉýÍøÕ¾µÄ»á¼ûËÙÂÊ¡£Ï£ÍûÄܶÔÄãÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginx·´ÏòÊðÀí»º´æÉèÖã¬ÌáÉýÍøÕ¾»á¼ûËÙÂʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡