Nginx中worker_processes是什么?Heroku各计划对应数量是多少?
Hey there! Let's break down Nginx's worker_processes and how it works with Heroku, since you're tuning your app's Nginx config.
worker_processes in Nginx? Think of worker_processes as the number of independent "worker" processes Nginx launches to handle incoming requests. Each worker runs separately, taking care of tasks like serving static files, proxying requests to your application, managing client connections, and more.
The core goal of this setting is to maximize your server's CPU utilization. If your server has multiple CPU cores, having one worker per core lets each core focus on its own set of requests, avoiding bottlenecks where a single worker can't keep up with high traffic. It also adds a layer of redundancy—if one worker crashes unexpectedly, the others will keep handling requests without downtime.
worker_processes on Heroku: Free vs Paid Plans That config snippet you found:
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>
uses Heroku's environment variable NGINX_WORKERS to set the number of workers, with a fallback to 4 if the variable isn't set. Here's how it maps to Heroku's plans:
- Free Plan: Heroku's free dynos are single-core. Running multiple workers here doesn't boost performance—in fact, it adds unnecessary overhead from context-switching between processes on the same core. For free dynos, you should override the default to set
worker_processes 1to optimize resource usage. - Paid Plans: The ideal number depends on your dyno's CPU core count:
- Hobby & Standard 1X dynos: Single-core →
worker_processes 1 - Standard 2X dynos: Dual-core →
worker_processes 2 - Performance M dynos: 4-core →
worker_processes 4(matches the fallback value in your config) - Performance L dynos: 8-core →
worker_processes 8
- Hobby & Standard 1X dynos: Single-core →
In most cases, Heroku will automatically set the NGINX_WORKERS environment variable to match your dyno's core count, but if you need to tweak it manually, aligning the number of workers with your dyno's CPU cores is the best practice.
内容的提问来源于stack exchange,提问作者Igor-Vuk

