Nginx»º´æÕûÀíÉèÖ㬸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´
nginx»º´æÕûÀíÉèÖ㬸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´
СÐò£º
Ëæ×ÅÍøÕ¾µÄÉú³¤ºÍÄÚÈݵĸüУ¬ÎªÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂʺÍÓû§ÌåÑ飬Ðí¶àÍøÕ¾¶¼»áʹÓÃNginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷¡£NginxµÄ»º´æ¹¦Ð§¿ÉÒÔ´ó´óÌáÉýÍøÕ¾µÄÐÔÄÜ£¬È»¶øÔÚ¸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´µÄÀú³ÌÖУ¬ÎÒÃÇÐèÒªÊÖ¶¯ÕûÀíNginx»º´æ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginx¾ÙÐлº´æÕûÀí£¬ÒÔ¼°ÔõÑù×Ô¶¯¸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´¡£
Ò»¡¢Nginx»º´æÕûÀíÉèÖÃ
½¨ÉèÒ»¸öÓÃÓÚÎüÊÕ»º´æÕûÀíÇëÇóµÄ·¾¶£¬ºÃ±È/cache/clear¡£ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº
location /cache/clear {
allow 127.0.0.1; deny all; proxy_cache_purge CACHE_NAME "$scheme$request_method$host$request_uri"; return 204;
µÇ¼ºó¸´ÖÆ
}
ÐÞ¸ÄNginx»º´æµÄÉèÖÃÎļþ£¬Ìí¼ÓÒ»¸öеĻº´æÇø¿é£¬²¢ÃüÃûΪCACHE_NAME¡£ÔÚÕâ¸ö»º´æÇø¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
proxy_cache_path /path/to/cache levels=1:2 keys_zone=CACHE_NAME:10m max_size=10g inactive=60m use_temp_path=off;
proxy_cache_key “$scheme$request_method$host$request_uri”;
ÖØÆôNginxЧÀÍÆ÷£¬Ê¹ÉèÖÃÎļþÉúЧ¡£
¶þ¡¢¸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´
ÔÚЧÀÍÆ÷ÉϽ¨ÉèÒ»¸ö´æ·Å¾²Ì¬×ÊÔ´µÄĿ¼£¬ºÃ±È/static¡£
ÔÚNginxµÄÉèÖÃÎļþÖУ¬Ìí¼ÓÒ»¸öÓÃÓÚ´¦Àí¾²Ì¬×ÊÔ´µÄlocation¡£
location /static {
root /path/to/static; expires max; add_header Cache-Control public;
µÇ¼ºó¸´ÖÆ
}
ÉèÖÃÒ»¸ö½ÅÔÀ´¸üÐÂÍøÕ¾µÄ¾²Ì¬×ÊÔ´¡£¿ÉÒÔʹÓÃshell¾ç±¾»òÕßÆäËû¾ç±¾ÓïÑÔÀ´ÊµÏÖ¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄ¾ç±¾£º
#!/bin/bash # ¾²Ì¬×ÊԴĿ¼ STATIC_DIR="/path/to/static" # »º´æÕûÀíURL CACHE_CLEAR_URL="http://localhost/cache/clear" # ½øÈ뾲̬×ÊԴĿ¼ cd $STATIC_DIR # ÀÈ¡×îеĴúÂë git pull # ÕûÀíNginx»º´æ curl -X PURGE $CACHE_CLEAR_URL # ¸´Öƾ²Ì¬×ÊÔ´µ½NginxµÄĿ¼ cp -R ./* /path/to/nginx/static # ÖØÆôNginxЧÀÍÆ÷ service nginx restart
µÇ¼ºó¸´ÖÆ
ÒÔÉϵľ籾»áÏÈÇл»µ½¾²Ì¬×ÊԴĿ¼£¬È»ºóͨ¹ýgitÏÂÁîÀÈ¡×îеĴúÂë¡£½Ó×Å£¬Ëü»áʹÓÃcurl·¢ËÍ»º´æÕûÀíÇëÇ󣬽«NginxµÄ»º´æÇå¿Õ¡£È»ºó£¬Ëü½«Ðµľ²Ì¬×ÊÔ´¸´ÖƵ½NginxµÄĿ¼£¬²¢×îºóÖØÆôNginxЧÀÍÆ÷¡£
ʹÓÃ׼ʱʹÃü»òÆäËû·½·¨À´×Ô¶¯Ö´Ðиüо²Ì¬×ÊÔ´µÄ¾ç±¾¡£ºÃ±È¿ÉÒÔÿСʱִÐÐÒ»´Î£¬È·±£ÍøÕ¾¾²Ì¬×ÊԴʱ¿Ì¼á³Ö×îС£
½áÂÛ£º
ͨ¹ýÉèÖÃNginxµÄ»º´æÕûÀíºÍ¸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´µÄ¾ç±¾£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ¼á³ÖÍøÕ¾µÄÐÔÄܺÍÄÚÈݸüС£ÔÚÍøÕ¾¿ª·¢Àú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÉèÖúÍÓÅ»¯£¬ÒÔÌáÉýÓû§ÌåÑéºÍÍøÕ¾µÄ»á¼ûËÙÂÊ¡£
ÒÔÉϾÍÊÇNginx»º´æÕûÀíÉèÖ㬸üÐÂÍøÕ¾¾²Ì¬×ÊÔ´µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡