nginxÔõÑùÉèÖøºÔØƽºâ
ÔõÑùÉèÖà nginx ¾ÙÐиºÔØƽºâ£¿½ç˵ÉÏÓÎЧÀÍÆ÷³Ø£¬Ö¸¶¨Ð§ÀÍÆ÷ ip ºÍ¶Ë¿Ú¡£½ç˵ÐéÄâÖ÷»ú£¬¼àÌýÅþÁ¬²¢×ª·¢µ½ÉÏÓγء£Ö¸¶¨Î»Öã¬Æ¥ÅäÇëÇó²¢×ª·¢µ½ÉÏÓγء£
Nginx ÉèÖøºÔØƽºâÖ¸ÄÏ
¸ºÔØƽºâÊÇÒ»ÖÖÍøÂçÊÖÒÕ£¬¿É½«´«ÈëÁ÷Á¿ÂþÑܵ½¶à¸öЧÀÍÆ÷ÉÏ£¬ÒÔÌá¸ßÐÔÄܲ¢ÔöÇ¿¿ÉÓÃÐÔ¡£Nginx ÊÇÒ»¿îÊ¢ÐÐµÄ Web ЧÀÍÆ÷£¬¿ÉÓÃÓÚÉèÖøºÔØƽºâ¡£
ÉèÖð취
ÒªÉèÖà Nginx ¸ºÔØƽºâ£¬ÇëÔÚ Nginx ÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÖ¸Á
upstream my_upstream { server 192.168.1.100:80; server 192.168.1.101:80; server 192.168.1.102:80; } server { listen 80; server_name my-domain.com; location / { proxy_pass http://my_upstream; } }
µÇ¼ºó¸´ÖÆ
Ïêϸ˵Ã÷
upstream ¿é½ç˵ÁËÉÏÓÎЧÀÍÆ÷³Ø£¬ÆäÖаüÀ¨Òª¾ÙÐиºÔØƽºâµÄЧÀÍÆ÷ÁÐ±í¡£server Ö¸ÁîÖ¸¶¨Ã¿Ì¨Ð§ÀÍÆ÷µÄ IP µØµãºÍ¶Ë¿Ú¡£
server ¿é½ç˵ÁËÒ»¸öÐéÄâÖ÷»ú£¬Ëü¼àÌý´«ÈëµÄÅþÁ¬²¢½«Æäת·¢µ½ÉÏÓÎЧÀÍÆ÷³Ø¡£server_name Ö¸ÁîÖ¸¶¨ÐéÄâÖ÷»úµÄÖ÷»úÃû¡£location / ¿é½ç˵ÁËÒ»¸öλÖã¬ËüÆ¥ÅäËùÓд«ÈëÇëÇó²¢½«Æäת·¢µ½ÉÏÓÎЧÀÍÆ÷³Ø¡£proxy_pass Ö¸ÁîÖ¸¶¨ÉÏÓÎЧÀÍÆ÷³ØµÄÃû³Æ¡£
¸ºÔØƽºâËã·¨
Nginx Ö§³ÖÒÔϸºÔØƽºâËã·¨£º
ÂÖѯ (least_conn)£ºÒÀ´Î½«ÇëÇó·Ö·¢µ½ÉÏÓÎЧÀÍÆ÷£¬Ð§ÀÍÆ÷ÅþÁ¬Êý×îÉÙµÄÓÅÏÈ´¦ÀíÇëÇó¡£
ȨÖØÑ»· (weight)£ºÆ¾Ö¤ÎªÃ¿¸öЧÀÍÆ÷ÉèÖõÄȨÖØ·ÖÅÉÇëÇó£¬È¨ÖؽϸߵÄЧÀÍÆ÷´¦Àí¸ü¶àÇëÇó¡£
IP ¹þÏ£ (ip_hash)£ºÆ¾Ö¤¿Í»§¶Ë IP µØµã¾ÙÐйþÏ££¬×ÜÊÇδÀ´×Ôͳһ¸ö IP µØµãµÄÇëÇó·¢Ë͵½Í³Ò»Ì¨Ð§ÀÍÆ÷¡£
¿ÉÒÔͨ¹ýÔÚupstream ¿éÖÐʹÓÃleast_conn¡¢weight »òhash µÈÖ¸ÁîÀ´ÉèÖÃÕâЩËã·¨¡£
¼à¿ØºÍ¿µ½¡¼ì²é
Nginx Ìṩ¶àÖÖ¼à¿ØºÍ¿µ½¡¼ì²é»úÖÆÀ´¼àÊÓÉÏÓÎЧÀÍÆ÷µÄÔËÐÐ״̬¡£ÀýÈ磬¿ÉÒÔʹÓÃproxy_monitor Ä£¿éÆôÓÃ×Ô¶¯¿µ½¡¼ì²é£¬ÈôÊÇЧÀÍÆ÷ÏìÓ¦²»ÊµÊ±£¬Ôò½«Æä´ÓÉÏÓÎЧÀÍÆ÷³ØÖбê¼ÇΪÍÑ»ú¡£
ÒÔÉϾÍÊÇnginxÔõÑùÉèÖøºÔØƽºâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡