NginxÔõÑùʵÏÖHTTPÇëÇóµÄ»º´æ¿ØÖÆÉèÖÃ
NginxÔõÑùʵÏÖHTTPÇëÇóµÄ»º´æ¿ØÖÆÉèÖÃ
Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÓµÓÐÇ¿Ê¢µÄ»º´æÖÎÀíºÍ¿ØÖƹ¦Ð§£¬¿ÉÒÔͨ¹ýÉèÖÃʵÏÖ¶ÔHTTPÇëÇóµÄ»º´æ¿ØÖÆ¡£±¾ÎĽ«Õë¶ÔNginxÔõÑùʵÏÖHTTPÇëÇóµÄ»º´æ¿ØÖÆÉèÖþÙÐÐÏêϸÏÈÈÝ£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Nginx»º´æÉèÖøÅÊö
NginxµÄ»º´æÉèÖÃÖ÷Ҫͨ¹ýproxy_cacheÄ£¿éʵÏÖ£¬¸ÃÄ£¿éÌṩÁ˸»ºñµÄÖ¸ÁîºÍ²ÎÊý£¬¿ÉÒÔÓÐÓõؿØÖÆ»º´æµÄÐÐΪ¡£ÔÚÉèÖûº´æ֮ǰ£¬ÐèÒªÏÈÔÚNginxµÄÉèÖÃÎļþÖмÓÔØproxy_cacheÄ£¿é£¬ÏêϸµÄÖ¸ÁîΪ£º
load_module modules/ngx_http_proxy_module.so;
µÇ¼ºó¸´ÖÆ
´ËÖ¸Á¼ÓÔØNginxµÄproxy_cacheÄ£¿é£¬Ê¹µÃÎÒÃÇ¿ÉÒÔÔÚÉèÖÃÎļþÖÐʹÓÃÏà¹ØµÄ»º´æ¿ØÖÆÖ¸Áî¡£
¶þ¡¢»º´æ¿ØÖÆÖ¸ÁîÏê½â
proxy_cache_path
proxy_cache_pathÖ¸ÁîÓÃÓÚ½ç˵»º´æ·¾¶ºÍÏà¹ØµÄÉèÖòÎÊý£¬ÀýÈ绺´æ´æ´¢Â·¾¶¡¢»º´æ¾Þϸ¡¢»º´æÕ½ÂԵȡ£ÏêϸµÄÓ÷¨ÈçÏ£º
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪmy_cacheµÄ»º´æÇøÓò£¬»º´æ·¾¶Îª/data/nginx/cache£¬×î´ó»º´æ¾ÞϸΪ10GB£¬»º´æÓâÆÚʱ¼äΪ60·ÖÖÓ¡£ÐèҪעÖصÄÊÇ£¬ÉèÖòÎÊýÐèƾ֤ÏÖʵÐèÇó¾ÙÐе÷½â¡£
proxy_cache
proxy_cacheÖ¸ÁîÓÃÓÚÆôÓûº´æ£¬²¢ÉèÖÃʹÓõĻº´æÇøÓò£¬¿ÉÒÔÔÚlocation¿éÖоÙÐÐÉèÖã¬ÀýÈ磺
location / { proxy_cache my_cache; proxy_cache_valid 200 304 5m; proxy_cache_valid 301 302 1h; proxy_cache_key $host$uri$is_args$args; proxy_cache_use_stale error timeout 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_revalidate on; proxy_cache_min_uses 3; proxy_cache_bypass $http_x_token; proxy_cache_methods GET HEAD; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇÆôÓÃÁËÃûΪmy_cacheµÄ»º´æÇøÓò£¬²¢ÉèÖÃÁ˲î±ðÏìӦ״̬ÂëµÄ»º´æÓÐÓÃʱ¼ä¡¢»º´æ¼ü¡¢»º´æ¸üÐÂÕ½ÂԵȲÎÊý¡£ÕâЩ²ÎÊý¿ÉÒÔƾ֤ÏêϸµÄ»º´æÐèÇó¾ÙÐÐÎÞаÉèÖá£
proxy_ignore_headers
proxy_ignore_headersÖ¸ÁîÓÃÓÚÖ¸¶¨NginxÔÚ¾ÙÐлº´æʱÐèÒªºöÂÔµÄHTTPÏìӦͷ£¬ÀýÈ磺
proxy_ignore_headers Cache-Control Set-Cookie;
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬ÎÒÃÇÒªÇóNginxÔÚ¾ÙÐлº´æʱºöÂÔCache-ControlºÍSet-CookieÏìӦͷ£¬È·±£»º´æµÄÒ»ÖÂÐÔºÍÓÐÓÃÐÔ¡£
proxy_cache_lock
proxy_cache_lockÖ¸ÁîÓÃÓÚ¿ØÖÆ»º´æÄÚÈݵIJ¢·¢»á¼û£¬¿ÉÒÔÓÐÓÃ×èÖ¹»º´æ»÷´©¡¢Ñ©±ÀµÈÎÊÌ⣬ÀýÈ磺
proxy_cache_lock on; proxy_cache_lock_timeout 5s;
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬ÎÒÃÇÆôÓÃÁË»º´æËø£¬²¢ÉèÖÃÁËÒ»¸ö5ÃëµÄ³¬Ê±Ê±¼ä£¬Áè¼Ý¸Ã³¬Ê±Ê±¼äµÄÇëÇ󽫼ÌÐø»á¼ûºó¶ËЧÀÍÆ÷ÒÔ¸üлº´æÄÚÈÝ¡£
Èý¡¢´úÂëʾÀý
»ùÓÚÒÔÉϵĻº´æ¿ØÖÆÖ¸ÁÎÒÃÇ¿ÉÒÔ±àдһ¸öÍêÕûµÄNginxÉèÖÃʾÀý£¬ÊµÏÖ¶ÔHTTPÇëÇóµÄ»º´æ¿ØÖÆ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£º
load_module modules/ngx_http_proxy_module.so; http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_valid 200 304 5m; proxy_cache_valid 301 302 1h; proxy_cache_key $host$uri$is_args$args; proxy_cache_use_stale error timeout 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_revalidate on; proxy_cache_min_uses 3; proxy_cache_bypass $http_x_token; proxy_cache_methods GET HEAD; proxy_ignore_headers Cache-Control Set-Cookie; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀýÖУ¬ÎÒÃÇÊ×ÏȼÓÔØÁËngx_http_proxy_moduleÄ£¿é£¬È»ºó½ç˵ÁËÒ»¸öÃûΪmy_cacheµÄ»º´æÇøÓò£¬²¢ÔÚserver¿éÖÐÉèÖÃÁËÒ»¸öÊðÀílocation£¬²¢ÆôÓÃÁË»º´æºÍÏìÓ¦µÄ»º´æ¿ØÖÆÖ¸Áî¡£µ±Óû§»á¼ûexample.comʱ£¬Nginx½«»áƾ֤ÉèÖõĻº´æ¹æÔò¾ÙÐлº´æÖÎÀíºÍ¿ØÖÆ¡£
ËÄ¡¢×ܽá
ͨ¹ýÒÔÉϵÄÏÈÈݺÍʾÀý£¬ÎÒÃÇÏêϸÏàʶÁËNginxÔõÑùʵÏÖHTTPÇëÇóµÄ»º´æ¿ØÖÆÉèÖ㬲¢Õë¶Ôproxy_cacheÄ£¿éÌṩµÄÏà¹ØÖ¸Áî¾ÙÐÐÁËÏêϸڹÊͺÍÑÝʾ¡£ºÏÀíµÄ»º´æÉèÖÿÉÒÔ´ó´óÌá¸ßÍøÕ¾µÄ»á¼ûËÙÂʺÍÐÔÄÜ£¬¼õÇáºó¶ËЧÀÍÆ÷µÄѹÁ¦£¬ÊµÏÖ¸üºÃµÄÓû§ÌåÑé¡£Òò´Ë£¬ÔÚÏÖʵµÄWebÓ¦Óÿª·¢ÖУ¬ºÏÀíʹÓÃNginxµÄ»º´æ¿ØÖƹ¦Ð§ÊǺÜÊÇÖ÷ÒªµÄ¡£
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖHTTPÇëÇóµÄ»º´æ¿ØÖÆÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡