ÉîÈë̽ÌÖNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÊÂÇéÔÀíºÍʵÏÖϸ½Ú
ÉîÈë̽ÌÖnginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÊÂÇéÔÀíºÍʵÏÖϸ½Ú
NginxÊÇÒ»ÖÖ¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬³£ÓÃÓÚ´¦Öóͷ£¸ß²¢·¢µÄÍøÂçÇëÇó¡£ËüÒÔÆäÎȹÌÐÔ¡¢¿É¿¿ÐԺ͸ßЧÐÔ¶øÊܵ½ÆÕ±éµÄ½Ó´ý¡£±¾ÎĽ«ÉîÈë̽ÌÖnginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÊÂÇéÔÀíºÍʵÏÖϸ½Ú£¬ÒÔ¼°ÔõÑùʹÓÃNginxÀ´ÊµÏÖ¸ºÔØƽºâ¡£
Ò»¡¢·´ÏòÊðÀíµÄÊÂÇéÔÀí
·´ÏòÊðÀíÊÇÖ¸¿Í»§¶ËÇëÇóЧÀÍÆ÷×ÊԴʱ£¬ÓÉÊðÀíЧÀÍÆ÷ÎüÊÕÇëÇó²¢ÏòЧÀÍÆ÷ת·¢ÇëÇó£¬×îºó½«Ð§ÀÍÆ÷µÄÏìÓ¦·µ»Ø¸ø¿Í»§¶Ë¡£¿Í»§¶ËÎÞ·¨Ö±½ÓÓëЧÀÍÆ÷ͨѶ£¬¶øÊÇÓëÊðÀíЧÀÍÆ÷½¨ÉèÅþÁ¬¡£·´ÏòÊðÀí¿ÉÒÔÆ𵽸ºÔØƽºâ¡¢»º´æºÍÇå¾²±£»¤µÈ×÷Óá£
Nginx×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆäÊÂÇéÀú³ÌÈçÏ£º
¿Í»§¶ËÏòNginxЧÀÍÆ÷·¢ËÍÇëÇó¡£
NginxЧÀÍÆ÷ÎüÊÕµ½ÇëÇóºó£¬Æ¾Ö¤Ò»¶¨µÄÕ½ÂÔÑ¡Ôñһ̨ºó¶ËЧÀÍÆ÷¡£
NginxЧÀÍÆ÷½«ÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷¡£
ºó¶ËЧÀÍÆ÷´¦Öóͷ£ÇëÇó£¬ÌìÉúÏìÓ¦¡£
ºó¶ËЧÀÍÆ÷½«ÏìÓ¦·µ»Ø¸øNginxЧÀÍÆ÷¡£
NginxЧÀÍÆ÷½«ÏìÓ¦·µ»Ø¸ø¿Í»§¶Ë¡£
¶þ¡¢¸ºÔØƽºâµÄʵÏÖϸ½Ú
¸ºÔØƽºâÊÇÖ¸½«ÇëÇó·Ö·¢µ½¶ą̀ЧÀÍÆ÷ÉÏ£¬ÒÔʹÿ̨ЧÀÍÆ÷¶¼ÄÜƽºâµØ¼ç¸ºÒ»²¿·Ö¸ºÔØ£¬Ìá¸ßϵͳµÄÕûÌåÐÔÄܺͿɿ¿ÐÔ¡£
NginxʵÏÖ¸ºÔØƽºâÖ÷Ҫͨ¹ýÒÔϼ¸ÖÖÕ½ÂÔ£º
ÂÖѯ£¨Round Robin£©Ëã·¨
ÂÖѯËã·¨ÊÇ×î¼òÆÓºÍ×î³£ÓõĸºÔØƽºâËã·¨¡£Nginxƾ֤ÇëÇóµÄ˳ÐòÒÀ´Î½«ÇëÇó·Ö·¢¸øºó¶ËЧÀÍÆ÷£¬Ã¿¸öЧÀÍÆ÷ÒÀ´Î´¦Öóͷ£Ò»¶¨ÃüÄ¿µÄÇëÇó£¬È»ºóÔÙÖØÐÂ×îÏÈ¡£ÂÖѯËã·¨ÊÊÓÃÓÚºó¶ËЧÀÍÆ÷µÄÐÔÄÜÏà¶ÔƽºâµÄÇéÐΡ£
ÉèÖÃʾÀý£º
http { upstream backend { server 192.168.1.1; server 192.168.1.2; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ȨÖØ£¨Weight£©Ëã·¨
ȨÖØËã·¨ÊÇƾ֤ºó¶ËЧÀÍÆ÷µÄÐÔÄÜÉèÖÃȨÖØ£¬Æ¾Ö¤È¨ÖرÈÀý·ÖÅÉÇëÇó¡£ÉèÖõÄȨÖØÔ½¸ß£¬Ð§ÀÍÆ÷´¦Öóͷ£µÄÇëÇó±ÈÀýÔ½¶à¡£È¨ÖØËã·¨ÊÊÓÃÓÚºó¶ËЧÀÍÆ÷ÐÔÄܲ»Æ½ºâµÄÇéÐΡ£
ÉèÖÃʾÀý£º
http { upstream backend { server 192.168.1.1 weight=3; server 192.168.1.2 weight=2; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
IP¹þÏ££¨IP Hash£©Ëã·¨
IP¹þÏ£Ë㷨ƾ֤¿Í»§¶ËµÄIPµØµãÀ´¾ÙÐиºÔØƽºâ£¬½«Í³Ò»¸ö¿Í»§¶ËµÄÇëÇó¶¨Ïòµ½Í³Ò»Ì¨ºó¶ËЧÀÍÆ÷ÉÏ¡£ÕâÑù¿ÉÒÔ¼á³ÖÓû§µÄ»á»°×´Ì¬£¬ÊÊÓÃÓÚÐèÒª¼á³Ö»á»°µÄÓ¦Óó¡¾°¡£
ÉèÖÃʾÀý£º
http { upstream backend { ip_hash; server 192.168.1.1; server 192.168.1.2; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
Ëæ»ú£¨Random£©Ëã·¨
Ëæ»úËã·¨ÊÇÔÚºó¶ËЧÀÍÆ÷ÖÐËæ»úÑ¡Ôñһ̨À´´¦Öóͷ£ÇëÇó¡£ÕâÖÖËã·¨¼òÆÓ¸ßЧ£¬ÊÊÓÃÓÚºó¶ËЧÀÍÆ÷ÐÔÄÜÏà¶ÔƽºâµÄ³¡¾°¡£
ÉèÖÃʾÀý£º
http { upstream backend { random; server 192.168.1.1; server 192.168.1.2; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
Èý¡¢´úÂëʾÀý
ÒÔÏÂÊÇÒ»¸öÍêÕûµÄNginxÉèÖÃʾÀý£¬ÍŽᷴÏòÊðÀíºÍ¸ºÔØƽºâ¡£
http { upstream backend { ip_hash; server 192.168.1.1 weight=3; server 192.168.1.2 weight=2; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
¸ÃÉèÖý«ÇëÇóת·¢µ½Á½Ì¨ºó¶ËЧÀÍÆ÷£¬ÆäÖÐ192.168.1.1ЧÀÍÆ÷µÄȨÖØΪ3£¬192.168.1.2ЧÀÍÆ÷µÄȨÖØΪ2£¬Ê¹ÓÃIP¹þÏ£Ëã·¨À´ÊµÏÖ¸ºÔØƽºâ¡£
½áÓï
±¾ÎÄÉîÈë̽ÌÖÁËNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÊÂÇéÔÀíºÍʵÏÖϸ½Ú£¬ÒÔ¼°Í¨¹ý´úÂëʾÀýչʾÁËNginxµÄÉèÖá£ÕÆÎÕNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹ØÓÚÌá¸ßWebÓ¦ÓõÄÐÔÄܺͿɿ¿ÐÔºÜÊÇÖ÷Òª£¬Ï£Íû±¾ÎĶԶÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÉîÈë̽ÌÖNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÊÂÇéÔÀíºÍʵÏÖϸ½ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡