Nginx·´ÏòÊðÀí»º´æÉèÖã¬ÊµÏÖ¾²Ì¬ÍøÒ³»á¼û¼ÓËÙ
nginx·´ÏòÊðÀí»º´æÉèÖã¬ÊµÏÖ¾²Ì¬ÍøÒ³»á¼û¼ÓËÙ
¼ò½é£º
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬»á¼ûËÙÂʳÉΪÁËÍøÕ¾ÔËÓªÖкÜÊÇÖ÷ÒªµÄÒ»¸öÒòËØ¡£ÎªÁËÌá¸ßÍøÒ³µÄ»á¼ûËÙÂÊ£¬ÎÒÃÇ¿ÉÒÔʹÓÃNginx·´ÏòÊðÀí»º´æÊÖÒÕÀ´ÊµÏÖÍøÒ³µÄ¼ÓËÙ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÉèÖ÷´ÏòÊðÀí»º´æÀ´ÊµÏÖ¾²Ì¬ÍøÒ³µÄ¼ÓËÙ¡£
Nginx·´ÏòÊðÀí»º´æÉèÖãº
×°ÖÃNginx£º
Ê×ÏÈÐèҪװÖÃNginxЧÀÍÆ÷£¬¿ÉÒÔͨ¹ýapt-get¡¢yumµÈ°ü¹ÜÀí¹¤¾ß¾ÙÐÐ×°Öá£
ÉèÖÃNginx·´ÏòÊðÀí£º
·¿ªNginxµÄÉèÖÃÎļþ£¨Ò»Ñùƽ³£Î»ÓÚ/etc/nginx/nginx.conf£©£¬ÕÒµ½server²¿·ÖµÄÉèÖá£ÔÚserverÏÂÌí¼ÓÈçÏ´úÂ룺
location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 304 12h; proxy_cache_valid any 5m; proxy_cache_key $host$scheme$request_uri; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; proxy_cache_background_update on; proxy_cache_lock on; proxy_cache_lock_timeout 5s; proxy_cache_lock_age 10s; proxy_ignore_headers Cache-Control; proxy_ignore_headers Set-Cookie; proxy_ignore_headers Expires; add_header X-Proxy-Cache $upstream_cache_status; }
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍÉÏÊöÉèÖãº
proxy_pass£ºÖ¸¶¨·´ÏòÊðÀíµÄºó¶ËЧÀÍÆ÷µØµã£¬¿ÉÒÔÊÇÍâµØµÄ¶Ë¿ÚºÅ»òÓòÃû¡£
proxy_cache£ºÖ¸¶¨»º´æµÄÃû³Æ£¬ÔÚNginxµÄÉèÖÃÎļþÖÐÐèÒªÁíÍâ½ç˵¡£
proxy_cache_valid£ºÖ¸¶¨»º´æÊý¾ÝµÄÓÐÓÃʱ¼ä£¬ÕâÀïÉèÖÃ200ºÍ304״̬ÂëµÄ»º´æʱ¼äΪ12Сʱ£¬ÆäËû״̬ÂëµÄ»º´æʱ¼äΪ5·ÖÖÓ¡£
proxy_cache_key£ºÖ¸¶¨»º´æÊý¾ÝµÄΨһ±êʶ£¬ÕâÀïʹÓÃÓòÃû¡¢ÐæźÍÇëÇóURIÀ´ÌìÉú¡£
proxy_cache_use_stale£ºÉèÖõ±ºó¶ËЧÀÍÆ÷¹ýʧʱÊÇ·ñʹÓÃÓâÆڵĻº´æ¡£
proxy_cache_background_update£ºÉèÖÃÊÇ·ñÔÚºǫ́¸üлº´æÊý¾Ý¡£
proxy_cache_lock£ºÉèÖÃÊÇ·ñÆôÓûº´æËø£¬ÒÔ×èÖ¹¶à¸öÇëÇóͬʱ¸üлº´æ¡£
proxy_ignore_headers£ºÉèÖúöÂÔµÄÏìӦͷ£¬ÕâÀïºöÂÔÁËCache-Control¡¢Set-CookieºÍExpires¡£
add_header£ºÌí¼ÓÏìӦͷ£¬ÓÃÓÚÏÔʾ»º´æÖÀÖÐ״̬¡£
ÉèÖÃNginx»º´æ£º
·¿ªNginxµÄÖ÷ÉèÖÃÎļþ£¬ÔÚhttp²¿·ÖµÄÉèÖÃÖÐÌí¼ÓÈçÏ´úÂ룺
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:50m inactive=24h max_size=500m;
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍÉÏÊöÉèÖãº
proxy_cache_path£ºÖ¸¶¨»º´æµÄ·¾¶¡¢»º´æ¼¶±ð¡¢»º´æÇøÓò¡¢²»»îԾʱ¼äºÍ×î´ó»º´æ¾Þϸ¡£
ÖØÆôNginxЧÀÍÆ÷£º
Íê³ÉÒÔÉÏÉèÖúó£¬ÉúÑIJ¢Í˳öÉèÖÃÎļþ£¬Í¨¹ýÒÔÏÂÏÂÁîÖØÆôNginxЧÀÍÆ÷£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
ʾÀýЧ¹û£º
¾ÓÉÉÏÊöÉèÖú󣬵±Óû§»á¼ûÍøվʱ£¬Nginx½«»áƾ֤»º´æµÄÉèÖþÙÐÐÅжϣ¬ÈôÊÇ»º´æÖб£´æ¶ÔÓ¦µÄ¾²Ì¬ÍøÒ³Îļþ£¬½«Ö±½Ó·µ»Ø»º´æÊý¾Ý£¬´Ó¶øʵÏÖÁËÍøÒ³µÄ¼ÓËÙ¡£
½áÂÛ£º
ͨ¹ýNginx·´ÏòÊðÀí»º´æµÄÉèÖã¬ÎÒÃÇ¿ÉÒÔ¿ìËÙʵÏÖ¾²Ì¬ÍøÒ³µÄ»á¼û¼ÓËÙ¡£Í¨¹ýºÏÀíµÄÉèÖûº´æÓÐÓÃÆÚ¡¢»º´æ¼üÒÔ¼°»º´æ·¾¶µÈ²ÎÊý£¬¿ÉÒÔ½øÒ»²½ÌáÉýÍøÒ³µÄ»á¼ûËÙÂʺÍÓû§ÌåÑ顣ͬʱ£¬Nginx·´ÏòÊðÀí»º´æÒ²¿ÉÒÔ¼õÇáºó¶ËЧÀÍÆ÷µÄ¸ºÔØ£¬Ìá¸ßϵͳµÄÎȹÌÐԺͿɿ¿ÐÔ¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNginx·´ÏòÊðÀí»º´æÉèÖÃÀ´ÊµÏÖ¾²Ì¬ÍøÒ³µÄ»á¼û¼ÓËÙ¡£Í¨¹ýÊʵ±µÄÉèÖ㬿ÉÒÔÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂÊ£¬¼õÇáºó¶ËЧÀÍÆ÷µÄ¸ºÔØ¡£Ï£Íû¶ÁÕß¿ÉÒÔƾ֤±¾ÎÄÌṩµÄʾÀý´úÂ룬ÍŽá×ÔÉíÐèÇó¾ÙÐÐÉèÖ㬴Ӷø¸ÄÉÆÍøÕ¾µÄÐÔÄܺÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇNginx·´ÏòÊðÀí»º´æÉèÖã¬ÊµÏÖ¾²Ì¬ÍøÒ³»á¼û¼ÓËÙµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡