ÏêϸÏÈÈÝNginx·´ÏòÊðÀíЧÀÍÆ÷µÄÈÝÔÖ±¸·ÝÉèÖÃÒªÁì
nginx·´ÏòÊðÀíЧÀÍÆ÷µÄÈÝÔÖ±¸·ÝÉèÖÃÒªÁìÏê½â
¸ÅÊö
ÔÚ¹¹½¨WebÓ¦ÓõÄÇéÐÎÖУ¬¸ß¿ÉÓÃÐÔºÍÈÝÔÖ±¸·ÝÊÇÖÁ¹ØÖ÷ÒªµÄ¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵķ´ÏòÊðÀíЧÀÍÆ÷£¬ÓµÓÐÇ¿Ê¢µÄÈÝÔÖ±¸·ÝÉèÖù¦Ð§£¬¿ÉÒÔÈ·±£ÏµÍ³ÔÚЧÀÍÆ÷¹ÊÕÏʱһÁ¬¿ÉÓᣱ¾ÎĽ«ÏÈÈÝNginx·´ÏòÊðÀíЧÀÍÆ÷µÄÈÝÔÖ±¸·ÝÉèÖÃÒªÁ죬Ïêϸ˵Ã÷ÔõÑùʹÓñ¸·ÝЧÀÍÆ÷°ü¹ÜϵͳµÄ¸ß¿ÉÓÃÐÔ¡£
×°ÖÃNginx
Ê×ÏÈ£¬ÐèҪȷ±£ÄãµÄЧÀÍÆ÷ÉÏÒѾװÖÃÁËNginx¡£ÈôÊÇÄ㻹ûÓÐ×°ÖÃNginx£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
$ sudo apt-get update $ sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃÖ÷ЧÀÍÆ÷
·¿ªNginxµÄÖ÷ÉèÖÃÎļþ£¬Í¨³£Îª/etc/nginx/nginx.conf£¬¾ÙÐÐÈçÏÂÐ޸ģº
http { upstream backend { server primary_server:80; server backup_server:80 backup; } server { listen 80; server_name your_domain.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃupstreamÖ¸Áî½ç˵ÁËÒ»¸öÃûΪbackendµÄ·´ÏòÊðÀíЧÀÍÆ÷µÄ¼¯Èº¡£ÆäÖУ¬primary_serverÊÇÖ÷ЧÀÍÆ÷µÄµØµãÓë¶Ë¿Ú£¬backup_serverÊDZ¸·ÝЧÀÍÆ÷µÄµØµãÓë¶Ë¿Ú£¬²¢ÇÒʹÓÃÁËbackup²ÎÊýÌåÏÖ±¸·ÝЧÀÍÆ÷¡£Í¨¹ýÉèÖÃlocation /£¬Ö¸¶¨ÁËÇëÇóµÄת·¢µØµãΪhttp://backend¡£
ÉèÖñ¸·ÝЧÀÍÆ÷
ÉèÖÃÎļþͬÑùΪ/etc/nginx/nginx.conf£¬ÈÔÈ»·¿ªnginxµÄÖ÷ÉèÖÃÎļþ£¬ÐÞ¸ÄÈçÏ£º
http { upstream backend { server primary_server:80 backup; server backup_server:80; } server { listen 80; server_name your_domain.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÔÚ±¸·ÝЧÀÍÆ÷ÖУ¬ÎÒÃǽ«primary_serverÓëbackup_serverµÄλÖöԵ÷£¬ÕâÑùÔÚÖ÷ЧÀÍÆ÷å´»úʱ£¬ÇëÇ󽫻áת·¢ÖÁ±¸·ÝЧÀÍÆ÷¡£
ÉèÖÿµ½¡¼ì²é
ΪÁËÌá¸ßϵͳµÄ¿ÉÓÃÐÔ£¬ÎÒÃÇ¿ÉÒÔÉèÖÃNginx¾ÙÐпµ½¡¼ì²é£¬ÒÔ±ãÔÚЧÀÍÆ÷²»¿ÉÓÃʱʵʱÇл»µ½±¸·ÝЧÀÍÆ÷¡£·¿ªNginxµÄÖ÷ÉèÖÃÎļþ£¬ÔöÌíÒÔÏÂÉèÖãº
http { upstream backend { server primary_server:80 backup; server backup_server:80; # ÆôÓÿµ½¡¼ì²é health_check; # ¼ì²é¾àÀëʱ¼ä check_interval 5s; # ʧ°Ü´ÎÊýãÐÖµ check_failure_threshold 3; # ¼ì²éµÄ³¬Ê±Ê±¼ä check_timeout 2s; } server { listen 80; server_name your_domain.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËhealth_checkÖ¸ÁîÆôÓÃÁË¿µ½¡¼ì²é¹¦Ð§£¬²¢ÉèÖÃÁ˼ì²éµÄ¾àÀëʱ¼ä¡¢Ê§°Ü´ÎÊýãÐÖµºÍ³¬Ê±Ê±¼ä¡£µ±Ö÷ЧÀÍÆ÷Ò»Á¬Ê§°Ü´ÎÊýµÖ´ïãÐֵʱ£¬Nginx½«»áÇл»µ½±¸·ÝЧÀÍÆ÷¡£
ÖØÆôNginxЧÀÍ
Íê³ÉÒÔÉÏÉèÖúó£¬Í¨¹ýÒÔÏÂÏÂÁîÖØÆôNginxЧÀÍʹÉèÖÃÉúЧ£º
$ sudo service nginx restart
µÇ¼ºó¸´ÖÆ
×ܽá
Nginx·´ÏòÊðÀíЧÀÍÆ÷ÌṩÁËÇ¿Ê¢µÄÈÝÔÖ±¸·ÝÉèÖù¦Ð§£¬Ê¹ÏµÍ³ÔÚЧÀÍÆ÷¹ÊÕÏʱÒÀÈ»Äܹ»Ò»Á¬¿ÉÓá£Í¨¹ýÉèÖÃÖ÷ЧÀÍÆ÷ºÍ±¸·ÝЧÀÍÆ÷¡¢¿µ½¡¼ì²éµÈ²Ù×÷£¬¿ÉÒÔÈ·±£ÏµÍ³¸ß¿ÉÓÃÐÔ¡£Ï£Íû±¾ÎÄËùÌṩµÄÐÅÏ¢¶ÔÄúÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÏêϸÏÈÈÝNginx·´ÏòÊðÀíЧÀÍÆ÷µÄÈÝÔÖ±¸·ÝÉèÖÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡