Three Tips To Optimize Nginx Server

Optimize Nginx
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.
ssh-cpu-cores

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.
nginx worker_processes

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;

nginx-worker_rlimit_nofile

Step 7:

Now find the line worker_connections. Change its value to:
Number of cores X 1,000

worker_connections  4000;

nginx-worker_connections

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!!

Our Recommended VPS Hosting

Leave us a Reply

Your email address will not be published. Required fields are marked *

* *