Nginx upstreamÉèÖÃÏê½â£¬ÊµÏÖÐÞ¸´ÍøÕ¾¹ÊÕÏ
nginx upstreamÉèÖÃÏê½â£¬ÊµÏÖÐÞ¸´ÍøÕ¾¹ÊÕÏ
СÐò:
Nginx ÊÇÒ»¸ö¸ßÐÔÄܵÄHTTPºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ËüµÄÇ¿Ê¢¹¦Ð§ºÍÎÞаµÄÉèÖÃʹÆä³ÉΪÐí¶àÍøÕ¾ºÍЧÀ͵ÄÀíÏëÑ¡Ôñ¡£ÔÚÍøÕ¾ÔËÓªÀú³ÌÖУ¬ÄÑÃâ»á·ºÆð¹ÊÕϺ͸ºÔØá¯ÁëµÄÎÊÌ⡣ΪÁË°ü¹ÜÍøÕ¾µÄ¿ÉÓÃÐÔºÍÎȹÌÐÔ£¬ÎÒÃÇÐèÒªÕÆÎÕNginx upstreamÉèÖõļ¼ÇÉ¡£±¾ÎĽ«ÏêϸÏÈÈÝNginx upstreamÉèÖõÄÔÀíºÍʹÓ÷½·¨£¬²¢Í¨¹ý´úÂëʾÀýÑÝʾÔõÑùʹÓÃupstreamÉèÖÃʵÏÖÐÞ¸´ÍøÕ¾¹ÊÕϵÄÒªÁì¡£
Ò»¡¢Nginx upstreamÉèÖõÄÔÀí
NginxµÄupstreamÄ£¿éÔÊÐíÎÒÃǽç˵һ×éºó¶ËЧÀÍÆ÷£¬²¢Æ¾Ö¤Ò»¶¨µÄÕ½ÂÔת·¢¿Í»§¶ËÇëÇóµ½ÕâЩºó¶ËЧÀÍÆ÷ÉÏ¡£Í¨¹ýupstreamÉèÖ㬿ÉÒÔʵÏÖ¸ºÔØƽºâºÍ¹ÊÕÏתÒƵȹ¦Ð§¡£Nginxƾ֤ÉèÖõÄÕ½ÂÔ£¬×Ô¶¯Ñ¡Ôñºó¶ËЧÀÍÆ÷£¬²¢½«¿Í»§¶ËÇëÇóת·¢µ½ËùÑ¡µÄЧÀÍÆ÷ÉÏ¡£ÔÚºó¶ËЧÀÍÆ÷¹ÊÕÏʱ£¬NginxÖ§³Ö×Ô¶¯ÌÞ³ý¹ÊÕÏЧÀÍÆ÷£¬²¢½«ÇëÇóÖØзÖÅɵ½ÆäËûÕý³£µÄЧÀÍÆ÷ÉÏ¡£
¶þ¡¢Nginx upstreamÉèÖõÄʹÓ÷½·¨
½ç˵upstream¿é
ÔÚNginxµÄÉèÖÃÎļþÖУ¬Í¨¹ýupstreamÒªº¦×Ö½ç˵һ¸öupstream¿é¡£Ã¿¸öupstream¿é¿ÉÒÔ°üÀ¨¶à¸öºó¶ËЧÀÍÆ÷£¬²¢¿ÉÒÔÉèÖøºÔØƽºâµÄÕ½ÂÔºÍÏà¹ØµÄ²ÎÊý¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ”backend”µÄupstream¿é£¬ÆäÖаüÀ¨ÁËÈý¸öºó¶ËЧÀÍÆ÷¡£
ʹÓÃupstream¿é
ÔÚNginxµÄÉèÖÃÎļþÖУ¬¿ÉÒÔͨ¹ýproxy_passÖ¸Á¿Í»§¶ËÇëÇóת·¢µ½upstream¿é½ç˵µÄºó¶ËЧÀÍÆ÷ÉÏ¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
location / { proxy_pass http://backend; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃǽ«¿Í»§¶ËÇëÇóת·¢µ½ÃûΪ”backend”µÄupstream¿é½ç˵µÄºó¶ËЧÀÍÆ÷ÉÏ¡£
Èý¡¢Ê¹ÓÃupstreamÉèÖÃʵÏÖÐÞ¸´ÍøÕ¾¹ÊÕϵÄÒªÁì
ÔÚÏÖʵÍøÕ¾ÔËÓªÖУ¬ÎÒÃǾ³£»áÓöµ½ºó¶ËЧÀÍÆ÷¹ÊÕϵÄÇéÐΡ£ÎªÁ˼á³ÖÍøÕ¾µÄ¿ÉÓÃÐÔ£¬ÎÒÃÇÐèҪʵʱ·¢Ã÷²¢½â¾ö¹ÊÕÏ£¬²¢È·±£¹ÊÕÏЧÀÍÆ÷²»»áÓ°Ïìµ½ÕûÌåµÄЧÀÍÖÊÁ¿¡£Í¨¹ýºÏÀíÉèÖÃupstream¿é£¬ÎÒÃÇ¿ÉÒÔÇáËÉʵÏÖ¹ÊÕÏתÒƺÍÐÞ¸´¡£
¼ì²âºó¶ËЧÀÍÆ÷µÄ¿ÉÓÃÐÔ
NginxÖ§³Ö¶àÖÖ·½·¨À´¼ì²âºó¶ËЧÀÍÆ÷µÄ¿ÉÓÃÐÔ£¬°üÀ¨HTTP¡¢TCPºÍUDPµÈ¡£ÔÚupstream¿éÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃhealth_checkÒªº¦×ÖÀ´ÆôÓÿµ½¡¼ì²é¹¦Ð§¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; health_check; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇͨ¹ýÉèÖÃhealth_checkÒªº¦×ÖÆôÓÃÁË¿µ½¡¼ì²é¹¦Ð§¡£Nginx»á°´ÆÚ·¢ËÍÇëÇ󵽺ó¶ËЧÀÍÆ÷£¬²¢Æ¾Ö¤·µ»ØµÄ״̬ÂëÅжÏЧÀÍÆ÷µÄ¿ÉÓÃÐÔ¡£
ÌÞ³ý¹ÊÕÏЧÀÍÆ÷
µ±Ð§ÀÍÆ÷¹ÊÕÏʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÊÖ¶¯»ò×Ô¶¯µÄ·½·¨½«¹ÊÕÏЧÀÍÆ÷ÌÞ³ý¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
upstream backend { server backend1.example.com; server backend2.example.com down; server backend3.example.com; health_check; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇÔÚ¹ÊÕϵÄЧÀÍÆ÷ÉèÖúóÌí¼ÓÁËdownÒªº¦×Ö¡£µ±Nginx¼ì²âµ½Ð§ÀÍÆ÷¹ÊÕϺ󣬻á×Ô¶¯½«down±ê¼ÇµÄЧÀÍÆ÷´Óupstream¿éµÄÑ¡Ôñ¹æÄ£ÖÐÌÞ³ý¡£
ÉèÖÃ×î´óʧ°Ü´ÎÊý
ΪÁË×èÖ¹ÎóÅкÍƵÈԵĹÊÕÏתÒÆ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃmax_failsÒªº¦×ÖÀ´ÏÞÖƹÊÕÏЧÀÍÆ÷µÄ×î´óʧ°Ü´ÎÊý¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
upstream backend { server backend1.example.com max_fails=3 fail_timeout=30s; server backend2.example.com down; server backend3.example.com max_fails=3 fail_timeout=30s; health_check; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃmax_failsÒªº¦×ÖÉèÖÃÁ˹ÊÕÏЧÀÍÆ÷µÄ×î´óʧ°Ü´ÎÊýΪ3´Î¡£µ±Ä³¸öЧÀÍÆ÷µÄʧ°Ü´ÎÊýµÖ´ïÏÞÖƺó£¬Nginx»á½«Æä´ÓÑ¡Ôñ¹æÄ£ÖÐÌÞ³ý£¬²¢ÔÚÉèÖõij¬Ê±Ê±¼äÄÚ²»ÔÙʵÑéÅþÁ¬¡£
½áÂÛ:
ͨ¹ýºÏÀíÉèÖÃNginxµÄupstream¿é£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¸ºÔØƽºâºÍ¹ÊÕÏתÒƵȹ¦Ð§£¬Ìá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÎȹÌÐÔ¡£ÔÚÍøÕ¾ÔËÓªÀú³ÌÖУ¬ÎÒÃÇÓ¦¸Ãʵʱ·¢Ã÷²¢ÐÞ¸´ºó¶ËЧÀÍÆ÷µÄ¹ÊÕÏ£¬²¢Í¨¹ýupstreamµÄ¿µ½¡¼ì²éºÍ¹ÊÕÏÌÞ³ý¹¦Ð§£¬È·±£Ð§ÀÍÆ÷µÄ¿ÉÓÃÐÔ¡£Ï£Íû±¾ÎĶԸ÷ÈËÃ÷È·Nginx upstreamÉèÖõÄÔÀíºÍʹÓ÷½·¨£¬²¢ÔÚÐÞ¸´ÍøÕ¾¹ÊÕÏʱÌṩ×ÊÖúºÍÖ¸µ¼¡£
ÒÔÉϾÍÊÇNginx upstreamÉèÖÃÏê½â£¬ÊµÏÖÐÞ¸´ÍøÕ¾¹ÊÕϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡