title: Using Nginx to Load Balance Node.js Application on a Multi CPU Server
body: <p>There are a bunch of answers around this on Stackoverflow, but I'm not sure I've found anything really complete, or that up to date.</p>
  
  <p>We are using node.js with node clusters on elastic beanstalk. This allows us to, in theory, use all the CPUs on a box by spinning up individual runtimes of node, one for each CPU, balances the calls between the different runtimes, and restarts the individual runtimes if they die for some reason.</p>
  
  <p>However, clustering currently is suspect in how it handles the load balancing - it tends to continuously pick one or two CPUs, at least on Linux. Version 12 relieves this issues, but AWS does not support it yet.</p>
  
  <p>I'm pretty sure I can configure nginx on beanstalk to handle the load balancing, though I'd love to see a working example of the nginx conf file. But I'm wondering if anyone has figured out how to start up the correct number of node runtimes, based on CPUs, and also how the restart works - if a worker dies, I want to start a new one up immediately. Beanstalk handles the main cluster manager now, and the cluster manager takes care of the workers.</p>
  
  <p>I'm open to something like Docker in beanstalk, also, but we like being in beanstalk for the ease of set up, scaling and management.</p>