The error occurs when you place Nginx's main configuration directives inside the wrong context. The worker_processes
directive must be placed in the main configuration context (nginx.conf), not within a server
block.
Here's how your configuration should be properly structured:
# Main context in nginx.conf
worker_processes 4;
worker_rlimit_nofile 8192;
worker_priority 0;
worker_cpu_affinity 0001 0010 0100 1000;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
server {
server_name example.com;
listen 80;
root /var/www/devsites/trademob/tm-hp-v2/;
}
}
Many administrators make these mistakes when configuring worker processes:
- Placing worker directives inside http or server blocks
- Using invalid CPU affinity masks
- Setting worker_processes to a number higher than available CPU cores
After modifying your configuration, always test it before applying changes:
sudo nginx -t
This will validate your configuration syntax and show any errors.
For optimal performance on a 8-core server with high traffic:
worker_processes auto;
worker_rlimit_nofile 200000;
worker_cpu_affinity auto;
events {
worker_connections 16384;
multi_accept on;
use epoll;
}
This configuration automatically matches worker processes to CPU cores and increases file handle limits for high traffic scenarios.
When you encounter the error "worker_processes directive is not allowed here", it means you've placed a main-level Nginx directive inside a server block context. The worker_processes
directive must be configured in the main configuration file (typically /etc/nginx/nginx.conf
), not within server blocks defined in sites-enabled.
Here's how your configuration should be structured:
# Main context in nginx.conf
worker_processes 4;
worker_rlimit_nofile 8192;
worker_priority 0;
events {
worker_connections 1024;
}
http {
# Server block in separate file (sites-enabled/default)
server {
server_name example.com;
listen 80;
root /var/www/devsites/trademob/tm-hp-v2/;
}
}
Many developers make these configuration mistakes:
- Placing main directives in server blocks
- Forgetting the events block
- Mixing main and http contexts
Instead, follow this hierarchy:
1. Main context (worker_processes, etc.)
2. Events context (worker_connections)
3. Http context (all server blocks)
After making changes, always test your configuration:
sudo nginx -t
sudo systemctl restart nginx
For optimal performance on a 4-core server:
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 200000;
events {
worker_connections 16384;
multi_accept on;
use epoll;
}