Nginx is small, fast and light weight alternative of Apache. But Nginx also needs few tweaking to optimize it. In this tutorial we are going to learn three tweaks to optimize a Nginx server and improve its performance.
Step 1: Login
Login to your server via SSH as root user.
Step 2: Backup Nginx Configuration file
We are going to make few modification in Nginx configuration file. So, let’s first make a backup copy before doing any editing. Use below command to create a backup copy of Nginx configuration file.
cp /etc/nginx/nginx.conf /etc/nginx/nginx-backup.conf
Step 3: Find number of CPU cores
Use below command to find number of CPU cores you have on the server / VPS.
grep processor /proc/cpuinfo | wc -l
This will show a number like 4.
Step 4: Open Nginx Configuration file
Open Nginx configuration file in any text editor like nano.
nano /etc/nginx/nginx.conf
Step 5:
Change value of worker_processes to number of CPU cores we have on the server. We got this value in step 3.
Step 6:
Below worker_processes line, add following line. This is not present in default settings.
The value of worker_rlimit_nofile should be:
Number of cores X 5,000
worker_rlimit_nofile 20000;
Step 7:
Now find the line worker_connections. Change its value to:
Number of cores X 1,000
worker_connections 4000;
Step 8: Save configuration
Save Configuration file: Ctrl + O
Exit Nano Editor: Ctrl + X
Step 9: Restart Nginx
Use below commands to reload and restart Nginx.
service nginx reload service nginx restart
Done!!