ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ·´ÏòÊðÀí»º´æ
ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄ·´ÏòÊðÀí»º´æ
NginxÊÇÒ»¿î¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷Èí¼þ£¬³ýÁË¿ÉÒÔ×÷ΪWebЧÀÍÆ÷À´´¦Öóͷ£¾²Ì¬×ÊÔ´ÇëÇóÍ⣬Nginx»¹¿ÉÒÔ¾ÙÐÐHTTPÇëÇóµÄ·´ÏòÊðÀí¡£·´ÏòÊðÀí¿ÉÒÔ×ÊÖúÎÒÃǽ«¿Í»§¶ËµÄÇëÇóת·¢µ½¶à¸öºó¶ËЧÀÍÆ÷ÉϾÙÐд¦Öóͷ££¬²¢ÇÒ¿ÉÒÔͨ¹ý»º´æÀ´Ìá¸ßÏìÓ¦ËÙÂÊ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚNginxÖÐÉèÖ÷´ÏòÊðÀí»º´æ¡£
×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNginxЧÀÍÆ÷¡£ÔÚLinuxÇéÐÎÏ£¬¿ÉÒÔͨ¹ý°ü¹ÜÀíÆ÷À´¾ÙÐÐ×°Öá£ÒÔDebian/UbuntuΪÀý£¬Ö´ÐÐÒÔÏÂÏÂÁ
sudo apt update sudo apt install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖ÷´ÏòÊðÀí»º´æ
ÔÚ×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒª±à¼NginxµÄÉèÖÃÎļþÀ´ÉèÖ÷´ÏòÊðÀí»º´æ¡£Ä¬ÈÏÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf¡£Ê¹ÓÃÎı¾±à¼Æ÷·¿ª¸ÃÎļþ£¬ÕÒµ½http{}¿é¡£
ÔÚhttp{}¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬/path/to/cacheÊÇ»º´æ·¾¶£¬¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÐ޸ġ£levels=1:2ÌåÏÖ»º´æĿ¼µÄ²ã¼¶¡£keys_zone=my_cache:10mÌåÏÖΪ»º´æ·ÖÅÉ10MµÄÄڴ棬ÓÃÓÚ´æ´¢»º´æµÄÔªÊý¾Ý¡£max_size=10gÌåÏÖ»º´æµÄ×î´ó¾ÞϸΪ10G¡£inactive=60mÌåÏÖ»º´æÎļþÔÚ60·ÖÖÓûÓб»»á¼ûʱ»á±»ÒÔΪÊÇÓâÆڵġ£
È»ºó£¬ÔÚserver{}¿éÄÚÌí¼ÓÒÔÏÂÉèÖãº
proxy_cache my_cache; proxy_cache_valid 200 302 1h; proxy_cache_valid 301 404 10m; proxy_cache_use_stale error timeout updating http_500 http_503;
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬proxy_cache my_cacheÌåÏÖÆôÓÃÃûΪmy_cacheµÄ»º´æ¡£proxy_cache_validÓÃÓÚÉèÖòî±ð״̬ÂëµÄ»º´æÓÐÓÃÆÚʱ¼ä£¬ÀýÈç200ºÍ302״̬ÂëµÄÇëÇ󻺴æÓÐÓÃÆÚΪ1Сʱ¡£proxy_cache_use_staleÓÃÓÚÖ¸¶¨µ±ºó¶ËЧÀÍÆ÷·ºÆð¹ýʧ»ò³¬Ê±Ê±£¬ÊÇ·ñʹÓÃÓâÆڵĻº´æ×÷ΪÏìÓ¦¡£
ÉèÖ÷´ÏòÊðÀí
ÔÚÍê³ÉÁË»º´æµÄÉèÖúó£¬ÎÒÃÇÐèÒªÉèÖ÷´ÏòÊðÀí¡£ÔÚserver{}¿éÄÚ£¬Ìí¼ÓÒÔÏÂÉèÖãº
location / { proxy_pass http://backend_server; proxy_set_header Host $host; proxy_cache_bypass $http_cache_control; proxy_cache_key $uri$is_args$args; }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬proxy_passÖ¸¶¨Á˺ó¶ËЧÀÍÆ÷µÄµØµã¡£proxy_set_headerÓÃÓÚÉèÖÃÊðÀíÇëÇóÖеÄÍ·ÐÅÏ¢£¬ÕâÀïÊǽ«ÇëÇóµÄhostÍ·ÉèÖÃΪĿ½ñÖ÷»ú¡£proxy_cache_bypassÓÃÓÚÖ¸¶¨ÄÄЩÇëÇó²»Ê¹Óûº´æ£¬ÕâÀïÊÇƾ֤ÇëÇóÍ·ÖеÄcache-controlÀ´¾öÒé¡£proxy_cache_keyÓÃÓÚÌìÉú»º´æµÄkey£¬ÕâÀïÊǽ«ÇëÇóµÄURIºÍ²ÎÊý×éºÏ¡£
ÖØÆôNginx²¢²âÊÔ
Íê³ÉÁËÉÏÊöÉèÖúó£¬ÉúÑIJ¢Í˳öÉèÖÃÎļþ¡£È»ºó£¬Í¨¹ýÒÔÏÂÏÂÁîÀ´ÖØÆôNginxЧÀÍ£º
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬Í¨¹ýä¯ÀÀÆ÷»òÏÂÁîÐй¤¾ß·¢ËÍÇëÇó£¬ÊÓ²ìNginx»º´æµÄÊÂÇéÇéÐΡ£
×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÀֳɵØÉèÖÃÁËNginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§¡£Í¨¹ýʹÓûº´æ£¬¿ÉÒÔÏÔÖøÌá¸ßÇëÇóµÄÏìÓ¦ËÙÂÊ£¬¼õÇáºó¶ËЧÀÍÆ÷µÄ¸ºÔØ¡£Í¬Ê±£¬NginxÒ²ÌṩÁËÎÞаµÄÉèÖÃÑ¡Ï¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐе÷½âºÍÓÅ»¯¡£
Ï£Íû±¾ÎĶÔÄãÃ÷È·ºÍʹÓÃNginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§ÓÐËù×ÊÖú¡£ÈôÓÐÈκÎÎÊÌâ»òÒÉ»ó£¬ÇëÔÚ̸ÂÛÇøÁôÑÔ£¬ÎÒ»áÆð¾¢½â´ð¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ·´ÏòÊðÀí»º´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡