NginxЧÀÍÆ÷µÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ´óÊý¾Ý³¡¾°ÖеÄÓ¦Óð¸ÀýÆÊÎö
nginxЧÀÍÆ÷µÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ´óÊý¾Ý³¡¾°ÖеÄÓ¦Óð¸ÀýÆÊÎö
СÐò£º
ÔÚÄ¿½ñÐÅÏ¢»¯Ê±´ú£¬´óÊý¾ÝÓ¦ÓõÄÆÕ±éÍƹã¶ÔЧÀÍÆ÷µÄÐÔÄܺͳÐÔØÄÜÁ¦Ìá³öÁ˸ü¸ßµÄÒªÇó¡£ÎªÁËÖª×ã´ó×ÚÓû§Í¬Ê±»á¼ûµÄÐèÇ󣬽ÓÄÉ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄ¼Ü×é³ÉΪÁ˳£ÓõĽâ¾ö¼Æ»®¡£±¾ÎĽ«ÒÔNginxЧÀÍÆ÷ΪÀý£¬ÆÊÎö·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ´óÊý¾Ý³¡¾°ÖеÄÓ¦Óð¸Àý£¬²¢ÍŽáÏÖʵ´úÂëʾÀý¾ÙÐÐÑÝʾ¡£
Ò»¡¢Nginx·´ÏòÊðÀíµÄÓ¦Óð¸Àý
1.1¡¢¸ºÔØƽºâÆ÷
ÔÚ´óÊý¾Ý³¡¾°ÖУ¬¾³£Óöµ½Ðí¶àÓªÒµÐèÇóÐèҪͨ¹ýÇëÇó·Ö·¢µ½¶à¸öЧÀÍÆ÷ÉÏ£¬ÒÔÌá¸ßЧÀÍÆ÷ÐÔÄܺͿɿ¿ÐÔ¡£NginxµÄ·´ÏòÊðÀí¹¦Ð§¿ÉÒÔ×÷Ϊһ¸ö¸ºÔØƽºâÆ÷£¬½«Óû§ÇëÇóƾ֤һ¶¨µÄËã·¨·Ö·¢µ½ºó¶ËÕæʵЧÀÍÆ÷£¬ÊµÏÖÇëÇóµÄ¸ºÔØƽºâ¡£
ʾÀý´úÂ룺
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
1.2¡¢ÇëÇ󻺴æÓëÊý¾Ý¾²Ì¬»¯
ÔÚ´óÊý¾Ý³¡¾°ÖУ¬ÎªÁËÌá¸ß»á¼ûËÙÂʺͽµµÍЧÀÍÆ÷ѹÁ¦£¬Í¨³£½«Ò»Ð©¾²Ì¬Êý¾Ý¾ÙÐлº´æ£¬²¢Ö±½Ó·µ»Ø»º´æЧ¹û¡£NginxµÄ·´ÏòÊðÀí¹¦Ð§¿ÉÒÔͨ¹ý»º´æ»úÖÆ£¬ÊµÏÖÇëÇóµÄ»º´æºÍÊý¾ÝµÄ¾²Ì¬»¯¡£
ʾÀý´úÂ룺
http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; } } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢Nginx¸ºÔØƽºâµÄÓ¦Óð¸Àý
2.1¡¢Ð§ÀÍÆ÷¼¯ÈºµÄ´î½¨
ÔÚ´óÊý¾Ý³¡¾°ÖУ¬Í¨³£»á¹¹½¨Ò»¸öÖØ´óµÄЧÀÍÆ÷¼¯ÈºÀ´³ÐÔغ£Á¿µÄÇëÇóºÍÊý¾Ý´¦Àí¡£NginxµÄ¸ºÔØƽºâ¹¦Ð§¿ÉÒÔʵÏÖ¶ą̀ЧÀÍÆ÷µÄ¶¯Ì¬µ÷Àí£¬È·±£Ã¿Ì¨Ð§ÀÍÆ÷µÄ×ÊԴʹÓÃÂʺ͸ºÔØƽºâ¡£
ʾÀý´úÂ룺
http { upstream backend { server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com max_fails=3 fail_timeout=30s; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
2.2¡¢Ó²¼þ×ÊÔ´µÄÓÅ»¯Ê¹ÓÃ
ÔÚ´óÊý¾Ý³¡¾°ÖУ¬Ð§ÀÍÆ÷µÄÓ²¼þ×ÊÔ´ÊǺÜÊÇÃû¹óµÄ£¬Òò´ËÐèÒªºÏÀíʹÓò¢¾ÙÐÐÓÅ»¯¡£NginxµÄ¸ºÔØƽºâ¹¦Ð§¿ÉÒÔͨ¹ýÖÇÄÜËã·¨£¬½«ÇëÇó·Ö·¢µ½Ð§ÀÍÆ÷ÖÐÐÔÄÜ×îÓŵĽڵãÉÏ£¬Ìá¸ßÓ²¼þ×ÊÔ´µÄʹÓÃÂÊ¡£
ʾÀý´úÂ룺
http { upstream backend { least_conn; server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
ͨ¹ýNginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹¦Ð§µÄÓ¦Óð¸ÀýÆÊÎö£¬ÎÒÃÇ¿ÉÒÔ·¢Ã÷£¬ÔÚ´óÊý¾Ý³¡¾°ÖУ¬Ê¹Ó÷´ÏòÊðÀíºÍ¸ºÔØƽºâ¿ÉÒÔ´ó´óÌá¸ßЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ¡£Í¨¹ýºÏÀíÉèÖÃNginxµÄÏà¹Ø²ÎÊý£¬¿ÉÒÔʹµÃЧÀÍÆ÷¸ºÔØƽºâ¡¢ÇëÇ󻺴æºÍÊý¾Ý¾²Ì¬»¯µÈ¹¦Ð§Ô½·¢ÓÐÓõØʵÏÖ¡£Òò´Ë£¬NginxµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ´óÊý¾Ý³¡¾°ÖÐÓÐ×ÅÆÕ±éµÄÓ¦ÓÃÔ¶¾°¡£
²Î¿¼ÎÄÏ×£º
http://nginx.org/en/docs/http/load_balancing.html
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
ÒÔÉϾÍÊÇNginxЧÀÍÆ÷µÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÔÚ´óÊý¾Ý³¡¾°ÖеÄÓ¦Óð¸ÀýÆÊÎöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡