Nginx»º´æÕûÀíÉèÖ㬼á³ÖÍøÕ¾ÄÚÈݸüÐÂ
nginx»º´æÕûÀíÉèÖ㬼á³ÖÍøÕ¾ÄÚÈݸüÐÂ
ÔÚ½¨ÉèÒ»¸öÍøվʱ£¬ÎÒÃǾ³£»áʹÓÃNginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬ÓÃÓÚ¼ÓËÙÍøÕ¾»á¼û»ººÍ´æ¾²Ì¬Îļþ¡£¿ÉÊÇ£¬µ±ÎÒÃǸüÐÂÁËÍøÕ¾ÄÚÈÝʱ£¬ÐèҪʵʱÕûÀíNginxµÄ»º´æ£¬ÒÔ¼á³ÖÓû§»á¼ûµÄÊÇ×îеÄÄÚÈÝ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginx»º´æÕûÀí£¬ÒÔ¼á³ÖÍøÕ¾ÄÚÈݵĸüС£
Ò»¡¢ÉèÖûº´æ·¾¶
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÉèÖÃNginxµÄ»º´æ·¾¶¡£·¿ªNginxµÄÉèÖÃÎļþ£¬Ò»Ñùƽ³£ÊÇ/etc/nginx/nginx.conf»ò/etc/nginx/conf.d/default.conf£¬ÕÒµ½http¶Î£¬Ìí¼ÓÒÔÏÂÉèÖãº
http { ... proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { ... } ... }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«»º´æÎļþ´æ´¢ÔÚ/var/cache/nginxĿ¼Ï£¬²¢ÉèÖÃÁË»º´æ¾ÞϸΪ10GB£¬»º´æÓâÆÚʱ¼äΪ60·ÖÖÓ¡£ÐèҪƾ֤ÏÖÕæÏàÐξÙÐе÷½â¡£
¶þ¡¢ÉèÖûº´æÕûÀí½Ó¿Ú
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖÃÒ»¸ö½Ó¿ÚÓÃÓÚÕûÀíNginxµÄ»º´æ¡£
ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÕÒµ½ÐèÒªÕûÀí»º´æµÄµØ·½£¬ºÃ±ÈÒ»¸öPOSTÇëÇó£º
http { ... server { ... location /update { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # ÕûÀí»º´æ proxy_cache_purge my_cache GET POST; } ... } ... }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬location /updateÓÃÓÚÆ¥ÅäÐèÒª¸üеÄURL·¾¶£¬ºÃ±È/update¡£proxy_cache_purgeÖ¸ÁîÓÃÓÚÕûÀí»º´æ£¬²ÎÊýGET POSTÌåÏÖͬʱÕûÀíGETºÍPOSTÇëÇóµÄ»º´æ¡£¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐе÷½â¡£
Èý¡¢×°ÖÃPurgeÄ£¿é
NginxµÄĬÈÏ×°ÖÃÖв¢²»°üÀ¨proxy_cache_purgeÄ£¿é£¬ÎÒÃÇÐèÒªÊÖ¶¯×°Öá£
ʹÓÃÒÔÏÂÏÂÁî×°ÖÃNginxµÄÄ£¿éÖÎÀí¹¤¾ß£º
yum install -y yum-utils
µÇ¼ºó¸´ÖÆ
ʹÓÃÒÔÏÂÏÂÁîÌí¼ÓNginxµÄÀ©Õ¹Ô´£º
yum-config-manager --add-repo=https://nginx.org/packages/centos/7/x86_64/
µÇ¼ºó¸´ÖÆ
×îºó£¬Ê¹ÓÃÒÔÏÂÏÂÁî×°ÖÃNginxµÄnginx-module-cache-purgeÄ£¿é£º
yum install -y nginx-module-cache-purge
µÇ¼ºó¸´ÖÆ
ËÄ¡¢ÖØÆôNginxЧÀÍ
Íê³ÉÒÔÉÏÉèÖúó£¬ÐèÒªÖØÆôNginxЧÀÍʹÉèÖÃÉúЧ¡£Ê¹ÓÃÒÔÏÂÏÂÁîÖØÆôNginx£º
systemctl restart nginx
µÇ¼ºó¸´ÖÆ
Îå¡¢ÕûÀí»º´æµÄ·½·¨
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûÕûÀí»º´æµÄ½Ó¿ÚÀ´ÊµÏÖÕûÀíNginxµÄ»º´æ¡£ºÃ±È£¬ÈôÊÇÎÒÃÇÒªÕûÀí/update·¾¶µÄ»º´æ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
curl -X POST http://yourdomain.com/update
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í»á´¥·¢NginxÕûÀí/update·¾¶ÏµĻº´æ¡£
Áù¡¢×Ô¶¯ÕûÀí»º´æ
ΪÁË°ü¹ÜÍøÕ¾ÄÚÈݵĸüУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý°´ÆÚÕûÀí»º´æµÄ·½·¨£¬×Ô¶¯¼á³ÖNginxÖеĻº´æÓëÏÖʵÍøÕ¾ÄÚÈݵÄÒ»ÖÂÐÔ¡£
ÎÒÃÇ¿ÉÒÔʹÓÃ׼ʱʹÃü¹¤¾ß£¨ÈçCron£©À´°´ÆÚÖ´ÐÐÕûÀí»º´æµÄÏÂÁî¡£ºÃ±È£¬ÉèÖÃÌìÌìÆÆÏþ4µãÕûÀíÒ»´Î»º´æ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî±à¼×¼Ê±Ê¹Ãü£º
crontab -e
µÇ¼ºó¸´ÖÆ
È»ºóÌí¼ÓÒÔÏÂÒ»ÐÐÏÂÁ
0 4 * * * curl -X POST http://yourdomain.com/update >> /tmp/nginx_cache_clear.log 2>&1
µÇ¼ºó¸´ÖÆ
ÉúÑIJ¢Í˳ö±à¼Æ÷£¬ÕâÑùÌìÌìÆÆÏþ4µã¾Í»á´¥·¢Ò»´ÎÕûÀí»º´æµÄ²Ù×÷£¬²¢½«Ö´ÐÐЧ¹ûÉúÑÄÔÚ/tmp/nginx_cache_clear.logÎļþÖС£
×ܽá
ͨ¹ýÒÔÉÏÉèÖã¬ÎÒÃÇ¿ÉÒÔʵÏÖNginx»º´æµÄ×Ô¶¯ÕûÀí£¬¼á³ÖÍøÕ¾ÄÚÈݵĸüС£Í¬Ê±£¬ÎÒÃÇÒ²¿ÉÒÔƾ֤ÐèÒªÎÞаµ÷½â»º´æ·¾¶ºÍÕûÀí·½·¨£¬ÒÔ˳Ӧ²î±ðµÄÍøÕ¾ÐèÇó¡£
ÒÔÉϾÍÊÇNginx»º´æÕûÀíÉèÖ㬼á³ÖÍøÕ¾ÄÚÈݸüеÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡