Ïêϸ½â¶ÁNginxµÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®
nginxµÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®Ïê½â
СÐò£º
ÔÚµ±½ñ»¥ÁªÍøÓ¦ÓÃÖУ¬¸ß¿ÉÓÃÐԺ͸ºÔØƽºâÊDz»¿É»òȱµÄÒªº¦ÒªËØ¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆÕ±éÓ¦ÓÃÓÚ¹¹½¨¸ß¿ÉÓÃÐԺ͸߲¢·¢µÄÓ¦Óüܹ¹¡£±¾ÎĽ«Ïêϸ½â¶ÁNginxµÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢¸ºÔØƽºâ¸ÅÊö
¸ºÔØƽºâÊǽ«ÍøÂçÁ÷Á¿·Ö·¢µ½¶à¸öЧÀÍÆ÷ÉÏ£¬ÒÔʵÏÖ×ÊÔ´µÄƽºâʹÓúÍÌá¸ßϵͳµÄÕûÌåÐÔÄÜ¡£Nginxͨ¹ýÆä·´ÏòÊðÀí¹¦Ð§ÊµÏÖ¸ºÔØƽºâ¡£ÔÚNginxÖУ¬¿ÉÒÔͨ¹ýÉèÖÃupstreamÀ´½ç˵һ×éºó¶ËЧÀÍÆ÷£¬Nginx»áƾ֤É趨µÄËã·¨½«ÇëÇó·Ö·¢µ½²î±ðµÄЧÀÍÆ÷ÉÏ¡£
¶þ¡¢³£ÓõĸºÔØƽºâËã·¨
ÂÖѯ£¨round-robin£©£ºÄ¬ÈϵĸºÔØƽºâËã·¨£¬°´Ë³ÐòÒÀ´Î½«ÇëÇó·Ö·¢µ½ºó¶ËЧÀÍÆ÷¡£
¼ÓȨÂÖѯ£¨weight round-robin£©£º¿ÉÒÔΪÿ¸öЧÀÍÆ÷ÉèÖÃȨÖØ£¬°´±ÈÀý·Ö·¢ÇëÇó¡£
IP Hash£ºÆ¾Ö¤¿Í»§¶ËIPµØµã¾ÙÐйþÏ£ÅÌË㣬½«Í³Ò»¸öIPµÄÇëÇó·Ö·¢µ½Í³Ò»Ì¨Ð§ÀÍÆ÷ÉÏ£¬ÊÊÓÃÓÚÐèÒª¼á³Ö»á»°µÄÓ¦Óá£
×îÉÙÅþÁ¬£¨least connected£©£º½«ÇëÇó·Ö·¢¸øÄ¿½ñÅþÁ¬Êý×îÉÙµÄЧÀÍÆ÷¡£
URL Hash£ºÆ¾Ö¤ÇëÇóµÄURL¾ÙÐйþÏ£ÅÌË㣬½«Í³Ò»¸öURLµÄÇëÇó·Ö·¢µ½Í³Ò»Ì¨Ð§ÀÍÆ÷ÉÏ¡£
Èý¡¢¸ºÔØƽºâÉèÖÃʾÀý
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNginx¸ºÔØƽºâÉèÖÃʾÀý£¬Ê¹ÓÃÂÖѯËã·¨½«ÇëÇó·Ö·¢µ½Èý̨ºó¶ËЧÀÍÆ÷£º
http { upstream backend { server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇͨ¹ýupstream½ç˵ÁËÈý¸öºó¶ËЧÀÍÆ÷µÄµØµã¡£ÔÚserver¿éÖУ¬Í¨¹ýÉèÖÃlocation½«ÇëÇóÊðÀíµ½ºó¶ËЧÀÍÆ÷¡£Ä¬ÈÏÇéÐÎÏ£¬Nginx»áʹÓÃÂÖѯËã·¨½«ÇëÇó·Ö·¢µ½ºó¶ËЧÀÍÆ÷¡£
ËÄ¡¢¸ß¿ÉÓÃÐÔ°²Åżƻ®
ΪÁË°ü¹ÜϵͳµÄ¸ß¿ÉÓÃÐÔ£¬ÎÒÃÇ¿ÉÒÔ½ÓÄÉÖ÷´Ó½á¹¹»òÕßÈȱ¸·Ý½á¹¹°²ÅÅNginx¡£
Ö÷´Ó½á¹¹£¨Active-Passive£©
ÔÚÖ÷´Ó½á¹¹ÖУ¬ÓÐÒ»¸öÖ÷ЧÀÍÆ÷ÈÏÕæ´¦ÀíËùÓеÄÇëÇ󣬶ø±¸·ÝЧÀÍÆ÷Ö»ÓÐÔÚÖ÷ЧÀÍÆ÷¹ÊÕÏʱ²Å»á½ÓÊÜÇëÇ󡣿ÉÒÔͨ¹ýkeepalived¹¤¾ßʵÏÖÖ÷´ÓЧÀÍÆ÷Ö®¼äµÄ×Ô¶¯Çл»¡£ÏÂÃæÊÇÒ»¸öÖ÷´Ó½á¹¹µÄÉèÖÃʾÀý£º
http { upstream backend { server 192.168.1.101; server 192.168.1.102 backup; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇÔÚµÚ¶þ¸öЧÀÍÆ÷ºóÃæ¼ÓÉÏÁËbackupÒªº¦×Ö£¬ÌåÏÖ¸ÃЧÀÍÆ÷Ö»ÓÐÔÚÖ÷ЧÀÍÆ÷²»¿ÉÓÃʱ²Å»áÎüÊÕÇëÇó¡£
Èȱ¸·Ý½á¹¹£¨Active-Active£©
Èȱ¸·Ý½á¹¹ÖУ¬¶ą̀ЧÀÍÆ÷¶¼´¦ÓÚ»îԾ״̬ÇÒͬʱ´¦ÀíÇëÇó£¬Í¨¹ýÈí¸ºÔØƽºâʵÏÖÇëÇóµÄ·Ö·¢¡£¿ÉÒÔʹÓÃLVS£¨Linux Virtual Server£©»òÕßHAProxyÀ´ÊµÏÖ¸ºÔØƽºâ¡£ÏÂÃæÊÇÒ»¸öÈȱ¸·Ý½á¹¹µÄÉèÖÃʾÀý£º
http { upstream backend { server 192.168.1.101; server 192.168.1.102; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÁ½Ì¨ºó¶ËЧÀÍÆ÷£¬²¢Í¨¹ýupstreamʵÏÖ¸ºÔØƽºâ¡£Í¨¹ýÉèÖöą̀ЧÀÍÆ÷²¢Ê¹ÓøºÔØƽºâËã·¨£¬¿ÉÒÔʵÏָ߿ÉÓÃÐԵݲÅÅ¡£
½áÂÛ£º
NginxÌṩÁËÇ¿Ê¢µÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®£¬Í¨¹ýºÏÀíµÄÉèÖÿÉÒÔʵÏÖ×ÊÔ´µÄƽºâʹÓúÍϵͳµÄ¸ß¿ÉÓÃÐÔ¡£±¾ÎÄÏêϸ½â¶ÁÁËNginxµÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®£¬²¢ÌṩÁËÏìÓ¦µÄ´úÂëʾÀý¡£Ï£Íû¶ÁÕß¿ÉÒÔͨ¹ý±¾ÎĵÄÖ¸µ¼ºÍʾÀýÀ´¹¹½¨¸ß¿ÉÓÃÐԺ͸߲¢·¢µÄÓ¦Óüܹ¹¡£
ÒÔÉϾÍÊÇÏêϸ½â¶ÁNginxµÄ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ°²Åżƻ®µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡