NGINXÓëPM2: ¹¹½¨¸ßЧµÄVPSÇéÐÎ
NGINXÓëPM2: ¹¹½¨¸ßЧµÄVPSÇéÐΣ¬ÐèÒªÏêϸ´úÂëʾÀý
Ëæ×Å»¥ÁªÍøµÄѸÃÍÉú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓóÌÐò°²ÅÅÔÚÐéÄâרÓÃЧÀÍÆ÷£¨VPS£©ÉÏ¡£ÎªÁËÈ·±£ÕâЩӦÓóÌÐòµÄ¸ßЧÔËÐкͿɿ¿ÐÔ£¬ÎÒÃÇÐèҪѡÔñÊʵ±µÄ¹¤¾ßºÍÊÖÒÕÀ´¹¹½¨ºÍÖÎÀíVPSÇéÐΡ£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÖصãÏÈÈÝÁ½¸öÒªº¦¹¤¾ß£ºNGINXºÍPM2¡£NGINXÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬¶øPM2ÊÇÒ»¸öÇ¿Ê¢µÄÀú³ÌÖÎÀíÆ÷¡£Í¨¹ýʹÓÃÕâÁ½¸ö¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔ¹¹½¨Ò»¸ö¸ßЧµÄVPSÇéÐΣ¬ÒÔÌṩÎȹ̺Ϳɿ¿µÄÓ¦ÓóÌÐòЧÀÍ¡£
Ê×ÏÈ£¬ÈÃÎÒÃÇÀ´ÏàʶһÏÂNGINX¡£NGINXÊÇÒ»¸ö¿ªÔ´µÄ¡¢¸ßÐÔÄܵÄHTTPЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷¡£Ëü¿ÉÒÔÓÐÓõش¦Àí´ó×ڵIJ¢·¢ÇëÇ󣬲¢Ìṩ¾²Ì¬×ÊÔ´µÄ¿ìËÙÏìÓ¦¡£±ðµÄ£¬NGINX»¹¾ßÓÐÇ¿Ê¢µÄ¸ºÔØƽºâºÍ·´ÏòÊðÀí¹¦Ð§£¬¿ÉÒÔ½«ÇëÇóת·¢µ½²î±ðµÄºó¶ËЧÀÍÆ÷¡£ÕâʹµÃÎÒÃÇ¿ÉÒÔÇáËÉʵÏָ߿ÉÓÃÐÔºÍÀ©Õ¹ÐÔ¡£
ÏÂÃæÊÇÒ»¸öʹÓÃNGINX×÷Ϊ·´ÏòÊðÀíµÄʾÀýÉèÖãº
server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÉèÖÃÖУ¬ÎÒÃǽ«ËùÓÐÀ´×Ôexample.comµÄÇëÇóת·¢µ½ÔËÐÐÔÚÍâµØµÄ3000¶Ë¿ÚÉϵÄÓ¦ÓóÌÐòÖС£Í¬Ê±£¬ÎÒÃÇ»¹ÉèÖÃÁËһЩÓÃÓÚת´ïÏà¹ØÐÅÏ¢µÄHTTPÍ·²¿¡£
½ÓÏÂÀ´£¬ÎÒÃÇÀ´ÏÈÈÝPM2¡£PM2ÊÇÒ»¸ö¿ªÔ´µÄÀú³ÌÖÎÀíÆ÷£¬¿ÉÓÃÓÚÖÎÀíNode.jsÓ¦ÓóÌÐò¡£Ëü¿ÉÒÔ¼à¿ØÓ¦ÓóÌÐòµÄ״̬²¢×Ô¶¯¾ÙÐйÊÕϻָ´¡£±ðµÄ£¬PM2»¹¾ßÓиºÔØƽºâºÍ¼¯Èº¹¦Ð§£¬¿ÉÒÔÇáËɵش¦Àí¶à¸ö²¢·¢ÇëÇó¡£
ÏÂÃæÊÇÒ»¸öʹÓÃPM2Æô¶¯ºÍÖÎÀíNode.jsÓ¦ÓóÌÐòµÄʾÀýÏÂÁ
pm2 start app.js -i max
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÏÂÁîÖУ¬ÎÒÃÇʹÓÃPM2Æô¶¯Ò»¸öÃûΪapp.jsµÄNode.jsÓ¦ÓóÌÐò£¬²¢Ê¹ÓÃ-i²ÎÊýÉèÖÃÀú³ÌÊýΪ×î´óÖµ¡£Õ⽫ƾ֤¿ÉÓõĴ¦ÀíÆ÷½¹µãÊýÄ¿×Ô¶¯ÉèÖÃÀú³ÌÊý¡£
ͨ¹ýʹÓÃNGINXºÍPM2£¬ÎÒÃÇ¿ÉÒÔ¹¹½¨Ò»¸ö¸ßЧµÄVPSÇéÐΡ£ÏÂÃæÊÇÒ»¸öÍêÕûµÄʾÀýÉèÖÃÎļþ£¬ÑÝʾÁËÔõÑù½«NGINXºÍPM2ÍŽáʹÓãº
http { upstream app_servers { server localhost:3000; server localhost:3001; server localhost:3002; } server { listen 80; server_name example.com; location / { proxy_pass http://app_servers; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÉèÖÃÎļþÖУ¬ÎÒÃÇʹÓÃÁËÒ»¸öÃûΪapp_serversµÄ¸ºÔØƽºâ×飬ÆäÖаüÀ¨ÁËÔËÐÐÔÚ²î±ð¶Ë¿ÚÉϵĶà¸öNode.jsÓ¦ÓóÌÐò¡£NGINX½«Æ¾Ö¤¸ºÔØƽºâËã·¨×Ô¶¯½«ÇëÇóת·¢µ½¿ÉÓõĺó¶ËЧÀÍÆ÷¡£
½Ó×Å£¬ÎÒÃÇ¿ÉÒÔʹÓÃPM2À´Æô¶¯ºÍÖÎÀíÕâЩNode.jsÓ¦ÓóÌÐò£º
pm2 start app.js -i 3
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÏÂÁîÖУ¬ÎÒÃÇʹÓÃPM2Æô¶¯Èý¸öÀú³ÌÀ´´¦ÀíÕâЩӦÓóÌÐò¡£PM2½«×Ô¶¯¼à¿ØÕâЩÀú³Ì£¬²¢ÔÚ±¬·¢¹ÊÕÏʱ¾ÙÐÐ×Ô¶¯ÖØÆô¡£
×ÛÉÏËùÊö£¬Ê¹ÓÃNGINXºÍPM2¿ÉÒÔ¹¹½¨¸ßЧµÄVPSÇéÐΡ£NGINXÌṩÁ˸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀí¹¦Ð§£¬¶øPM2ÔòÌṩÁËÇ¿Ê¢µÄÀú³ÌÖÎÀíºÍ¹ÊÕϻָ´ÄÜÁ¦¡£Í¨¹ýÍŽáʹÓÃÕâÁ½¸ö¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨ºÍÖÎÀíÎȹ̺Ϳɿ¿µÄÓ¦ÓóÌÐòЧÀÍ¡£
ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇ»¹¿ÉÒÔƾ֤ÏêϸÐèÇó¾ÙÐнøÒ»²½µÄÉèÖúÍÓÅ»¯¡£Í¨¹ýºÏÀíµÄ¸ºÔØƽºâºÍ¼¯ÈºÉèÖã¬ÎÒÃÇ¿ÉÒÔʵÏÖ¸ü¸ßµÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£±ðµÄ£¬°´Æڵļà¿ØºÍÈÕÖ¾ÆÊÎöÒ²ÊÇÈ·±£ÏµÍ³ÎȹÌÔËÐеÄÖ÷Òª»·½Ú¡£
Òò´Ë£¬Ï£Íû±¾ÎÄÌṩµÄNGINXºÍPM2µÄ´úÂëʾÀýÄܹ»×ÊÖú¶ÁÕß¹¹½¨Ò»¸ö¸ßЧµÄVPSÇéÐΣ¬²¢´ÓÖлñµÃ¸üºÃµÄÓ¦ÓóÌÐòЧÀÍÌåÑé¡£
ÒÔÉϾÍÊÇNGINXÓëPM2: ¹¹½¨¸ßЧµÄVPSÇéÐεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡