ÉîÈë̽ÌÖNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ΢ЧÀͼܹ¹ÖеĸßЧ°²ÅÅ·½·¨
ÉîÈë̽ÌÖnginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ΢ЧÀͼܹ¹ÖеĸßЧ°²ÅÅ·½·¨
Ç°ÑÔ£º
ÔÚ΢ЧÀͼܹ¹ÖУ¬Ð§ÀÍÖ®¼äµÄÏ໥ÒÀÀµÒÔ¼°Á÷Á¿µÄ¿ìËÙÔöÌí¸øЧÀ͵İ²ÅźÍÖÎÀí´øÀ´ÁËÖØ´óµÄÌôÕ½¡£ÎªÏàʶ¾öÕâÒ»ÎÊÌ⣬Nginx×÷ΪһÖÖ¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀí£¬Í¨¹ýÌṩ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ¹¦Ð§£¬³ÉΪÁË΢ЧÀͼܹ¹Öа²ÅźÍÖÎÀíµÄÖ÷Òª¹¤¾ßÖ®Ò»¡£±¾ÎĽ«Ì½ÌÖNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ΢ЧÀͼܹ¹ÖеĸßЧ°²ÅÅ·½·¨£¬²¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢Nginx·´ÏòÊðÀí
1.1 ʲôÊÇ·´ÏòÊðÀí
·´ÏòÊðÀíÊÇÖ¸½«ÇëÇó´Ó¿Í»§¶Ë·¢Ë͵½Ð§ÀÍÆ÷¶Ë£¬²¢½«ÏìÓ¦´ÓЧÀÍÆ÷¶Ë·µ»Ø¸ø¿Í»§¶Ë¡£ÓëÕýÏòÊðÀí²î±ð£¬ÕýÏòÊðÀíÊÇÊðÀíЧÀÍÆ÷½«ÇëÇó´Ó¿Í»§¶Ë·¢Ë͵½Ä¿µÄЧÀÍÆ÷£¬²¢½«ÏìÓ¦·µ»Ø¸ø¿Í»§¶Ë¡£Í¨¹ý·´ÏòÊðÀí£¬ÎÒÃÇ¿ÉÒÔÒþ²ØЧÀÍÆ÷µÄÄÚ²¿½á¹¹£¬Ìá¸ßÇå¾²ÐÔ£¬Í¬Ê±¿ÉÒÔʵÏÖ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ¡£
1.2 NginxʵÏÖ·´ÏòÊðÀí
ÒªÔÚNginxÖÐʵÏÖ·´ÏòÊðÀí£¬ÎÒÃÇÐèÒªÉèÖÃNginxµÄ·´ÏòÊðÀíЧÀÍÆ÷¿é£¬ÈçÏÂËùʾ£º
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ”backend”µÄЧÀÍÆ÷¼¯Èº£¬ÆäÖаüÀ¨ÁËÈý¸öºó¶ËЧÀÍÆ÷µÄµØµã¡£È»ºó£¬ÔÚ”location /”ÖУ¬ÎÒÃÇʹÓÃÁËproxy_passÖ¸ÁÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷¼¯Èº¡£
¶þ¡¢Nginx¸ºÔØƽºâ
2.1 ʲôÊǸºÔØƽºâ
¸ºÔØƽºâÊÇÖ¸½«ÇëÇó·Ö·¢µ½¶à¸öЧÀÍÆ÷ÉÏ£¬ÒÔµÖ´ïÌá¸ßÐÔÄܺͿɿ¿ÐÔµÄÄ¿µÄ¡£Í¨¹ý½«Á÷Á¿ÔȳƵطÖÅɵ½¶à¸öЧÀÍÆ÷ÉÏ£¬¿ÉÒÔ¼õÇáµ¥¸öЧÀÍÆ÷µÄ¸ºÔØ£¬Ìá¸ßϵͳµÄÏìÓ¦ËÙÂʺÍÀ©Õ¹ÐÔ¡£
2.2 NginxʵÏÖ¸ºÔØƽºâ
ÔÚNginxÖУ¬ÊµÏÖ¸ºÔØƽºâÐèҪʹÓÃupstreamÖ¸ÁîºÍÏìÓ¦µÄ¸ºÔØƽºâËã·¨¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĸºÔØƽºâÉèÖÃʾÀý£º
http { upstream backend { least_conn; server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁË”least_conn”Ëã·¨À´ÊµÏÖ¸ºÔØƽºâ¡£Õâ¸öËã·¨»á½«ÇëÇó·¢Ë͵½Ä¿½ñÅþÁ¬Êý×îÉٵĺó¶ËЧÀÍÆ÷¡£Í¨¹ýʹÓÃÕâÖÖ¸ºÔØƽºâËã·¨£¬ÎÒÃÇ¿ÉÒÔ°ü¹ÜЧÀÍÆ÷µÄ¸ºÔØÔȳƷÖÅÉ¡£
Èý¡¢Î¢Ð§Àͼܹ¹ÖеÄNginx°²ÅÅ·½·¨
ÔÚ΢ЧÀͼܹ¹ÖУ¬Ð§ÀÍͨ³£ÒÔÈÝÆ÷»¯µÄ·½·¨°²ÅÅ£¬Ã¿¸öЧÀÍÔËÐÐÔÚ×ÔÁ¦µÄÈÝÆ÷ÖС£ÎªÁËʵÏÖ¸ßЧµÄ°²ÅÅ£¬ÎÒÃÇ¿ÉÒÔ½«Nginx×÷Ϊһ¸ö×ÔÁ¦µÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâЧÀÍ£¬½«Á÷Á¿Ö¸µ¼µ½²î±ðµÄЧÀÍÈÝÆ÷ÖС£
3.1 Docker°²ÅÅNginx
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚDockerÖа²ÅÅNginxÈÝÆ÷¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÀÈ¡ºÍÔËÐÐNginxÈÝÆ÷£º
docker pull nginx docker run -d -p 80:80 --name nginx-container nginx
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁÀÈ¡×îеÄNginx¾µÏñ£¬²¢ÔÚÈÝÆ÷ÄÚÆô¶¯Ò»¸öNginxʵÀý¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûÖ÷»úµÄ80¶Ë¿ÚÀ´»á¼ûNginxÈÝÆ÷¡£
3.2 ÉèÖÃNginx·´ÏòÊðÀíºÍ¸ºÔØƽºâ
ÔÚNginxÈÝÆ÷ÖУ¬ÎÒÃÇÐèÒª±à¼NginxÉèÖÃÎļþÀ´ÊµÏÖ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª½øÈëNginxÈÝÆ÷ÄÚ²¿£º
docker exec -it nginx-container /bin/bash
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃvi»òÆäËû±à¼Æ÷À´±à¼NginxµÄÉèÖÃÎļþ(/etc/nginx/nginx.conf)¡£ÔÚÉèÖÃÎļþÖУ¬Æ¾Ö¤Ö®Ç°µÄʾÀý£¬ÉèÖ÷´ÏòÊðÀíºÍ¸ºÔØƽºâ¹æÔò¡£ÉúÑÄÎļþ²¢ÖØмÓÔØNginxÉèÖãº
nginx -s reload
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬NginxÈÝÆ÷¾ÍÉèÖúÃÁË·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹æÔò£¬¿ÉÒÔƾ֤ÉèÖý«ÇëÇóת·¢µ½²î±ðµÄ΢ЧÀÍÈÝÆ÷ÖС£
½áÓ
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ΢ЧÀͼܹ¹ÖеĸßЧ°²ÅÅ·½·¨¡£Í¨¹ýʹÓÃNginxÌṩµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʵÏÖЧÀ͵ĸ߿ÉÓÃÐÔºÍÀ©Õ¹ÐÔ¡£Ï£Íû±¾ÎÄÄܶÔÄãÔÚ΢ЧÀͼܹ¹Öеİ²ÅźÍÖÎÀíÊÂÇéÓÐËù×ÊÖú¡£
²Î¿¼Á´½Ó£º
https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
´úÂëʾÀýÒÑÔÚÕýÎÄÖиø³ö¡£
ÒÔÉϾÍÊÇÉîÈë̽ÌÖNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ΢ЧÀͼܹ¹ÖеĸßЧ°²ÅÅ·½·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡