ÔõÑùʹÓÃNginx¾ÙÐж¯Ì¬ÄÚÈÝ»º´æ
ÔõÑùʹÓÃnginx¾ÙÐж¯Ì¬ÄÚÈÝ»º´æ
NginxÊÇÒ»¿î¸ßÐÔÄܵÄwebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆÕ±éÓÃÓÚ¹¹½¨¸ßÐÔÄܵÄÍøÕ¾ºÍÓ¦ÓóÌÐò¡£³ýÁË×÷ΪЧÀÍÆ÷£¬Nginx»¹¿ÉÒÔ×÷Ϊ»º´æЧÀÍÆ÷À´»º´æ¾²Ì¬ºÍ¶¯Ì¬ÄÚÈÝ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´»º´æ¶¯Ì¬ÄÚÈÝ£¬ÌáÉýÍøÕ¾ÐÔÄܺÍÓû§ÌåÑé¡£
NginxʹÓÃproxy_cacheÄ£¿éÀ´ÊµÏÖ»º´æ¹¦Ð§¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýÉèÖÃÎļþ£º
http { proxy_cache_path /path/to/cache keys_zone=my_cache:10m; server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_key $scheme$host$request_uri; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; } } upstream backend { server backend.example.com; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÉèÖÃÎļþÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö»º´æĿ¼/path/to/cache£¬²¢¸øËü·ÖÅÉÁËÒ»¸öÃûΪmy_cacheµÄkeys_zone£¬ÕâÑùNginx¾Í¿ÉÒÔ½«»º´æÄÚÈÝÉúÑĵ½¸ÃĿ¼ÖС£È»ºó£¬ÎÒÃÇÔÚserver¿éÖеÄlocationÖпªÆôÁËÊðÀíģʽ£¬²¢½«ÇëÇóת·¢¸øÁ˺ó¶ËЧÀÍÆ÷backend.example.com¡£
ÔÚproxy_cacheÏà¹ØµÄÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËproxy_cache_keyÖ¸ÁîÀ´½ç˵»º´æµÄkey£¬ÕâÀïʹÓÃÁËÇëÇóµÄscheme¡¢hostºÍrequest_uriÀ´×öΪ»º´æµÄkey¡£ÕâÑù¾Í¿ÉÒÔÈ·±£ÏàͬµÄÇëÇó»á±»»º´æÆðÀ´£¬×èÖ¹ÏàͬµÄÇëÇó¶à´ÎÏòºó¶ËЧÀÍÆ÷·¢ËÍ¡£
ÎÒÃÇ»¹Ê¹ÓÃÁËproxy_cache_validÖ¸ÁîÀ´½ç˵ÁË»º´æµÄÓÐÓÃʱ¼ä¡£¹ØÓÚ·µ»ØÂëΪ200ºÍ302µÄÏìÓ¦£¬ÎÒÃÇÉèÖÃÁËÓÐÓÃʱ¼äΪ10·ÖÖÓ£»¹ØÓÚ·µ»ØÂëΪ404µÄÏìÓ¦£¬ÉèÖÃÁËÓÐÓÃʱ¼äΪ1·ÖÖÓ¡£ÕâÑù¾Í¿ÉÒÔƾ֤²î±ðµÄ·µ»ØÂëÀ´ÉèÖòî±ðµÄ»º´æʱ¼ä¡£
ÉèÖÃÍê±Ïºó£¬ÉúÑÄÎļþ²¢ÖØмÓÔØNginxµÄÉèÖ᣽ÓÏÂÀ´£¬µ±ÓÐÇëÇóµÖ´ïʱ£¬Nginx»áÊ×Ïȼì²éÊÇ·ñÓлº´æ£¬ÈôÊÇÓУ¬¾ÍÖ±½Ó·µ»Ø»º´æÄÚÈÝ£¬ÈôÊÇûÓУ¬Ôò½«ÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷²¢½«ÏìÓ¦ÉúÑĵ½»º´æÖС£
ÖµµÃ×¢ÖصÄÊÇ£¬NginxµÄ»º´æÊÇ»ùÓÚÎļþµÄ£¬ÕâÒâζ׏ØÓÚ¶¯Ì¬ÄÚÈݵĻº´æ£¬Í¨³£»áÉúÑÄΪ¾²Ì¬Îļþ¡£Òò´Ë£¬ÈôÊǶ¯Ì¬ÄÚÈÝƵÈÔת±ä£¬»òÕßÐèҪƾ֤Óû§µÄÌض¨ÐÅÏ¢À´·µ»ØÄÚÈÝ£¬ÐèҪעÖØ»º´æµÄ¸üÐÂÕ½ÂÔ¡£
ͨ¹ýʹÓÃNginx¾ÙÐж¯Ì¬ÄÚÈÝ»º´æ£¬¿ÉÒÔ´ó´óÌáÉýÍøÕ¾µÄÐÔÄܺÍÓû§ÌåÑé¡£µ±»º´æÖÀÖÐʱ£¬¿ÉÒÔ×èÖ¹Öظ´´¦Öóͷ£ÇëÇ󣬼õÇáºó¶ËЧÀÍÆ÷µÄ¸ºÔØ£»Í¬Ê±£¬ÓÉÓÚ¾²Ì¬ÎļþµÄ´«ÊäËÙÂʸü¿ì£¬Ò²¿ÉÒÔËõ¶ÌÒ³Ãæ¼ÓÔØʱ¼ä£¬ÌáÉýÓû§µÄ»á¼ûÌåÑé¡£
Ï£Íû±¾ÎÄÄܶԸ÷ÈËÏàʶÔõÑùʹÓÃnginx¾ÙÐж¯Ì¬ÄÚÈÝ»º´æÓÐËù×ÊÖú¡£ÈôÓÐÈκÎÒÉÎÊ£¬½Ó´ýÁôÑÔÌÖÂÛ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐж¯Ì¬ÄÚÈÝ»º´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡