ÔõÑùʵÏÖNginxµÄЧÀÍÆ÷¿µ½¡¼ì²éÉèÖÃ
ÔõÑùʵÏÖ Nginx µÄЧÀÍÆ÷¿µ½¡¼ì²éÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
Nginx ÊÇÒ»ÖÖ¸ßÐÔÄÜµÄ Web ЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Ëæ×Å»¥ÁªÍøÓªÒµµÄÉú³¤£¬¶ÔЧÀÍÆ÷µÄ¸ß¿ÉÓÃÐԺͿµ½¡×´Ì¬µÄ¼à¿Ø±äµÃÔ½À´Ô½Ö÷Òª¡£±¾ÎĽ«ÏÈÈÝÔõÑùͨ¹ýÉèÖà Nginx ʵÏÖЧÀÍÆ÷µÄ¿µ½¡¼ì²é£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Ð§ÀÍÆ÷¿µ½¡¼ì²éµÄÔÀí
ÔÚ¾ÙÐпµ½¡¼ì²éʱ£¬Nginx Ö÷Ҫͨ¹ýÏòºó¶ËЧÀÍÆ÷·¢ËÍ HTTP ÇëÇóÀ´¼ì²âЧÀÍÆ÷µÄ¿µ½¡×´Ì¬¡£ÈôÊÇЧÀÍÆ÷Õý³£ÏìÓ¦ÇëÇó£¨×´Ì¬ÂëΪ2xx£©£¬ÔòÒÔΪЧÀÍÆ÷¿µ½¡£¬½«¼ÌÐø½«ÇëÇóת·¢¸ø¸ÃЧÀÍÆ÷£»ÈôÊÇЧÀÍÆ÷ûÓÐÏìÓ¦£¨³¬Ê±»ò״̬ÂëΪ5xx£©£¬ÔòÒÔΪЧÀÍÆ÷²»¿ÉÓ㬽«×èÖ¹½«ÇëÇóת·¢¸ø¸ÃЧÀÍÆ÷£¬Ö±µ½Ð§ÀÍÖØÊÓбäΪ¿ÉÓÃ״̬¡£
¶þ¡¢ÉèÖÿµ½¡¼ì²é
1¡¢ÉèÖà upstream
¿µ½¡¼ì²éͨ³£ÔÚ upstream ÉèÖÿéÖоÙÐС£upstream ÊÇ Nginx µÄÄ£¿é£¬ÓÃÓÚ½ç˵һ×éºó¶ËЧÀÍÆ÷£¬²¢ÈÏÕ潫ÇëÇó·Ö·¢¸øÕâЩЧÀÍÆ÷¡£ÒÔÏÂÊÇÒ»¸ö¼òÆ upstream ÉèÖÃʾÀý£º
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; }
µÇ¼ºó¸´ÖÆ
2¡¢ÉèÖÿµ½¡¼ì²é²ÎÊý
ÔÚ upstream ÉèÖÿéÖУ¬¿ÉÒÔͨ¹ýÌí¼ÓһЩ²ÎÊýÀ´ÉèÖÿµ½¡¼ì²éµÄÐÐΪ¡£ÀýÈ磬¿ÉÒÔÖ¸¶¨¼ì²éµÄ¾àÀëʱ¼ä¡¢×î´óʧ°Ü´ÎÊý¡¢³¬Ê±Ê±¼äµÈ¡£ÒÔÏÂÊdz£ÓõĿµ½¡¼ì²é²ÎÊýʾÀý£º
upstream backend { server backend1.example.com max_fails=3 fail_timeout=30s; server backend2.example.com max_fails=3 fail_timeout=30s; server backend3.example.com max_fails=3 fail_timeout=30s; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬max_fails Ö¸¶¨ÁË×î´óʧ°Ü´ÎÊý£¬fail_timeout Ö¸¶¨Á˳¬Ê±Ê±¼ä¡£ÈôÊÇÒ»¸öЧÀÍÆ÷ÔÚÒ»Á¬Ê§°Ü´ÎÊýÁè¼Ý×î´óʧ°Ü´ÎÊýºó£¬½«»á±»ÒÔΪ²»¿ÉÓã¬Nginx ²»ÔÙ½«ÇëÇóת·¢¸ø¸ÃЧÀÍÆ÷£¬²¢ÔÝÍ£½«ÇëÇóת·¢¸ø¸ÃЧÀÍÆ÷µÄʱ¼äΪ³¬Ê±Ê±¼ä¡£
3¡¢ÉèÖÿµ½¡¼ì²éÇëÇó
Nginx ͨ¹ýÏòºó¶ËЧÀÍÆ÷·¢ËÍ HTTP ÇëÇóÀ´¾ÙÐпµ½¡¼ì²é¡£¿ÉÒÔͨ¹ý½ç˵һ¸ö location ¿é£¬²¢ÔÚÆäÖÐÉèÖÿµ½¡¼ì²éÇëÇóµÄ·¾¶ºÍÏìÓ¦ÂëÀ´ÊµÏÖ¿µ½¡¼ì²é¡£ÒÔÏÂÊÇÒ»¸ö¿µ½¡¼ì²éÇëÇóµÄÉèÖÃʾÀý£º
location /healthcheck { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ""; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬/healthcheck ÊÇ¿µ½¡¼ì²éÇëÇóµÄ·¾¶£¬proxy_pass Ö¸¶¨Á˼ì²éÇëÇóת·¢µÄÄ¿µÄЧÀÍÆ÷¡£
4¡¢ÉèÖÿµ½¡¼ì²é״̬
Nginx ͨ¹ý proxy_next_upstream Ö¸ÁîÉèÖÿµ½¡¼ì²éµÄ״̬¡£µ±ËùÓÐЧÀÍÆ÷¶¼²»¿ÉÓÃʱ£¬¿ÉÒÔÉèÖà Nginx ·µ»ØÒ»¸ö×Ô½ç˵µÄÏìÓ¦Â룬Ҳ¿ÉÒÔÖ±½Ó·µ»Ø¸ø¿Í»§¶ËÒ»¸ö¹ýʧҳÃæ¡£ÒÔÏÂÊÇÒ»¸ö¿µ½¡¼ì²é״̬µÄÉèÖÃʾÀý£º
http { upstream backend { server backend1.example.com max_fails=3 fail_timeout=30s; server backend2.example.com max_fails=3 fail_timeout=30s; server backend3.example.com max_fails=3 fail_timeout=30s; check interval=5s rise=2 fall=3 timeout=2s; } server { listen 80; server_name example.com; location /healthcheck { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ""; } error_page 502 = /custom_502.html; location = /custom_502.html { root /usr/share/nginx/html; internal; } proxy_next_upstream error timeout invalid_header http_502; } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬check interval Ö¸¶¨Á˼ì²éµÄ¾àÀëʱ¼ä£¬rise Ö¸¶¨ÁËÒ»Á¬ÀֳɴÎÊý£¬fall Ö¸¶¨ÁËÒ»Á¬Ê§°Ü´ÎÊý£¬timeout Ö¸¶¨Á˳¬Ê±Ê±¼ä¡£ÕâЩ²ÎÊýÓÃÓÚÉèÖÿµ½¡¼ì²éµÄÐÐΪ¡£
ͨ¹ýÒÔÉϵÄÉèÖ㬾ͿÉÒÔʵÏÖ Nginx µÄЧÀÍÆ÷¿µ½¡¼ì²é¡£
×ܽ᣺ЧÀÍÆ÷¿µ½¡¼ì²éÊÇ°ü¹ÜЧÀ͸߿ÉÓÃÐÔµÄÒªº¦¡£Í¨¹ýÉèÖà Nginx µÄ¿µ½¡¼ì²é²ÎÊýºÍÇëÇ󣬿ÉÒÔÓÐÓüà¿Øºó¶ËЧÀÍÆ÷µÄ¿µ½¡×´Ì¬£¬²¢ÔÚЧÀÍÆ÷²»¿ÉÓÃʱ×Ô¶¯×èÖ¹½«ÇëÇóת·¢¸øËü¡£Ï£Íû±¾ÎÄÌṩµÄÏêϸÉèÖÃʾÀý¶ÔÄãÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginxµÄЧÀÍÆ÷¿µ½¡¼ì²éÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡