ÆÊÎöNginxµÄ·´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æʵÏÖϸ½Ú
ÆÊÎöNginxµÄ·´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æʵÏÖϸ½Ú
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷¡£ÔÚÏÖʵµÄWebÓ¦ÓÃÖУ¬ÎªÁËÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂʺÍÐÔÄÜ£¬¾³£ÐèҪʹÓ÷´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æÀ´¼ÓËÙWebÒ³ÃæµÄ¼ÓÔغÍÏìÓ¦¡£±¾ÎĽ«ÏêϸÆÊÎöNginxµÄ·´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æµÄʵÏÖϸ½Ú£¬²¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢·´ÏòÊðÀí»º´æµÄʵÏÖϸ½Ú
·´ÏòÊðÀí»º´æÊÇÖ¸NginxÔÚ×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷µÄͬʱ£¬Ò²¿ÉÒÔ½«¿Í»§¶ËÇëÇóµÄÏìӦЧ¹û»º±£´æ×ÔÉíµÄÄÚ´æ»ò´ÅÅÌÉÏ£¬ÒÔ±ãÏ´ÎÇëÇóͬÑùµÄ×ÊԴʱֱ½Ó·µ»Ø»º´æЧ¹û£¬´Ó¶øÌá¸ß»á¼ûËÙÂʺͼõÇáºó¶ËЧÀÍÆ÷µÄѹÁ¦¡£
ÆôÓ÷´ÏòÊðÀí»º´æ
Ê×ÏÈÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÆôÓ÷´ÏòÊðÀí»º´æµÄ¹¦Ð§¡£ÔÚhttpÄ£¿éÄÚ¿ÉÒÔÌí¼ÓÒÔÏÂÉèÖÃÐÅÏ¢£º
http { ... proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { ... } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬proxy_cache_pathÖ¸¶¨ÁË»º´æµÄ·¾¶ºÍÏà¹Ø²ÎÊý¡£levels²ÎÊýÖ¸¶¨ÁË»º´æµÄĿ¼½á¹¹£¬keys_zone²ÎÊýÖ¸¶¨ÁË»º´æµÄÃû³ÆºÍ¾Þϸ£¬max_size²ÎÊýÖ¸¶¨ÁË»º´æµÄ×î´óÈÝÁ¿£¬inactive²ÎÊýÖ¸¶¨ÁË»º´æµÄÓâÆÚʱ¼ä£¬use_temp_path²ÎÊýÖ¸¶¨ÁËÊÇ·ñʹÓÃÔÝʱ·¾¶¾ÙÐлº´æ¡£ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËÃûΪmy_cacheµÄ»º´æ¡£
ÉèÖ÷´ÏòÊðÀí»º´æµÄ¹æÔò
ÔÚNginxµÄÉèÖÃÎļþÖУ¬¿ÉÒÔͨ¹ýlocationÖ¸ÁîÀ´ÉèÖ÷´ÏòÊðÀí»º´æµÄ¹æÔò¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½«Ö¸¶¨µÄURL·¾¶¾ÙÐлº´æ£¬ÏêϸµÄÉèÖÃÈçÏÂËùʾ£º
server { ... location /cached { proxy_cache my_cache; proxy_cache_valid 200 304 10m; proxy_cache_use_stale error timeout invalid_header http_500; proxy_cache_background_update on; proxy_pass http://backend_server; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬proxy_cacheÖ¸¶¨ÁËʹÓÃÄĸö»º´æ£¬proxy_cache_validÖ¸¶¨ÁË»º´æµÄÓÐÓÃÆÚ£¬proxy_cache_use_staleÖ¸¶¨Á˵±»º´æÓâÆÚʱÊÇ·ñʹÓþɵĻº´æ¾ÙÐÐÏìÓ¦£¬proxy_cache_background_updateÖ¸¶¨ÁËÊÇ·ñºǫ́¸üлº´æ£¬proxy_passÖ¸¶¨Á˺ó¶ËЧÀÍÆ÷µÄµØµã¡£
ɨ³ý»º´æ
ÓÐʱ¼äÐèÒªÊÖ¶¯É¨³ý»º´æ£¬¿ÉÒÔʹÓÃproxy_cache_purgeÄ£¿éÀ´ÊµÏÖ¡£ÔÚNginxµÄÉèÖÃÎļþÖУ¬¿ÉÒÔÌí¼ÓÒÔÏÂÉèÖãº
location /purge { bproxy_cache_purge my_cache "$scheme$request_method$host$request_uri"; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬proxy_cache_purgeÖ¸¶¨ÁËɨ³ý»º´æµÄÏÂÁ²¢×ª´ïÁË»º´æµÄÃû³ÆºÍÇëÇóµÄURI¡£
¶þ¡¢¶¯Ì¬ÄÚÈÝ»º´æµÄʵÏÖϸ½Ú
¶¯Ì¬ÄÚÈÝ»º´æÊÇÖ¸NginxÔÚ·´ÏòÊðÀíЧÀÍÆ÷µÄͬʱ£¬Ò²¿ÉÒÔ½«¶¯Ì¬ÌìÉúµÄÄÚÈݾÙÐлº´æ£¬ÒÔ¼õÇáºó¶ËЧÀÍÆ÷µÄ¸ºÔغÍÌá¸ßÏìÓ¦ËÙÂÊ¡£
ÆôÓö¯Ì¬ÄÚÈÝ»º´æ
Ê×ÏÈÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÆôÓö¯Ì¬ÄÚÈÝ»º´æµÄ¹¦Ð§¡£ÔÚhttpÄ£¿éÄÚ¿ÉÒÔÌí¼ÓÒÔÏÂÉèÖÃÐÅÏ¢£º
http { ... fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { ... } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬fastcgi_cache_pathÖ¸¶¨ÁË»º´æµÄ·¾¶ºÍÏà¹Ø²ÎÊý£¬Óë·´ÏòÊðÀí»º´æµÄÉèÖÃÀàËÆ¡£Í¬Ñù£¬ÎÒÃÇʹÓÃÁËÃûΪmy_cacheµÄ»º´æ¡£
ÉèÖö¯Ì¬ÄÚÈÝ»º´æµÄ¹æÔò
ÔÚNginxµÄÉèÖÃÎļþÖУ¬Ò²¿ÉÒÔͨ¹ýlocationÖ¸ÁîÀ´ÉèÖö¯Ì¬ÄÚÈÝ»º´æµÄ¹æÔò¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½«Ö¸¶¨µÄURL·¾¶µÄ¶¯Ì¬ÄÚÈݾÙÐлº´æ£¬ÏêϸµÄÉèÖÃÈçÏÂËùʾ£º
server { ... location /dynamic { fastcgi_cache my_cache; fastcgi_cache_valid 200 10m; fastcgi_cache_methods GET HEAD; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_cache_lock on; fastcgi_cache_lock_timeout 5s; fastcgi_pass backend_server; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬fastcgi_cacheÖ¸¶¨ÁËʹÓÃÄĸö»º´æ£¬fastcgi_cache_validÖ¸¶¨ÁË»º´æµÄÓÐÓÃÆÚ£¬fastcgi_cache_methodsÖ¸¶¨ÁËÄÄЩHTTPÒªÌå»á±»»º´æ£¬fastcgi_cache_use_staleÖ¸¶¨Á˵±»º´æÓâÆÚʱÊÇ·ñʹÓþɵĻº´æ¾ÙÐÐÏìÓ¦£¬fastcgi_cache_lockÖ¸¶¨ÁËÊÇ·ñÆôÓûº´æËøºÍËøµÄ³¬Ê±Ê±¼ä£¬fastcgi_passÖ¸¶¨Á˺ó¶ËЧÀÍÆ÷µÄµØµã¡£
ɨ³ý»º´æ
ͬÑùµØ£¬ÓÐʱ¼äÐèÒªÊÖ¶¯É¨³ý»º´æ£¬¿ÉÒÔʹÓÃfastcgi_cache_purgeÄ£¿éÀ´ÊµÏÖ¡£ÔÚNginxµÄÉèÖÃÎļþÖУ¬¿ÉÒÔÌí¼ÓÒÔÏÂÉèÖãº
location /purge { fastcgi_cache_purge my_cache "$scheme$request_method$host$request_uri"; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬fastcgi_cache_purgeÖ¸¶¨ÁËɨ³ý»º´æµÄÏÂÁ²¢×ª´ïÁË»º´æµÄÃû³ÆºÍÇëÇóµÄURI¡£
×ÛÉÏËùÊö£¬±¾ÎÄÏêϸÆÊÎöÁËNginxµÄ·´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æµÄʵÏÖϸ½Ú£¬²¢¸ø³öÁËÏìÓ¦µÄ´úÂëʾÀý¡£Í¨¹ýºÏÀíÉèÖúÍʹÓÃNginxµÄ»º´æ¹¦Ð§£¬¿ÉÒÔÏÔÖøÌáÉýWebÓ¦ÓõÄÐÔÄܺÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÆÊÎöNginxµÄ·´ÏòÊðÀí»º´æºÍ¶¯Ì¬ÄÚÈÝ»º´æʵÏÖϸ½ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡