Optimize NGINX for high-traffic websites

Nginx is the most famous and widely used web server application for websites today. For static websites, Nginx is no less than a boon, while for dynamic websites it serves the need of a most efficient and lightweight webserver.

The default Nginx configuration is enough to take moderate web traffic battle, but you can optimize it more. In this article, I will show some of the tips for optimizing Nginx for high-traffic websites.

worker_processes auto;

events {
    worker_connections 768;
}

http {
    access_log off;
    keepalive_timeout 65;
    gzip on;
    # Add the below directives to further enhance the Gzip.
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 1;
    gzip_min_length 256;
    gzip_type text/css text/javascript text/plain text/xml application/javascript application/json application/x-javascript application/xml;
}

worker_processes

It is a single-threaded process that handles all the tasks. If your system has multiple cores it can take all advantage of the same and make things much smoother for you just like our high-end computers.

Let’s assume Nginx is doing CPU-intensive work such as TLS and Gzipping, and the server has 2 or more CPU cores, then you may set worker_processes to be equal to the number of physical or virtual CPU cores. This way worker process will handle both the jobs efficiently and faster.

Or on the safer side, it is set to “auto” by default if you don’t know.

worker_connections

It means the total number of network connections. Make it at least 10240 and do not under-limit it so that server can accept as many as new connections. Every single network connection is counted towards a worker connection.

multi_accept

By default, it is not there in configuration and set to off. Once set to on, it allows the system to accept as many as connection requests without any proper management. Since Nginx is efficient on this side, let it off.

access_log

Logs are not necessary if you know everything is working fine and under your control, you can leave it on with a small disk read and write penalty but you can set it off for better performance, we can disable the access log completely to avoid additional read and writes and put that performance to more important load.

keepalive_timeout

It means keeping the connection alive in case the users refresh or load another webpage. The value should be in numbers and in seconds – like 60 for 1 minute or 3600 for 1 hour.

Bloggers can set a long value like 3600, while downloader or wallpaper sites can set this value to a low like 30.

gzip

Gzip is a compression mechanism that compresses the webpage and its elements so that users have to download less data. This increases the speed of a website. But turn it on only when you are using some sort of cache otherwise there will be a lot of stress on the CPU for constant content zipping.

Under Gzip, you can set compression level and file type to compress. I’ve added all of them to the above code.

There is no need of adding or modifying any other directive as the latest version of Nginx is already well optimized for most things. The above optimizations are left because everyone needs their own settings here.

Leave a Reply

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