NGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯Èº
NGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯Èº£¬ÐèÒªÏêϸ´úÂëʾÀý
СÐò£º
ÔÚµ±½ñµÄ»¥ÁªÍøʱ´ú£¬¸ß¿ÉÓÃÐÔÒѾ³ÉΪÁ˹¹½¨Îȹ̿ɿ¿µÄÓ¦ÓÃЧÀ͵ÄÖ÷ÒªÒªËØÖ®Ò»¡£ÎªÁËʵÏָ߿ÉÓÃÐÔ£¬Ðí¶àÆóÒµºÍ¿ª·¢Õ߶¼×îÏÈÑ¡ÔñʹÓü¯ÈºÀ´°²ÅÅËûÃǵÄÓ¦ÓóÌÐò¡£¶øÔÚ¼¯ÈºÖУ¬Ê¹ÓÃNGINXºÍPM2×÷Ϊ¸ºÔØƽºâÆ÷ºÍÀú³ÌÖÎÀí¹¤¾ßÊǷǾ³£¼ûµÄÑ¡Ôñ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINX¡¢PM2ºÍVPS´î½¨Ò»¸ö¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯Èº£¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Ê²Ã´ÊÇNGINXºÍPM2
NGINXÊÇÒ»¸öÇáÁ¿¼¶µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¬Ëü¿ÉÒÔ×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷¡¢¸ºÔØƽºâÆ÷ºÍHTTP»º´æЧÀÍÆ÷µÈ¡£Í¨¹ýNGINX£¬ÎÒÃÇ¿ÉÒÔ½«ÇëÇó·Ö·¢µ½ºó¶ËµÄ¶à¸öÓ¦ÓÃЧÀÍÆ÷ÉÏ£¬´Ó¶øÌá¸ßϵͳµÄ¿É¿¿ÐÔºÍÐÔÄÜ¡£
PM2ÊÇÒ»¸öNode.jsÓ¦ÓõÄÀú³ÌÖÎÀí¹¤¾ß£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÀû±ãµØÖÎÀíNode.jsÓ¦ÓõÄÆô¶¯¡¢×èÖ¹¡¢ÖØÆôÒÔ¼°ÈÕÖ¾Êä³öµÈ¡£Í¨¹ýPM2£¬ÎÒÃÇ¿ÉÒÔÔÚÓ¦Óñ¬·¢Òì³£Í˳öʱ×Ô¶¯ÖØÆôÓ¦Ó㬴Ӷø°ü¹ÜЧÀ͵ĿÉÓÃÐÔ¡£
¶þ¡¢¹¹½¨NGINX·´ÏòÊðÀíºÍ¸ºÔØƽºâ
ÔÚ¹¹½¨¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯ÈºÖ®Ç°£¬ÎÒÃÇÊ×ÏÈÐèÒª´î½¨Ò»¸ö»ù´¡µÄNGINX·´ÏòÊðÀíºÍ¸ºÔØƽºâÇéÐΡ£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNGINXÉèÖÃʾÀý£º
http { upstream app_servers { server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; } server { listen 80; location / { proxy_pass http://app_servers; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪapp_serversµÄupstream¿é£¬ÆäÖÐÁгöÁËÎÒÃǵÄÓ¦ÓÃЧÀÍÆ÷µÄµØµãºÍ¶Ë¿Ú¡£NGINX»áƾ֤¸ºÔØƽºâËã·¨½«ÇëÇó·Ö·¢µ½ÕâЩЧÀÍÆ÷ÉÏ¡£Í¨¹ýÉèÖÃproxy_passÖ¸ÁNGINXʵÏÖÁË·´ÏòÊðÀíµÄ¹¦Ð§£¬½«¿Í»§¶ËµÄÇëÇóͨ¹ýNGINXת·¢µ½ºó¶ËµÄÓ¦ÓÃЧÀÍÆ÷¡£
Èý¡¢Ê¹ÓÃPM2ÖÎÀíNode.jsÓ¦ÓÃÀú³Ì
ÔÚ¹¹½¨ÁËNGINX·´ÏòÊðÀíºÍ¸ºÔØƽºâÇéÐÎÖ®ºó£¬ÎÒÃÇÐèҪʹÓÃPM2À´ÖÎÀíÎÒÃǵÄNode.jsÓ¦ÓõÄÀú³Ì¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄPM2ÉèÖÃʾÀý£º
module.exports = { apps : [ { name : "app", script : "app.js", instances : "max", exec_mode : "cluster" } ] }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪappµÄÓ¦Óã¬Ö¸¶¨ÁËÓ¦ÓõÄÈë¿ÚÎļþΪapp.js¡£Í¨¹ýÉèÖÃinstancesΪmaxÒÔ¼°exec_modeΪcluster£¬ÎÒÃǸæËßPM2ÔÚÆô¶¯Ó¦ÓÃʱ½¨É辡¿ÉÄܶàµÄÀú³Ì£¬´Ó¶øʵÏÖÓ¦Óõĸ߲¢·¢´¦ÀíÄÜÁ¦¡£
ËÄ¡¢Ê¹ÓÃVPSʵÏָ߿ÉÓÃÐÔ¼¯Èº
ÔÚÉÏÃæµÄ»ù´¡Ö®ÉÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃVPSÀ´¹¹½¨Ò»¸ö¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯Èº£¬ÒÔÌṩԽ·¢Îȹ̿ɿ¿µÄЧÀÍ¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄVPS¼¯ÈºÊ¾Àý£º
server { listen 80; location / { proxy_pass http://backend; } location /status { stub_status on; allow 127.0.0.1; deny all; } } upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËVPS×÷Ϊǰ¶ËµÄ¸ºÔØƽºâÆ÷¡£µ±ÓÐÇëÇó½øÀ´Ê±£¬VPS»á½«ÇëÇóת·¢¸øºó¶ËµÄ¶ą̀ӦÓÃЧÀÍÆ÷£¬´Ó¶øʵÏÖ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ¡£
ͨ¹ýÕâÑùµÄ·½·¨£¬×ÝÈ»ÆäÖÐһ̨ӦÓÃЧÀÍÆ÷±¬·¢¹ÊÕÏ£¬ÆäËûÕý³£ÔËÐеÄЧÀÍÆ÷ÈÔÈ»¿ÉÒÔ¼á³ÖЧÀ͵ÄÔËÐУ¬´Ó¶øÌá¸ßϵͳµÄ¿É¿¿ÐÔ¡£
½áÂÛ£º
ͨ¹ýʹÓÃNGINX¡¢PM2ºÍVPS£¬ÎÒÃÇ¿ÉÒÔ¹¹½¨Ò»¸ö¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯Èº£¬ÊµÏÖ¸ºÔØƽºâºÍ¹ÊÕϻָ´¡£ÕâÑùµÄ¼Ü¹¹¿ÉÒÔÌá¸ßÓ¦ÓõĿÉÓÃÐÔ£¬²¢ÇÒÄܹ»´¦Àí¸ü¶àµÄ²¢·¢ÇëÇó¡£Ï£Íû±¾ÎÄËùÌṩµÄ´úÂëʾÀýÄܹ»×ÊÖúÄã¸üºÃµØÃ÷È·ºÍÓ¦ÓÃÕâЩ¹¤¾ßºÍÊÖÒÕ¡£ÈÃÎÒÃÇÒ»Æð¹¹½¨Îȹ̿ɿ¿µÄÓ¦ÓÃЧÀÍ°É£¡
ÒÔÉϾÍÊÇNGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓÃÐÔµÄÓ¦ÓÃЧÀͼ¯ÈºµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡