NginxÔõÑùʵÏÖ¸ºÔØƽºâÉèÖÃ
NginxÔõÑùʵÏÖ¸ºÔØƽºâÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
СÐò£º
¸ºÔØƽºâÊÇÒ»ÖÖÔÚÍøÂçЧÀÍÆ÷ÖзÖÅÉÊÂÇ鸺ºÉµÄÒªÁ죬ʹµÃ¶ą̀ЧÀÍÆ÷¿ÉÒÔÐͬ´¦Öóͷ£ÇëÇó£¬Ìá¸ßϵͳµÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£ÔÚNginxÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃʵÏÖ¸ºÔØƽºâ£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginx¾ÙÐиºÔØƽºâÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý£¬Ï£Íû¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
Ò»¡¢Nginx¸ºÔØƽºâµÄ»ùÀ´Ô´Àí
Nginxͨ¹ýHTTP·´ÏòÊðÀíµÄ·½·¨ÊµÏÖ¸ºÔØƽºâ¡£µ±¿Í»§¶Ë·¢ËÍÇëÇóʱ£¬Nginx×÷ΪÊðÀíЧÀÍÆ÷½«ÇëÇóת·¢¸øºǫ́µÄ¶ą̀ЧÀÍÆ÷£¬ÔÙ½«Ð§ÀÍÆ÷´¦Öóͷ£µÄЧ¹û·µ»Ø¸ø¿Í»§¶Ë¡£NginxÖ§³Ö¶àÖÖ¸ºÔØƽºâÕ½ÂÔ£¬°üÀ¨ÂÖѯ¡¢¼ÓȨÂÖѯ¡¢IP¹þÏ£¡¢×îÉÙÅþÁ¬ÊýµÈ¡£
¶þ¡¢Nginx¸ºÔØƽºâµÄÉèÖÃÒªÁì
×°ÖÃNginx
Ê×ÏÈÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£ÏêϸװÖÃÒªÁì¿É²Î¿¼Nginx¹Ù·½Îĵµ¡£
ÉèÖúǫ́ЧÀÍÆ÷
ÔÚʵÏÖ¸ºÔØƽºâ֮ǰ£¬ÐèÒªÏÈÔÚºǫ́׼±¸¶à¸öЧÀÍÆ÷ÓÃÓÚ´¦Öóͷ£ÇëÇ󡣿ÉÒÔÊǶą̀ÎïÀíЧÀÍÆ÷£¬Ò²¿ÉÒÔÊÇͳһ̨ЧÀÍÆ÷µÄ¶à¸öÐéÄâÖ÷»ú¡£
ÉèÖÃNginxÊðÀí
±à¼NginxµÄÉèÖÃÎļþ£¬Ò»Ñùƽ³£Î»ÓÚ/etc/nginx/nginx.conf¡£ÔÚhttpÄ£¿éÖÐÌí¼ÓÒÔÏ´úÂ룺
http { upstream backend { server backend1.example.com weight=1; server backend2.example.com weight=2; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬upstreamÒªº¦×Ö½ç˵ÁËÒ»×éºǫ́ЧÀÍÆ÷£¬¿ÉÒÔƾ֤ÐèÇóÌí¼Ó»òɾ³ýЧÀÍÆ÷¡£ÆäÖУ¬backend1.example.comºÍbackend2.example.com¿ÉÒÔÌ滻ΪÏÖʵµÄЧÀÍÆ÷ÓòÃû»òIPµØµã¡£weight²ÎÊý½ç˵ÁËÿ¸öЧÀÍÆ÷µÄȨÖØ£¬È¨ÖØÔ½¸ß£¬´¦Öóͷ£µÄÇëÇóÔ½¶à¡£
ÖØÆôNginxЧÀÍ
ÉúÑÄÉèÖÃÎļþ²¢ÖØÆôNginxЧÀÍ£¬Ê¹ÉèÖÃÉúЧ¡£
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
Èý¡¢Nginx¸ºÔØƽºâµÄʵÀýºÍЧ¹û
¼ÙÉèÓÐÁ½Ì¨Ð§ÀÍÆ÷backend1£¨192.168.1.100£©ºÍbackend2£¨192.168.1.200£©£¬ÆäÖÐbackend2µÄȨÖØÊÇbackend1µÄÁ½±¶¡£µ±Óпͻ§¶Ë·¢ËÍÇëÇóʱ£¬Nginx½«»áƾ֤ȨÖؾÙÐзַ¢¡£ÏêϸÉèÖÃÈçÏ£º
http { upstream backend { server 192.168.1.100 weight=1; server 192.168.1.200 weight=2; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏÉèÖÃÖУ¬µ±ÓÐ100¸öÇëÇó·¢Ë͵½Nginxʱ£¬backend1½«´¦Öóͷ£È¨ÖØΪ100/3¡Ö34¸öÇëÇ󣬶øbackend2½«´¦Öóͷ£È¨ÖØΪ100/3*2¡Ö67¸öÇëÇ󣬴ӶøʵÏÖ¸ºÔØƽºâ¡£
½áÂÛ£º
ͨ¹ýÒÔÉϵÄÉèÖúÍʾÀý´úÂ룬ÎÒÃÇ¿ÉÒÔ¿´³ö£¬NginxµÄ¸ºÔØƽºâ¹¦Ð§ºÜÊÇÇ¿Ê¢ÇÒÒ×ÓÚÉèÖá£ÎÞÂÛÊÇСÐÍÍøÕ¾ÕվɴóÐÍÓ¦ÓóÌÐò£¬¶¼¿ÉÒÔ½èÖúNginxʵÏָ߿ÉÓú͸ßÐÔÄܵĸºÔØƽºâ¡£Ï£Íû±¾ÎĶÔÄúÃ÷È·Nginx¸ºÔØƽºâµÄÉèÖÃÒªÁìÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ¸ºÔØƽºâÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡