Optimization of NGINX Server for Best Performance

NGINX is the fastest Web Server in the World if it is properly optimized. Here in this tutorial we will discuss few optimization tips for optimum performance.

Step 1: Optimize NginX according to your VPS hosting hardware configuration

There are different levels of VPS. For example, a basic level of VPS is 512MB RAM, 1 core processor, 20GB hard disk storage. The next level i.e. level 2 VPS, say, 1GB RAM, 2 core processor, 40GB hard disk and so on. Depending upon the no. of cores you should specify certain parameters in nginx configuration file.

sudo nano /etc/nginx/nginx.conf

Change the default value of 4 at the second line of the configuration file to a value equals to the no. of core you have in your VPS configuration.

worker_processes 2;

If you have a level 2 VPS then change the value to 2.

Step 2. Enable gZip Compression

Check your site at GTMetrix whether your website’s components are gZip compressed or not. If not then you need to edit nginx.conf file.

sudo nano /etc/nginx/nginx.conf

Uncomment the following lines:-

gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Step 3: Disable Access Log

By default nginx writes access log and error log in two different files namely access.log and error.log. There is no point to log access data because a busy site have tons of visits for which recording data for each visit to access.log file consume a lot of resources. So, it is better to disable access log in a busy site.

# Logging Settings

# access_log /var/log/nginx/access.log;
access_log off;
error_log /var/log/nginx/error.log;

We just commented access_log and inserted a line beneath the log path. We also deleted the access log file

sudo rm /var/log/nginx/access.log

Step 4: Lower the keepalive_timeout

By default keepalive_timeout is set to 65 seconds. By reducing this value increase the site performance.

keepalive_timeout 15;

Leave a Reply

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