NginxЧÀÍÆ÷µÄ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´Ïêϸ˵Ã÷
nginxЧÀÍÆ÷µÄ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´Ïêϸ˵Ã÷
СÐò£º
ÔÚµ±½ñ»¥ÁªÍøÓ¦ÓÃÖУ¬¸ß²¢·¢ºÍ¸ß¿ÉÓÃÐÔÊǺÜÊÇÖ÷ÒªµÄ¡£ÎªÁË°ü¹ÜϵͳµÄÎȹÌÐÔ£¬ÎÒÃÇÐèÒª¶ÔЧÀÍÆ÷µÄ¿µ½¡×´Ì¬¾ÙÐмà¿ØºÍ¼ì²é£¬²¢Äܹ»×Ô¶¯¾ÙÐйÊÕϻָ´¡£±¾ÎĽ«Ïêϸ˵Ã÷ÔõÑùʹÓÃNginxЧÀÍÆ÷¾ÙÐпµ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´£¬²¢¸ø³öÏà¹ØµÄ´úÂëʾÀý¡£
Nginx¿µ½¡¼ì²éÔÀí
ÔÚNginxÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýupstreamÄ£¿éÀ´½ç˵һ×éºó¶ËЧÀÍÆ÷£¬Nginx»áƾ֤һ¶¨µÄË㷨ΪÇëÇó·ÖÅɵ½²î±ðµÄЧÀÍÆ÷ÉÏ¡£ÔÚÕâ¸öÀú³ÌÖУ¬Nginx¿ÉÒÔͨ¹ý¼ì²éºó¶ËЧÀÍÆ÷µÄ¿µ½¡×´Ì¬À´¶¯Ì¬µ÷½âÇëÇóµÄ·ÖÅÉ¡£³£ÓõĿµ½¡¼ì²é·½·¨ÓÐÒÔÏÂÁ½ÖÖ£º
1.1 ×Ô¶¯¿µ½¡¼ì²é£º
Nginx»áÖÜÆÚÐԵķ¢ËÍÇëÇ󵽺ó¶ËЧÀÍÆ÷£¬È»ºóƾ֤·µ»ØµÄ״̬ÂëÀ´ÅжÏЧÀÍÆ÷µÄ¿µ½¡×´Ì¬¡£ÈôÊÇ·µ»ØµÄ״̬ÂëÌåÏÖЧÀÍÆ÷Õý³££¬Nginx»á½«¸ÃЧÀÍÆ÷¼ÓÈëµ½¿ÉÓÃЧÀÍÆ÷ÁбíÖУ»ÈôÊÇ·µ»ØµÄ״̬ÂëÌåÏÖЧÀÍÆ÷Òì³££¬Nginx»á½«¸ÃЧÀÍÆ÷´Ó¿ÉÓÃЧÀÍÆ÷ÁбíÖÐÒƳý¡£
1.2 ±»¶¯¿µ½¡¼ì²é£º
µ±NginxÏòºó¶ËЧÀÍÆ÷·¢ËÍÇëÇóÇÒÅþÁ¬³¬Ê±»òÕßÇëÇóʧ°Üʱ£¬Nginx»áÅжϸÃЧÀÍÆ÷Ϊ²»¿ÉÓÃЧÀÍÆ÷²¢½«Æä´Ó¿ÉÓÃЧÀÍÆ÷ÁбíÖÐÒƳý¡£µ±ÓÐеÄÇëÇóµÖ´ïʱ£¬Nginx»áʵÑéÅþÁ¬ÆäËû¿ÉÓÃЧÀÍÆ÷¡£
Nginx¿µ½¡¼ì²éÉèÖÃʾÀý
ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýupstreamÄ£¿éÖеÄhealth_check²ÎÊýÀ´¿ªÆô¿µ½¡¼ì²é¹¦Ð§£¬²¢ÉèÖÃÏà¹ØµÄ¿µ½¡¼ì²éÏî¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
http {
µã»÷ÏÂÔØ¡°àËÃûÌÃÊý¾Ý»Ö¸´¾Þ½³¡±£»
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; # ¿ªÆô¿µ½¡¼ì²é¹¦Ð§ health_check; # ÉèÖÿµ½¡¼ì²éÏî health_check_timeout 3s; health_check_interval 10s; health_check_http_2xx; health_check_http_3xx; } server { listen 80; location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÊöµÄÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪbackendµÄupstream×飬²¢ÉèÖÃÁËÈý¸öºó¶ËЧÀÍÆ÷¡£Í¨¹ýÉèÖÃhealth_check²ÎÊýΪ¿ªÆô£¬¿ªÆôÁË¿µ½¡¼ì²é¹¦Ð§¡£¿µ½¡¼ì²éÏîµÄÏêϸÉèÖÃÈçÏ£º
health_check_timeout£ºÉèÖÿµ½¡¼ì²éµÄ³¬Ê±Ê±¼ä¡£
health_check_interval£ºÉèÖÿµ½¡¼ì²éµÄ¾àÀëʱ¼ä¡£
health_check_http_2xx£ºµ±·µ»ØµÄ״̬ÂëΪ2xx£¨Èç200£©Ê±£¬ÌåÏÖЧÀÍÆ÷Õý³£¡£
health_check_http_3xx£ºµ±·µ»ØµÄ״̬ÂëΪ3xx£¨Èç301¡¢302£©Ê±£¬ÌåÏÖЧÀÍÆ÷Õý³£¡£
Nginx×Ô¶¯¹ÊÕϻָ´ÉèÖÃʾÀý
³ýÁË¿µ½¡¼ì²é£¬Nginx»¹ÌṩÁË×Ô¶¯¹ÊÕϻָ´µÄ¹¦Ð§¡£µ±ºó¶ËЧÀÍÆ÷±»Åжϲ»¿ÉÓÃʱ£¬Nginx»á×Ô¶¯½«Æä´Ó¿ÉÓÃЧÀÍÆ÷ÁбíÖÐÒƳý£¬²¢ÔÚÒ»¶Îʱ¼äºóÔٴξÙÐпµ½¡¼ì²é¡£Èôºó¶ËЧÀÍÆ÷ͨ¹ý¿µ½¡¼ì²é£¬Nginx»áÔٴν«Æä¼ÓÈë¿ÉÓÃЧÀÍÆ÷ÁÐ±í£¬´Ó¶øʵÏÖ×Ô¶¯¹ÊÕϻָ´¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
http {
µã»÷ÏÂÔØ¡°àËÃûÌÃÊý¾Ý»Ö¸´¾Þ½³¡±£»
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; # ¿ªÆô¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´¹¦Ð§ health_check; max_fails 3; fail_timeout 30s; # ÉèÖÿµ½¡¼ì²éÏî health_check_timeout 3s; health_check_interval 10s; health_check_http_2xx; health_check_http_3xx; } server { listen 80; location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÊöµÄÉèÖÃÖУ¬ÎÒÃÇÌí¼ÓÁËmax_failsºÍfail_timeoutÁ½¸ö²ÎÊýÓÃÓÚÉèÖÃ×Ô¶¯¹ÊÕϻָ´µÄ¹¦Ð§¡£
max_fails£ºÉèÖÃÔÚһ׼ʱ¼äÄÚ£¨ÓÉfail_timeout²ÎÊýÖ¸¶¨£©ÄÚµÄ×î´óʧ°Ü´ÎÊý¡£Ä¬ÈÏÇéÐÎÏ£¬Îª1´Î¡£
fail_timeout£ºÉèÖùÊÕϻָ´µÄ³¬Ê±Ê±¼ä¡£
µ±Ä³¸öºó¶ËЧÀÍÆ÷µÄʧ°Ü´ÎÊýµÖ´ïmax_failsÉèÖõÄֵʱ£¬Nginx»á½«¸ÃЧÀÍÆ÷´Ó¿ÉÓÃЧÀÍÆ÷ÁбíÖÐÒƳý£¬²¢ÔÚfail_timeoutÉèÖõij¬Ê±Ê±¼äºóÔٴξÙÐпµ½¡¼ì²é¡£ÈôÊǸÃЧÀÍÆ÷ͨ¹ý¿µ½¡¼ì²é£¬Nginx»áÔٴν«Æä¼ÓÈëµ½¿ÉÓÃЧÀÍÆ÷ÁÐ±í¡£
½áÓ
NginxЧÀÍÆ÷µÄ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´¹¦Ð§Äܹ»ÓÐÓÃÌáÉýϵͳµÄÎȹÌÐԺͿÉÓÃÐÔ¡£Í¨¹ýÉÏÊöµÄÉèÖÃʾÀý£¬ÎÒÃÇ¿ÉÒÔÇáËɵØÉèÖÃNginxʵÏÖ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´¹¦Ð§¡£Ï£Íû±¾ÎĶԸ÷ÈËÏàʶºÍʹÓÃNginxЧÀÍÆ÷µÄ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´¹¦Ð§ÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginxЧÀÍÆ÷µÄ¿µ½¡¼ì²éºÍ×Ô¶¯¹ÊÕϻָ´Ïêϸ˵Ã÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡