You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which @angular/* package(s) are relevant/related to the feature request?
platform-server
Description
Currently to scale Angular SSR beyond 1 core if you use PM2 you must have nginx in front with configured upstreams on multiple ports. PM2 configuration is also complicated with increment_var setting for port. Any change to a number of instances requires changing nginx configuration.
Proposed solution
Angular SSR should be able to run in PM2 cluster mode for easy scaling
Regardless of using clustering or not. It's highly recommended to place a load balancer like Nginx in front of your Node.js application. Direct traffic should generally never reach the Node.js server itself.
PM2 and Cluster Mode
When using PM2 in cluster mode, or utilizing the standard Node.js cluster module, all worker processes typically share the same network port. The primary process (or PM2) acts as a load balancer and manages the distribution of incoming connections to these workers. Why do you want to have different ports?
I tried PM2 clustering with Angular SSR, and it worked right away without needing any changes.
Clustering Implementation Example without PM2
Implementing basic clustering is also straightforward using the native Node.js cluster API. The following code snippet demonstrates a primary process forking workers and ensuring a dead worker is replaced:
if(cluster.isPrimary){// Primary/Master process logicconsole.log(`Master process ${process.pid} is running`);// Fork workers based on CPU countfor(leti=0;i<numCPUs;i++){cluster.fork();}// Auto-restart failed workerscluster.on('exit',(worker,code,signal)=>{console.log(`Worker ${worker.process.pid} died. Spawning a new one.`);cluster.fork();// Spawn a replacement worker});}else{// Worker process logic (listens for requests)constport=4000;app.listen(port,(error)=>{if(error){throwerror;}console.log(`Node Express server listening on http://localhost:${port} (Worker ${process.pid})`);});}
@alan-agius4 I fully agree nginx should be in front. I believe it's not stated in documentation.
Why do you want to have different ports?
I don't understand the question. I don't want to have multiple ports but I need to have them because cluster mode is not supported and in fork mode each instance must have it's own port.
I tried PM2 clustering with Angular SSR, and it worked right away without needing any changes.
I tried few months ago and it would just hang on start. Also I found references on the internet that it's not supported.
Are you sure it works for you? If yes I will test again.
Now I tried with ng new project and it works like you said. yey...
For some reason it does not work with my project that also uses latest angular and stock server.ts.
Looks like something from inside angular project is preventing it to ran and that's strange....
It was about permissions on a logs folder.
In fork mode logs are written by pm2 process, in cluster mode it's run by child process.
On my server those are not same users.
Reacted by Alan Agius
angular-automatic-lock-bot commented on Nov 20, 2025
Activity
alan-agius4 commentedon Oct 15, 2025
Regardless of using clustering or not. It's highly recommended to place a load balancer like Nginx in front of your Node.js application. Direct traffic should generally never reach the Node.js server itself.
PM2 and Cluster Mode
When using PM2 in cluster mode, or utilizing the standard Node.js
clustermodule, all worker processes typically share the same network port. The primary process (or PM2) acts as a load balancer and manages the distribution of incoming connections to these workers. Why do you want to have different ports?I tried PM2 clustering with Angular SSR, and it worked right away without needing any changes.
Clustering Implementation Example without PM2
Implementing basic clustering is also straightforward using the native Node.js
clusterAPI. The following code snippet demonstrates a primary process forking workers and ensuring a dead worker is replaced:hheexx commentedon Oct 15, 2025
@alan-agius4 I fully agree nginx should be in front. I believe it's not stated in documentation.
I don't understand the question. I don't want to have multiple ports but I need to have them because cluster mode is not supported and in fork mode each instance must have it's own port.
I tried PM2 clustering with Angular SSR, and it worked right away without needing any changes.I tried few months ago and it would just hang on start. Also I found references on the internet that it's not supported.
Are you sure it works for you? If yes I will test again.
alan-agius4 commentedon Oct 15, 2025
@hheexx, yes I worked when using the latest version to scaffold the project.
hheexx commentedon Oct 17, 2025
I tried now with my project that uses stock server.ts and it does not work.
Application enters restart loop with status errored end no log output.
alan-agius4 commentedon Oct 17, 2025
What is your PM2 config? I tried again and it just works out of the box.
ecosystem.config.js
alan-agius4 commentedon Oct 17, 2025
Which version of Angular CLI are using to generate the project? (i.e. the globally installed version). Maybe that’s outdated?
hheexx commentedon Oct 17, 2025
Now I tried with ng new project and it works like you said. yey...
For some reason it does not work with my project that also uses latest angular and stock server.ts.
Looks like something from inside angular project is preventing it to ran and that's strange....
alan-agius4 commentedon Oct 17, 2025
Check that you have the following condition:
angular-cli/packages/schematics/angular/ssr/files/application-builder/server.ts.template
Line 54 in b1d6d2f
Closing since in an
ng newproject clustering works out of the box.hheexx commentedon Oct 20, 2025
Found it!
It's not Angular related.
It was about permissions on a logs folder.
In fork mode logs are written by pm2 process, in cluster mode it's run by child process.
On my server those are not same users.
angular-automatic-lock-bot commentedon Nov 20, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.