ÔõÑùʹÓÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷ÉϵÄЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ
ÔõÑùʹÓÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷ÉϵÄЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ£¿
ÔÚÓ¦¶Ô¸ß²¢·¢»á¼ûºÍÌá¸ßЧÀÍÆ÷ÐÔÄܵÄÐèÇóÏ£¬Ð§ÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ³ÉΪÁËÒ»ÖÖÖ÷ÒªµÄ½â¾ö¼Æ»®¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´ÊµÏÖVPSЧÀÍÆ÷ÉϵÄЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ¡£
Ò»¡¢Ê²Ã´ÊÇNGINXºÍPM2£¿
NGINXÊÇÒ»¿î¿ªÔ´µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¬²¢ÇҼ汸·´ÏòÊðÀí¡¢¸ºÔØƽºâ¡¢»º´æµÈ¹¦Ð§¡£ËüÒÔÆ侫²ÊµÄÐÔÄܺͿɿ¿ÐÔ±»ÆÕ±éÓ¦ÓÃÔÚ»¥ÁªÍøÁìÓò¡£
PM2ÊÇÒ»¸öÏÖ´ú»¯µÄNode.jsÀú³ÌÖÎÀíÆ÷£¬Ëü¿ÉÒÔÖúÁ¦ÎÒÃÇÖÎÀíNode.jsÓ¦ÓõݲÅÅ¡¢¼à¿ØºÍ×Ô¶¯ÖØÆôµÈ¡£
¶þ¡¢Ê¹ÓÃNGINX¾ÙÐÐЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ
×°ÖÃNGINX
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINX¡£ÏêϸµÄ×°ÖÃÒªÁì¿ÉÒÔƾ֤²î±ðµÄ²Ù×÷ϵͳ¾ÙÐÐËÑË÷ºÍ²éÕÒ¡£
ÉèÖÃNGINX·´ÏòÊðÀí
ÔÚʹÓÃNGINX¾ÙÐиºÔØƽºâ֮ǰ£¬ÎÒÃÇÐèÒªÉèÖ÷´ÏòÊðÀí½«ÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷¡£±à¼NGINXµÄÉèÖÃÎļþ£¬ÔÚhttp¿éÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
upstream backend { server backend1; server backend2; server backend3; } 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; } }
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄbackend1¡¢backend2ºÍbackend3ÊǺó¶ËЧÀÍÆ÷µÄµØµã£¬¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÉèÖá£
Æô¶¯NGINXЧÀÍ
ÉúÑÄÉèÖÃÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯NGINXЧÀÍ£º
sudo service nginx start
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬NGINX¾ÍÒѾÉèÖÃÍê³É£¬¿ÉÒÔͨ¹ý»á¼û example.com À´²âÊÔ·´ÏòÊðÀíÊÇ·ñÉúЧ¡£
Èý¡¢Ê¹ÓÃPM2¾ÙÐÐNode.jsÓ¦ÓõÄÀ©Õ¹ºÍ¸ºÔØƽºâ
×°ÖÃPM2
ÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNode.jsºÍPM2¡£ÏêϸµÄ×°ÖÃÒªÁì¿ÉÒÔƾ֤²î±ðµÄ²Ù×÷ϵͳ¾ÙÐÐËÑË÷ºÍ²éÕÒ¡£
°²ÅÅNode.jsÓ¦ÓÃ
ͨ¹ýPM2£¬ÎÒÃÇ¿ÉÒÔ½«Node.jsÓ¦Óð²Åŵ½Ð§ÀÍÆ÷ÉÏ¡£ÔÚÏîÄ¿µÄ¸ùĿ¼Ï£¬Ö´ÐÐÒÔÏÂÏÂÁ
pm2 start app.js
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄapp.jsÊÇÄãµÄNode.jsÓ¦ÓõÄÈë¿ÚÎļþ¡£
ÉèÖÃPM2µÄÀú³ÌÊýºÍ¸ºÔØƽºâ
ĬÈÏÇéÐÎÏ£¬PM2Ö»»áÆô¶¯Ò»¸öÀú³Ì¡£ÈôÊÇÐèÒª¿ªÆô¶à¸öÀú³ÌÒÔʵÏÖ¸ºÔØƽºâ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
pm2 scale app +3
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄappÊÇÄãµÄNode.jsÓ¦ÓÃÔÚPM2ÖеÄÃû³Æ£¬+3ÌåÏÖÒªÆô¶¯Èý¸öÀú³Ì¡£¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÉèÖúÍÐÔÄÜ×ÔÐе÷½âÀú³ÌÊýÄ¿¡£
ËÄ¡¢ÍŽáNGINXºÍPM2ʵÏÖЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ
ÐÞ¸ÄNGINXµÄÉèÖÃ
ÔÚ֮ǰÉèÖõÄNGINXµÄÉèÖÃÎļþÖУ¬½«·´ÏòÊðÀíµÄת·¢µØµãÐÞ¸ÄΪNode.jsÓ¦ÓÃËù¼àÌýµÄµØµãºÍ¶Ë¿Ú£¬ÀýÈ磺
upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; }
µÇ¼ºó¸´ÖÆ
ÖØÆôNGINXºÍPM2
ÉúÑÄÉèÖÃÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÖØÆôNGINXºÍPM2ЧÀÍ£º
sudo service nginx restart pm2 restart all
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬NGINXºÍPM2¾ÍÍê³ÉÁËЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâµÄÉèÖá£Í¨¹ý»á¼û example.com£¬ÇëÇ󽫻ᱻNGINXת·¢¸øºó¶ËµÄ¶à¸öNode.jsÀú³Ì¾ÙÐд¦Àí¡£
±¾ÎļòÆÓÏÈÈÝÁËÔõÑùʹÓÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷ÉϵÄЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâ¡£Í¨¹ýNGINXµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâÉèÖã¬ÍŽáPM2µÄ¶àÀú³Ì°²ÅÅ£¬¿ÉÒÔÌá¸ßЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ¡£ÏÖʵӦÓÃÖУ¬¿ÉÒÔƾ֤ÏêϸÐèÇó¶ÔNGINXºÍPM2¾ÙÐиüÏêϸµÄÉèÖú͵÷½â¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷ÉϵÄЧÀÍÀ©Õ¹ºÍ¸ºÔØƽºâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡