ÔõÑùʵÏÖNginxµÄ·´ÏòÊðÀí¸ºÔØƽºâÉèÖÃ
ÔõÑùʵÏÖNginxµÄ·´ÏòÊðÀí¸ºÔØƽºâÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ÐèÒª´¦Àí´ó×ڵĻá¼ûÇëÇó¡£ÎªÁËÈ·±£ÏµÍ³µÄ¸ß¿ÉÓÃÐÔºÍÐÔÄܵÄÎȹÌÐÔ£¬ÍùÍùÐèÒª½«»á¼ûÇëÇó·Ö·¢¸ø¶ą̀ЧÀÍÆ÷¾ÙÐд¦Àí¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÌṩÁËÇ¿Ê¢µÄ¸ºÔØƽºâ¹¦Ð§£¬±¾ÎĽ«´øÄúÏàʶÔõÑùʹÓÃNginxʵÏÖ·´ÏòÊðÀí¸ºÔØƽºâÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Ê²Ã´ÊÇ·´ÏòÊðÀí¸ºÔØƽºâ
·´ÏòÊðÀí¸ºÔØƽºâÊÇָͨ¹ýÒ»ÆäÖÐÐÄÕÛÎñÆ÷£¬Î´À´×ÔÍⲿ¿Í»§¶ËµÄÇëÇó·Ö·¢¸ø¶ą̀ÕæʵЧÀÍÆ÷¾ÙÐд¦Àí¡£ËüµÄÖ÷ҪĿµÄÊÇÌá¸ßϵͳµÄÐÔÄܺͿÉÓÃÐÔ£¬×èÖ¹µ¥µã¹ÊÕÏ£¬²¢Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÔÚ·´ÏòÊðÀí¸ºÔØƽºâģʽÏ£¬ÖÐÐÄÕÛÎñÆ÷ÎüÊÕµ½¿Í»§¶ËµÄÇëÇóºó£¬»áƾ֤һ¶¨µÄÕ½ÂÔ£¬½«ÇëÇóת·¢¸øÕæʵЧÀÍÆ÷¼¯ÈºÖеÄһ̨»ò¶ą̀ЧÀÍÆ÷¾ÙÐд¦Àí£¬È»ºó½«Ð§¹û·µ»Ø¸ø¿Í»§¶Ë¡£ÕâÑù¿ÉÒÔ½«ÇëÇóÔȳƵطַ¢¸ø¶ą̀ЧÀÍÆ÷£¬ÒԵִ︺ÔØƽºâµÄЧ¹û¡£
¶þ¡¢Nginx·´ÏòÊðÀí¸ºÔØƽºâÉèÖÃ
NginxÊÇÒ»¸öÇáÁ¿¼¶¡¢¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬¾ß±¸ÎÞаµÄÉèÖúÍÇ¿Ê¢µÄÐÔÄÜ¡£ÏÂÃæÊÇʹÓÃNginxʵÏÖ·´ÏòÊðÀí¸ºÔØƽºâµÄÉèÖð취£º
×°ÖÃNginx
Ê×ÏÈÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£¿ÉÒÔͨ¹ý°ü¹ÜÀí¹¤¾ß£¬Èçapt»òyumÀ´×°ÖÃNginx¡£ÏêϸµÄ×°Öð취¿ÉÒԲο¼NginxµÄ¹Ù·½Îĵµ¡£
ÉèÖ÷´ÏòÊðÀí
·¿ªNginxµÄÉèÖÃÎļþ£¬Ä¬ÈÏ·¾¶Îª/etc/nginx/nginx.conf£¬ÕÒµ½http¿é£¬Ìí¼ÓÈçÏÂÉèÖãº
http { # ½ç˵upstream£¬Ö¸¶¨ÕæʵЧÀÍÆ÷¼¯Èº upstream backend { server 192.168.1.1:8080; server 192.168.1.2:8080; server 192.168.1.3:8080; } # ½ç˵·´ÏòÊðÀí 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; } } }
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄbackendÊÇÒ»¸öupstream¿é£¬ÓÃÓÚ½çËÃ÷ÈÕæʵЧÀÍÆ÷¼¯ÈºµÄµØµãºÍ¶Ë¿Ú¡£ÔÚproxy_passÖ¸ÁîÖУ¬½«ÇëÇóת·¢¸øbackend£¬ÊµÏÖÁË·´ÏòÊðÀí¹¦Ð§¡£Í¨¹ýproxy_set_headerÖ¸Á½«¿Í»§¶ËµÄÇëÇóÍ·ÐÅϢת´ï¸øÕæʵЧÀÍÆ÷¡£
ÉúÑÄÉèÖÃÎļþ²¢ÖØÆôNginx
ÉúÑÄÉèÖÃÎļþºó£¬Ê¹ÓÃÏÂÁî¡°sudo service nginx restart¡±ÖØÆôNginxЧÀÍÆ÷£¬Ê¹ÉèÖÃÉúЧ¡£
Èý¡¢¸ºÔØƽºâËã·¨
NginxÌṩÁ˶àÖÖ¸ºÔØƽºâËã·¨£¬¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵÄËã·¨£º
ÂÖѯËã·¨£¨Ä¬ÈÏ£©£ºÆ¾Ö¤Ë³ÐòÖð¸ö»á¼ûЧÀÍÆ÷£¬ÈôÊÇÆäÖÐÓÐЧÀÍÆ÷å´»ú£¬ÔòÌø¹ý¸ÃЧÀÍÆ÷¡£
¼ÓȨÂÖѯËã·¨£ºÔÚÂÖѯËã·¨µÄ»ù´¡ÉÏ£¬ÎªÃ¿¸öЧÀÍÆ÷Ö¸¶¨Ò»¸öȨÖØ£¬Æ¾Ö¤È¨ÖØÖð¸ö»á¼ûЧÀÍÆ÷£¬ÒÔʵÏÖ²î±ðЧÀÍÆ÷µÄ¸ºÔØƽºâ¡£
IP¹þÏ£Ëã·¨£ºÆ¾Ö¤¿Í»§¶ËµÄIPµØµã£¬½«ÆäÓ³É䵽ij̨ÕæʵЧÀÍÆ÷´¦ÀíÇëÇó£¬ÊµÏֻỰ¼á³Ö¡£
×îÉÙÅþÁ¬Ëã·¨£º½«ÇëÇó·¢Ë͸øÄ¿½ñÕýÔÚ´¦ÀíÅþÁ¬Êý×îÉÙµÄЧÀÍÆ÷£¬ÒÔʵÏÖ¶¯Ì¬¸ºÔØƽºâ¡£
ÉèÖøºÔØƽºâËã·¨
ÔÚupstream¿éÖУ¬¿ÉÒÔͨ¹ýÖ¸¶¨¸ºÔØƽºâËã·¨£¬À´ÉèÖòî±ðµÄ·ÖÅÉÕ½ÂÔ¡£ÏÂÃæÊǼ¸ÖÖ³£ÓõÄÉèÖÃʾÀý£º
ÂÖѯËã·¨£º
upstream backend { server 192.168.1.1:8080; server 192.168.1.2:8080; server 192.168.1.3:8080; }
µÇ¼ºó¸´ÖÆ
¼ÓȨÂÖѯËã·¨£º
upstream backend { server 192.168.1.1:8080 weight=3; server 192.168.1.2:8080 weight=2; server 192.168.1.3:8080 weight=1; }
µÇ¼ºó¸´ÖÆ
IP¹þÏ£Ëã·¨£º
upstream backend { ip_hash; server 192.168.1.1:8080; server 192.168.1.2:8080; server 192.168.1.3:8080; }
µÇ¼ºó¸´ÖÆ
×îÉÙÅþÁ¬Ëã·¨£º
upstream backend { least_conn; server 192.168.1.1:8080; server 192.168.1.2:8080; server 192.168.1.3:8080; }
µÇ¼ºó¸´ÖÆ
ÉèÖÃÍ긺ÔØƽºâËã·¨ºó£¬ÉúÑÄÉèÖÃÎļþ²¢ÖØÆôNginx¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNginxʵÏÖ·´ÏòÊðÀí¸ºÔØƽºâÉèÖ㬲¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£Ê¹Ó÷´ÏòÊðÀí¸ºÔØƽºâ¿ÉÒÔÌá¸ßϵͳµÄÐÔÄܺͿÉÓÃÐÔ£¬·Ö·¢ÇëÇó¸ø¶ą̀ÕæʵЧÀÍÆ÷£¬½«¸ºÔØƽºâ»¯£¬´Ó¶øʵÏÖЧÀ͵ĸ߿ÉÓúÍÎȹÌÐÔ¡£Ï£Íû±¾ÎĶÔÄúÓÐËù×ÊÖú£¬ÌṩÁËÒ»ÖÖʵÏÖNginx·´ÏòÊðÀí¸ºÔØƽºâµÄ¼Æ»®¡£ÈôÊǶÔNginx¸ºÔØƽºâÉèÖÃÓÐÈκÎÎÊÌ⣬¿ÉÒԲο¼NginxµÄ¹Ù·½Îĵµ»òÕß×ÉѯÏà¹ØÊÖÒÕÖ°Ô±¡£
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginxµÄ·´ÏòÊðÀí¸ºÔØƽºâÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡