ÉîÈëÃ÷È·NginxµÄ¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁì
ÉîÈëÃ÷È·nginxµÄ¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁì
СÐò
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤ºÍÓ¦ÓõÄÆÕ¼°£¬¸ß²¢·¢»á¼û³ÉΪÁËwebÓ¦ÓõÄÖ÷ÒªÎÊÌâÖ®Ò»¡£¶ø¸ºÔØƽºâÊÖÒÕÔòÊǽâ¾ö¸ß²¢·¢»á¼ûÎÊÌâµÄÒªº¦Ö®Ò»¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Æ为ÔØƽºâ¹¦Ð§±¸ÊÜ¿ª·¢ÕßµÄÇàíù¡£±¾ÎĽ«ÉîÈë̽ÌÖNginxµÄ¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁì¡£
NginxµÄ¸ºÔØƽºâËã·¨
2.1 ÂÖѯ£¨Round Robin£©
ÂÖѯÊÇNginxĬÈϵĸºÔØƽºâËã·¨¡£Ëü°´Ë³Ðò½«ÇëÇó·ÖÅɸøºó¶ËЧÀÍÆ÷£¬¼á³Öÿ¸öЧÀÍÆ÷µÄ¸ºÔØÏà¶Ôƽºâ¡£µ±ÓÐеÄÇëÇóµÖ´ïʱ£¬Nginx½«Æ¾Ö¤ÊÂÏȽç˵µÄupstreamÖеÄЧÀÍÆ÷Áбí˳ÐòÒÀ´Î½«ÇëÇóת·¢¸øÿ¸öЧÀÍÆ÷¡£
ʾÀý´úÂëÈçÏ£º
upstream backend { server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; } server { location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
2.2 ¼ÓȨÂÖѯ£¨Weighted Round Robin£©
¼ÓȨÂÖѯÊÇÔÚÂÖѯËã·¨µÄ»ù´¡ÉÏ£¬¸øÿ¸öЧÀÍÆ÷·ÖÅÉÒ»¸öȨÖØÖµ£¬È¨ÖØÔ½¸ß£¬±»·ÖÅɵ½ÇëÇóµÄ¸ÅÂʾÍÔ½´ó¡£ÕâÑù¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÐÔÄܺ͸ºÔØÇéÐΣ¬ºÏÀí·ÖÅÉÇëÇó¡£
ʾÀý´úÂëÈçÏ£º
upstream backend { server 192.168.1.101 weight=3; server 192.168.1.102 weight=2; server 192.168.1.103 weight=1; } server { location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
2.3 IP Hash
IP HashËã·¨ÊÇƾ֤ÇëÇóµÄ¿Í»§¶ËIPµØµã¾ÙÐÐhashÅÌË㣬Ȼºó½«ÇëÇóת·¢¸ø¶ÔÓ¦µÄЧÀÍÆ÷¡£ÕâÑù¿ÉÒÔʹÀ´×Ôͳһ¸öIPµØµãµÄÇëÇó¶¼×ª·¢µ½Í³Ò»Ì¨Ð§ÀÍÆ÷£¬ÊµÏÖSession¼á³Ö¡£
ʾÀý´úÂëÈçÏ£º
upstream backend { ip_hash; server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; } server { location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
Õ½ÂÔÑ¡ÕªÒªÁì
3.1 ¾²Ì¬ÉèÖÃ
¾²Ì¬ÉèÖÃÊÇ×î³£¼ûµÄÕ½ÂÔÑ¡ÕªÒªÁ죬¼´Í¨¹ýÊÖ¶¯ÉèÖÃupstreamÖеÄЧÀÍÆ÷ÁбíºÍȨÖØÖµÀ´ÊµÏÖ¶ÔÓ¦µÄ¸ºÔØƽºâËã·¨¡£ÕâÖÖÒªÁìÊÊÓÃÓÚЧÀÍÆ÷¹æÄ£Ïà¶ÔÎȹ̡¢¸ºÔØÏà¶ÔƽºâµÄÇéÐΡ£
3.2 ¶¯Ì¬ÉèÖÃ
¶¯Ì¬ÉèÖÃÊÇƾ֤ÏÖʵÔËÐÐʱµÄЧÀÍÆ÷¸ºÔØÇéÐÎÀ´¶¯Ì¬µ÷½â¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡Ôñ¡£NginxÌṩÁËһЩÏà¹ØµÄÄ£¿é£¬Èçnginx-plus-upsyncºÍnginx-upsync-module£¬¿ÉÒÔͨ¹ý°´ÆÚ¼ì²éºó¶ËЧÀÍÆ÷״̬£¬²¢×Ô¶¯µ÷½âȨÖØÖµ¡¢Ìí¼Ó¡¢É¾³ýºÍÐ޸ĺó¶ËЧÀÍÆ÷£¬ÒÔʵÏÖ¶¯Ì¬¸ºÔØƽºâ¡£
ʾÀý´úÂëÈçÏ£º
upstream backend { zone backend 64k; server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; keepalive 64; hash $remote_addr consistent; } server { location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
½áÂÛ
±¾ÎÄÉîÈëÃ÷È·ÁËNginxµÄ¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁì¡£ÔÚÏÖʵ¿ª·¢ÖУ¬Æ¾Ö¤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵĸºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁ죬¿ÉÒÔÓÐÓÃÌá¸ßÓ¦ÓõÄÐÔÄܺͿɿ¿ÐÔ¡£Í¬Ê±£¬Ïàʶ¶¯Ì¬ÉèÖõÄÒªÁ죬¿ÉÒÔƾ֤ЧÀÍÆ÷¸ºÔØÇéÐζ¯Ì¬µ÷½â¸ºÔØƽºâÕ½ÂÔ£¬½øÒ»²½ÌáÉýÓ¦ÓõÄÎȹÌÐԺͿÉÀ©Õ¹ÐÔ¡£
²Î¿¼×ÊÁÏ£º
Nginx Documentation – http://nginx.org/en/docs/
Nginx Load Balancing – http://nginx.org/en/docs/http/load_balancing.html
ÒÔÉϾÍÊÇÉîÈëÃ÷È·NginxµÄ¸ºÔØƽºâËã·¨ºÍÕ½ÂÔÑ¡ÕªÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡