Nginx¸ºÔØƽºâ¶àÖÖÕ½ÂÔÉèÖã¬ÓÅ»¯ÍøÕ¾ÐÔÄÜ
nginx¸ºÔØƽºâ¶àÖÖÕ½ÂÔÉèÖã¬ÓÅ»¯ÍøÕ¾ÐÔÄÜ
¸ÅÊö£º
Ëæ×Å»¥ÁªÍøµÄѸÃÍÉú³¤£¬ÍøÕ¾µÄ»á¼ûÁ¿Ò²Ô½À´Ô½´ó¡£ÎªÁËÖª×ãÓû§µÄÐèÇó£¬Ìá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÐÔÄÜ£¬ÎÒÃÇ¿ÉÒÔʹÓøºÔØƽºâÀ´·Öµ£Ð§ÀÍÆ÷µÄ¸ºÔØѹÁ¦¡£NginxÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ËüÌṩÁ˶àÖÖ¸ºÔØƽºâÕ½ÂÔ¹©ÎÒÃÇÑ¡Ôñ£¬±¾ÎĽ«ÏÈÈÝNginx¸ºÔØƽºâµÄ¼¸ÖÖÕ½ÂÔÉèÖ㬲¢¸½´ø´úÂëʾÀý¡£
ÂÖѯ£¨Round Robin£©Õ½ÂÔ£º
ÂÖѯÊÇ×î³£¼ûµÄ¸ºÔØƽºâÕ½ÂÔÖ®Ò»£¬Ò²ÊÇNginxµÄĬÈÏÕ½ÂÔ¡£Ëü½«ÇëÇóƽºâµØ·Ö·¢µ½ºó¶ËµÄ¶à¸öЧÀÍÆ÷ÉÏ£¬Ã¿¸öÇëÇó°´Ë³ÐòÒÀ´Î·Ö·¢µ½²î±ðµÄЧÀÍÆ÷ÉÏ¡£µ±ÓÐЧÀÍÆ÷å´»úʱ£¬Nginx»á×Ô¶¯½«Æäɨ³ýÔÚ¸ºÔØƽºâµÄ¹æÄ£Íâ¡£ÂÖѯսÂÔµÄNginxÉèÖÃÈçÏ£º
http { upstream backend { server 192.168.1.1; server 192.168.1.2; server 192.168.1.3; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
×îÉÙÅþÁ¬£¨Least Connections£©Õ½ÂÔ£º
×îÉÙÅþÁ¬Õ½ÂԻὫÇëÇó·¢Ë͵½Ä¿½ñÅþÁ¬Êý×îÉÙµÄЧÀÍÆ÷ÉÏ£¬ÒÔʵÏÖ¸ºÔØƽºâ¡£ÕâÑù¿ÉÒÔÈ·±£Ã¿¸öЧÀÍÆ÷ÉϵÄÅþÁ¬ÊýÏà¶Ôƽºâ£¬×èֹij¸öЧÀÍÆ÷±»Ì«¹ýѹÁ¦¡£NginxÌṩÁËÒ»¸öÄ£¿éleast_connÀ´ÊµÏÖ×îÉÙÅþÁ¬Õ½ÂÔ¡£ÉèÖÃÒªÁìÈçÏ£º
http { upstream backend { least_conn; server 192.168.1.1; server 192.168.1.2; server 192.168.1.3; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
IP¹þÏ££¨IP Hash£©Õ½ÂÔ£º
IP¹þÏ£Õ½ÂÔ»áƾ֤¿Í»§¶ËµÄIPµØµã½«ÇëÇó·Ö·¢µ½ºó¶ËЧÀÍÆ÷ÉÏ¡£ÕâÑù¿ÉÒÔÈ·±£Í³Ò»¸ö¿Í»§¶ËµÄÇëÇ󶼻ᱻ·¢Ë͵½Í³Ò»¸öºó¶ËЧÀÍÆ÷ÉÏ£¬Ìá¸ß»º´æµÄЧ¹û¡£NginxÌṩÁËÒ»¸öÄ£¿éip_hashÀ´ÊµÏÖIP¹þÏ£Õ½ÂÔ¡£ÉèÖÃÒªÁìÈçÏ£º
http { upstream backend { ip_hash; server 192.168.1.1; server 192.168.1.2; server 192.168.1.3; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
¼ÓȨÂÖѯ£¨Weighted Round Robin£©Õ½ÂÔ£º
¼ÓȨÂÖѯսÂÔÔÊÐí¸ø²î±ðµÄЧÀÍÆ÷ÉèÖòî±ðµÄȨÖØ£¬Ð§ÀÍÆ÷µÄȨÖØÔ½¸ß£¬±»Ñ¡ÖеĸÅÂʾÍÔ½´ó¡£ÕâÑù¿ÉÒÔÓÐÓõطÖÅÉЧÀÍÆ÷µÄ¸ºÔØѹÁ¦¡£NginxµÄÉèÖÃÒªÁìÈçÏ£º
http { upstream backend { server 192.168.1.1 weight=3; server 192.168.1.2 weight=2; server 192.168.1.3 weight=1; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
×ܽ᣺
ͨ¹ýºÏÀíÑ¡ÔñºÍÉèÖøºÔØƽºâÕ½ÂÔ£¬ÎÒÃÇ¿ÉÒÔÓÅ»¯ÍøÕ¾µÄÐÔÄÜ£¬Ìá¸ßÓû§µÄ»á¼ûÌåÑé¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÌṩÁ˶àÖÖ¸ºÔØƽºâÕ½ÂÔ¹©ÎÒÃÇÑ¡Ôñ¡£±¾ÎÄÏÈÈÝÁËÂÖѯ¡¢×îÉÙÅþÁ¬¡¢IP¹þÏ£ºÍ¼ÓȨÂÖѯÕ⼸ÖÖ³£ÓõÄÕ½ÂÔ£¬²¢ÌṩÁËÏìÓ¦µÄNginxÉèÖÃʾÀý¡£Ï£Íû±¾ÎÄÄܶԸ÷ÈËѧϰºÍÊÂÇéÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginx¸ºÔØƽºâ¶àÖÖÕ½ÂÔÉèÖã¬ÓÅ»¯ÍøÕ¾ÐÔÄܵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡