×ðÁú¿­Ê±¹ÙÍøµÇ¼

Nginx»º´æÕûÀíÉèÖÃÊÃ÷ÈÕ½£¬ÓÅ»¯ÍøÕ¾ÄÚÈݸüÐÂ

nginx»º´æÕûÀíÉèÖÃÊÃ÷ÈÕ½£¬ÓÅ»¯ÍøÕ¾ÄÚÈݸüÐÂ

СÐò£º

ÔÚÏÖ´ú»¥ÁªÍøÇéÐÎÏ£¬ÍøÕ¾µÄÄÚÈݸüÐÂËÙÂÊÔ½À´Ô½¿ì£¬Óû§¶ÔÐÂÏÊÄÚÈݵÄÐèÇóÒ²Ô½À´Ô½ÆÈÇС£ÎªÁËÌá¸ßÍøÕ¾µÄÓû§ÌåÑ飬ïÔ̭ЧÀÍÆ÷µÄ¸ºÔØ£¬ÍøÕ¾ÖÎÀíԱͨ³£»áʹÓûº´æÀ´¼ÓËÙÍøÕ¾µÄ»á¼ûËÙÂÊ¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬¾ß±¸Ç¿Ê¢µÄ»º´æ¹¦Ð§£¬¿ÉÒÔÓÐÓõػº´æ¾²Ì¬×ÊÔ´¡£È»¶ø£¬»º´æÓâÆÚºóÔõÑùʵʱÕûÀí£¬ÒÔ°ü¹ÜÓû§Äܹ»ÊµÊ±¿´µ½×îÐÂÄÚÈÝ£¬ÊÇÒ»¸öÐèÒª½â¾öµÄÎÊÌâ¡£±¾ÎĽ«¸ø³öNginx»º´æÕûÀíµÄÊÃ÷ÈÕ½ÉèÖã¬×ÊÖúÍøÕ¾ÖÎÀíÔ±ÓÅ»¯ÍøÕ¾ÄÚÈݸüС£

Ò»¡¢Nginx»º´æÉèÖÃ

Nginx»º´æ·¾¶ÉèÖÃ

Ê×ÏÈ£¬ÔÚnginx.confÉèÖÃÎļþÖÐÕÒµ½http¿é£¬½«ÒÔÏÂÉèÖÃÌí¼Óµ½ÆäÖУº

http {
 ...    
 proxy_cache_path /path/to/cache levels=1:2 keys_zone=cache_zone:10m max_size=10g inactive=60m;
 
 server {
     ...
 }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУº

/path/to/cacheΪ»º´æÎļþµÄ·¾¶£¬Æ¾Ö¤ÏÖÕæÏàÐξÙÐÐÉèÖá£

levels=1:2ÌåÏÖ»º´æÎļþϵͳµÄ²ã¼¶½á¹¹£¬¿Éƾ֤ÏÖʵÐèÇó¾ÙÐе÷½â¡£

keys_zone=cache_zone:10mÖ¸¶¨ÁË»º´æÎļþµÄÄÚ´æ¿Õ¼ä£¬10mÌåÏÖ10Õ××Ö½Ú¡£

max_size=10gÊÇ»º´æÎļþµÄ×î´óÈÝÁ¿£¬10gÌåÏÖ10ǧÕ××Ö½Ú¡£

inactive=60mÌåÏÖ»º´æÎļþÔÚ60·ÖÖÓûÓб»»á¼ûºó£¬½«±»É¾³ý¡£

»º´æÉèÖÃ

ÔÚÐèÒª¾ÙÐлº´æµÄЧÀÍÖУ¬Ìí¼ÓÒÔÏÂÉèÖãº

location / {
 proxy_cache cache_zone;
 proxy_cache_valid 200 302 5m;
 proxy_cache_valid 404 1m;
 
 proxy_pass http://backend;
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУº

proxy_cache cache_zone;Ö¸¶¨ÁËʹÓÃ֮ǰÉèÖõĻº´æÇøÓò¡£

proxy_cache_valid 200 302 5m;ÌåÏÖ¹ØÓÚ״̬ÂëΪ200ºÍ302µÄÏìÓ¦£¬»º´æÓÐÓÃʱ¼äΪ5·ÖÖÓ¡£

proxy_cache_valid 404 1m;ÌåÏÖ¹ØÓÚ״̬ÂëΪ404µÄÏìÓ¦£¬»º´æÓÐÓÃʱ¼äΪ1·ÖÖÓ¡£

proxy_pass http://backend;Ö¸¶¨ÁË·´ÏòÊðÀíµÄºó¶ËЧÀÍÆ÷µØµã¡£

¶þ¡¢Nginx»º´æÕûÀíÉèÖÃ

ΪÁË°ü¹ÜÓû§Äܹ»ÊµÊ±¿´µ½×îеÄÄÚÈÝ£¬»º´æÕûÀíÊDZز»¿ÉÉٵġ£NginxÌṩÁËÐí¶àÒªÁìÀ´ÕûÀí»º´æ£¬±¾ÎĽ«ÏÈÈݼ¸ÖÖ³£ÓõÄÒªÁì¡£

ÊÖ¶¯ÕûÀí

¿ÉÒÔÊÖ¶¯É¾³ý»º´æÎļþÀ´ÕûÀíNginx»º´æ¡£Ê×ÏÈ£¬½øÈëµ½»º´æÎļþËùÔÚµÄĿ¼£¬Ö´ÐÐÒÔÏÂÏÂÁ

rm -rf /path/to/cache/*

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÏÂÁɾ³ý¸ÃĿ¼ÏµÄËùÓлº´æÎļþ¡£

ʹÓÃNginxÌṩµÄÄ £¿é

NginxÌṩÁËngx_cache_purgeÄ £¿é£¬¿ÉÒÔͨ¹ýÉèÖÃÏîproxy_cache_purgeÀ´ÕûÀí»º´æ¡£Ê×ÏÈ£¬ÔÚnginx.confÉèÖÃÎļþµÄhttp¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº

http {
 ...
 
 proxy_cache_path /path/to/cache levels=1:2 keys_zone=cache_zone:10m max_size=10g inactive=60m;
 
 server {
     ...
     
     location ~ /purge(/.*) {
         allow all;
         proxy_cache_purge cache_zone $1$is_args$args;
     }
 }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÉèÖÃÖУ¬~ /purge(/.*)ÌåÏÖÆ¥ÅäÒÔ/purge/¿ªÍ·µÄ·¾¶¡£ÔÚÐèÒªÕûÀí»º´æµÄµØ·½£¬·¢ËÍÒ»¸ö´øÓÐ/purge/·¾¶µÄGETÇëÇó¼´¿É£¬ÀýÈ磺

curl -X GET http://your-domain.com/purge/your/path

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÏÂÁɨ³ýyour/path¶ÔÓ¦µÄ»º´æ¡£

ʹÓÃLua¾ç±¾

Nginx»¹Ö§³ÖʹÓÃLua½ÅÔ­À´ÕûÀí»º´æ¡£Ê×ÏÈ£¬ÐèÒªÔÚNginxÖÐ×°ÖÃlua-nginx-moduleÄ £¿é¡£½Ó×Å£¬ÔÚnginx.confÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº

http {
 ...
 
 proxy_cache_path /path/to/cache levels=1:2 keys_zone=cache_zone:10m max_size=10g inactive=60m;
 
 server {
     ...
     
     location ~ /purge(/.*) {
         set $key $1$is_args$args;
         access_by_lua_file /path/to/purge.lua;
     }
 }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÉèÖÃÖУ¬access_by_lua_fileÖ¸Áî»áÖ¸¶¨Ò»¸öLua¾ç±¾ÎļþÀ´´¦ÀíÕûÀí»º´æµÄÂß¼­¡£ÔÚ/purge.luaÎļþÖУ¬Ìí¼ÓÒÔÏ´úÂ룺

local cache_zone = ngx.shared.cache_zone
ngx.req.read_body()
local key = ngx.var.key
ngx.say("Purging cache for key: " .. key)

cache_zone:delete(key)

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУ¬ngx.shared.cache_zoneÌåÏÖ´ÓNginxÖлñȡ֮ǰ½ç˵µÄ»º´æÇøÓò¡£ngx.var.keyÌåÏÖ´ÓNginxÖлñÈ¡ÕûÀí»º´æµÄ·¾¶£¬È»ºóʹÓÃcache_zone:delete(key)À´É¾³ý¶ÔÓ¦µÄ»º´æ¡£

½áÂÛ£º

ͨ¹ýÒÔÉÏÊÃ÷ÈÕ½ÉèÖã¬ÎÒÃÇ¿ÉÒÔÕûÀíNginx»º´æ£¬°ü¹ÜÓû§Äܹ»ÊµÊ±¿´µ½×îеÄÄÚÈÝ¡£Ê¹Óûº´æÄܹ»ÏÔÖøÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂʺÍÓû§ÌåÑ飬¿ÉÊÇ»º´æÓâÆÚºóҪʵʱÕûÀí£¬ÒÔÃâ¸øÓû§Ìṩ¹ýʱµÄÄÚÈÝ¡£ÎÞÂÛÊÇÊÖ¶¯ÕûÀí¡¢Ê¹ÓÃNginxÌṩµÄÄ £¿éÕÕ¾ÉʹÓÃLua¾ç±¾£¬Ñ¡ÔñÊʺÏ×Ô¼ºµÄÒªÁìÀ´´¦Àí»º´æÕûÀíÊǺÜÊÇÖ÷ÒªµÄ£¬ÒÔ±ãÌá¸ßÍøÕ¾µÄÐÔÄܺͿÉÓÃÐÔ¡£

²Î¿¼×ÊÁÏ£º

Nginx documentation, ngx_http_proxy_module

Nginx documentation, ngx_cache_purge_module

OpenResty documentation, lua-nginx-module

ÒÔÉϾÍÊÇNginx»º´æÕûÀíÉèÖÃÊÃ÷ÈÕ½£¬ÓÅ»¯ÍøÕ¾ÄÚÈݸüеÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ