NginxÔõÑùʵÏÖHTTPÇëÇóµÄÖØÊÔÉèÖÃ
NginxÔõÑùʵÏÖHTTPÇëÇóµÄÖØÊÔÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
NginxÊÇÒ»¿îºÜÊÇÊ¢ÐеĿªÔ´·´ÏòÊðÀíЧÀÍÆ÷£¬ËüÓµÓÐÇ¿Ê¢µÄ¹¦Ð§ºÍÎÞаµÄÉèÖÃÑ¡Ï¿ÉÒÔÓÃÀ´ÊµÏÖHTTPÇëÇóµÄÖØÊÔÉèÖá£ÔÚÍøÂçͨѶÖУ¬ÓÉÓÚÖÖÖÖÔµ¹ÊÔÓÉ£¬ÀýÈçÍøÂçÑÓ³Ù¡¢Ð§ÀÍÆ÷¸ºÔصȣ¬ÓÐʱ¼äÎÒÃÇÌᳫµÄHTTPÇëÇó¿ÉÄÜ»áʧ°Ü¡£ÎªÁËÌá¸ßÓ¦ÓóÌÐòµÄ¿É¿¿ÐÔºÍÎȹÌÐÔ£¬ÎÒÃÇ¿ÉÄÜÐèÒªÔÚÇëÇóʧ°Üʱ¾ÙÐÐÖØÊÔ¡£
ÏÂÃ潫ÏÈÈÝÔõÑùʹÓÃNginxÉèÖÃHTTPÇëÇóµÄÖØÊÔ¹¦Ð§¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÈçÏ´úÂë¶Î£º
http { upstream backend { server backend1.example.com weight=5 max_fails=3 fail_timeout=30s; server backend2.example.com backup; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_connect_timeout 2s; proxy_set_header Host $host; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖеÄupstream¿é½ç˵Á˺ó¶ËЧÀÍÆ÷µÄµØµãºÍÉèÖÃÑ¡Ïî¡£ÆäÖÐbackend1.example.comºÍbackend2.example.comÊÇÕæʵµÄºó¶ËЧÀÍÆ÷µØµã£¬weight=5ÌåÏÖȨÖØΪ5£¬max_fails=3ÌåÏÖÖØÊÔ3´ÎºóÈÔȻʧ°ÜÔòÒÔΪЧÀÍÆ÷²»¿ÉÓã¬fail_timeout=30sÌåÏÖЧÀÍÆ÷±»±ê¼ÇΪ²»¿ÉÓúóµÄʱ¼ä¾àÀëΪ30Ãë¡£
proxy_passÖ¸ÁîÓÃÓÚת·¢ÇëÇ󵽺ó¶ËЧÀÍÆ÷£¬proxy_next_upstreamÖ¸ÁîÓÃÓÚ½ç˵µ±ÇëÇóʧ°Üʱ¾ÙÐÐÖØÊÔµÄÌõ¼þ£¬ÆäÖÐerrorÌåÏÖÇëÇó·ºÆð¹ýʧʱ¾ÙÐÐÖØÊÔ£¬timeoutÌåÏÖÇëÇó³¬Ê±Ê±¾ÙÐÐÖØÊÔ£¬invalid_headerÌåÏÖÏìӦͷÐÅÏ¢ÎÞЧʱ¾ÙÐÐÖØÊÔ£¬http_500¡¢http_502¡¢http_503ºÍhttp_504ÌåÏÖµ±ÏìӦ״̬ÂëΪ500¡¢502¡¢503ºÍ504ʱ¾ÙÐÐÖØÊÔ¡£
ÁíÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃproxy_connect_timeoutÖ¸ÁîÉèÖÃÓëºó¶ËЧÀÍÆ÷½¨ÉèÅþÁ¬µÄ³¬Ê±Ê±¼ä£¬proxy_set_headerÖ¸ÁîÉèÖÃÇëÇóÍ·ÐÅÏ¢ÖеÄHost×Ö¶ÎΪÔʼÇëÇóµÄHost×ֶΡ£
ÉèÖÃÍê³Éºó£¬ÉúÑIJ¢ÖØмÓÔØNginxÉèÖÃÎļþ¡£
½ÓÏÂÀ´£¬µ±ÎÒÃÇÌᳫHTTPÇëÇóʱ£¬Nginx½«Æ¾Ö¤ÎÒÃǵÄÉèÖÃÑ¡Ïî¾ÙÐÐÇëÇóµÄת·¢£¬²¢ÔÚÇкÏÖØÊÔÌõ¼þµÄÇéÐÎϾÙÐÐÖØÊÔ¡£
ÀýÈ磬µ±ÎÒÃÇͨ¹ýä¯ÀÀÆ÷»á¼ûhttp://example.comʱ£¬Nginx½«»áת·¢ÇëÇ󵽺ó¶ËЧÀÍÆ÷£¬ÈôÊÇÇëÇóʧ°Ü»ò³¬Ê±£¬Nginx»áƾ֤ÉèÖþÙÐÐÖØÊÔ£¬Ö±µ½ÇëÇóÀֳɻòµÖ´ï×î´óÖØÊÔ´ÎÊý¡£
ÖµµÃ×¢ÖصÄÊÇ£¬²î±ðµÄÓ¦Óó¡¾°¿ÉÄÜÐèÒª²î±ðµÄÖØÊÔÉèÖã¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒª¾ÙÐе÷½â¡£ÖØÊÔÉèÖõÄÎÞаÐÔÊÇNginxµÄÒ»´óÓÅÊÆ£¬¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐж¨ÖÆ»¯µÄÉèÖá£
×ܽáÀ´Ëµ£¬Nginx¿ÉÒÔͨ¹ýÉèÖÃÑ¡ÏîʵÏÖHTTPÇëÇóµÄÖØÊÔ¹¦Ð§£¬Ìá¸ßÓ¦ÓóÌÐòµÄ¿É¿¿ÐÔºÍÎȹÌÐÔ¡£Í¨¹ýºÏÀíµÄÖØÊÔÉèÖã¬ÎÒÃÇ¿ÉÒÔÓ¦¶ÔÍøÂçͨѶÖпÉÄÜ·ºÆðµÄÖÖÖÖÎÊÌ⣬°ü¹ÜÇëÇóµÄÀÖ³ÉÏìÓ¦¡£ÔÚÏÖʵʹÓÃÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇó¾ÙÐÐÎÞаµÄÉèÖ㬲¢Í¨¹ýNginxµÄÈÕÖ¾¹¦Ð§¾ÙÐмà¿ØºÍÆÊÎö£¬´Ó¶øÓÅ»¯ÇëÇóµÄÖØÊÔÕ½ÂÔ¡£
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖHTTPÇëÇóµÄÖØÊÔÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡