9 steps to optimize your nginx web server

Google uses it’s Page speed ranking algorithm to rank website listings. A fast website is always important if you want your users to visit  your website again. In this post, I am going to discuss on 9 steps to optimize your nginx web server

STEP1: Keep the number of worker processes to be same as number of processor cores on the server.

The worker_processes directive tells VM as to how many workers should be launched when nginx starts up.
A recommened setting is to keep this number to be same as number of CPU cores on the VM.

You can calculate the number of CPU cores on your server using below command
grep processor /proc/cpuinfo | wc -l

We should also configure worker_connections directive properly, This value determines how many clients can be served by nginx simultaneously.
The default value is 768. The capacity of your nginx server can be determined by multiplying worker_processes with worker_connections.

vi /etc/nginx/nginx.conf

worker_processes 2;
worker_connections 1024;

 

For a 2-core processor with above configuration, Nginx can server 2048 concurrent connections.

STEP2: Optimize gzip compression for static contents

Gzip compression is the most common optimization parameter. This parameter can save your server’s network bandwidth utilization & speed up response time.
When gzip compression is enabled for static resources, Nginx will compress them & transfers. The client browser then decompresses them & renders the resource.

Following is the basic gzip compression configuration from nginx.conf

gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;

STEP3: Leverage browser caching to reduce unnecessary server load

Another important parameter which greatly contributes to the performance of your website.
If your static assets like css, images & js files don’t change frequently, it’s possible to cache them in client’s browser.
This will save the bandwidh & increase performance.

Following setting can be placed in your “server” block of your virtual host

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}

 

STEP4: Integrate with Pagespeed module for better performance

An open source module for nginx/apache ships with latest Web optimization features.
For installing latest version of this module, nginx needs to be compiled with it’s latest package.
Click here to learn How to Build ngx_pagespeed from source.

STEP5: Enable keep-alive connections

We should configure keepalive_timeouts value to be a minimum. The recommended value is between 10 to 15.
This value determines how long the worker connection is kept alive on the client browser so that all the new client requests with be served through this connection.

Modify nginx.conf & put this value inside “http” block

keepalive_timeout 15;

STEP6: Enable shared session cache when on HTTPS

When nginx is on HTTPS, SSL Handshake is the most CPU-Intensive operation.
There are two ways to minimize the number of these operations per client:

  • By enabling keepalive connections to send several requests via one connection
  • Reuse SSL session parameters to avoid SSL handshakes for parallel and subsequent connections

To enable this, following configuration needs to be placed in “http” block inside nginx.conf

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

Refer this guide for more details http://nginx.org/en/docs/http/configuring_https_servers.html

STEP7: Implement tmpfs file system for Cache directory.
A RAM directory/folder is always much faster than a directory based on normal hard disk, but it can lost all data when you reboot the server,
so the ideal is always to use this RAM directory to store cache files, that can easily re-created after the reboot.

Follow the below steps to enable tmpfs file system.
In this example, i have configured a WordPress plugin to cache the website files under /var/www/html/wp-content/cache

1. Configure a tmpfs partition on a new Directory.

mount -t tmpfs -o size=2G tmpfs /var/www/html/wp-content/cache

2. To make this partition survive after reboot, Add following entry to /etc/fstab file

tmpfs /var/www/nginxtips.com/wp-content/cache tmpfs defaults,size=2G 0 0

Now test your website & you should see faster response times after a few hits.

STEP8: Have a CDN to cache static assets

Cloudflare has a very intelligent caching system & CDN. I would recommend this if your website has users worldwide.
Cloudflare also has threat protection system for your website, which can be enabled on a single click from their control panel.
Click here  to learn more about Cloudflare.

STEP9: Serve static assets using seperate domain
Having static assets on a seperate subdomain will help in increasing the load time of your site.

Thoughts or Suggestions?.

Author: Ahmed I

I am a Linux System Admin

Leave a comment