Ubuntu 20.04 LTS Nginx Performance Optimization

Ubuntu 20.04 LTS Nginx Performance Optimization

Posted on

Nginx is a free and open-source internet server that acts as a load balancer, reverse proxy, HTTP cache, and mail proxy. Though Nginx is a comparatively new internet server compared to others, its reputation is rising on account of its excellent efficiency. Whilst you might obtain fast pace with the default Nginx configuration, we will optimize Nginx efficiency by altering a couple of variables.

This put up will focus on eight various strategies for optimizing Nginx’s efficiency. I put in Nginx on an Ubuntu 20.04 LTS server to show the situation on this put up.

Modification of Employee Processes

A employee course of processes all internet server requests in Nginx. In Nginx, employee processes are divided into many employee processes that every course of a request, with a grasp course of in command of controlling all of the employee processes and evaluating the settings. The employee course of parameter is about to auto by default in Nginx, which spawns the employee course of primarily based on the accessible CPU cores. As suggested by Nginx’s official documentation, that is the optimum strategy to keep the employee course of in accordance with the accessible CPU cores, and so auto is the popular setting. In case you’re curious concerning the variety of cores in your processor, execute the next command.

$ grep processor /proc/cpuinfo | wc -l

word image 92The default worth for the employee course of may be modified within the Nginx configuration file discovered at /and so forth/nginx/nginx.conf. In case your server is experiencing extra demand and you could add extra employee processes, it’s endorsed that you just improve to a multi-core CPU.

word image 93Boosting Employee Connections Restrict

The quantity of simultaneous connections that every accessible employee course of can handle is known as the employee connection depend. By default, the employee course of is restricted to managing 512 connections. Earlier than altering the employee connection worth, you will need to first examine the system’s most connection restrict to make sure that the next command updates the connection configuration appropriately.

$ ulimit -n

word image 94Set the employee connection worth to the utmost variety of connections allowed by the system within the nginx.conf file to maximise the efficiency of Nginx.

word image 95Content material Compression Implementation

Nginx employs gzip to compress internet content material in an effort to cut back community bandwidth utilization and increase content material supply time. The gzip configuration is commented within the configuration, however you may uncomment it and alter it to fit your wants. As a result of the gzip compression course of takes system sources, when you’ve got restricted sources, alter the configuration to compress solely a specified kind of file, compression stage, and so forth.

word image 96Static Content material Caching

In as we speak’s internet growth, the vast majority of the fabric is statically provided to the browser or consumer, so caching the static information will make the content material load sooner. It would additionally cut back the variety of connection requests to Nginx as the fabric is loaded from the cache. To start caching, add the next directive to your Nginx digital host configuration file.

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

The useful resource file is cached for 30 days by the directive above. You may customise the cache expiry date to fit your wants.


Buffering can enhance the effectivity of client-server communication by storing a portion of the response till the buffer is full. If the response exceeds the precise buffer measurement, Nginx will write the response to disk, which can trigger a efficiency subject. You may change the buffer measurement within the following directive to fit your wants.

Shopper physique buffer measurement: This parameter specifies the scale of the buffer used to retailer consumer response information.

Shopper header buffer measurement: This property controls the scale of the consumer header. Usually, setting the worth to 1k suffices.

Shopper max physique measurement: This property restricts the consumer’s most physique response. If the physique measurement exceeds its restrict, Nginx will throw an error message stating “Request Entity Too Giant.”

To alter the buffering measurement, embody the next directive within the HTTP part.

http {
client_body_buffer_size 80k;
client_max_body_size 9m;
client_header_buffer_size 1k;

Buffering for Entry Logs

One of the essential capabilities in debugging and auditing is logging. As a result of logging retains all request information, it impacts each I/O cycles and CPU efficiency, leading to efficiency considerations. You may lower the influence of the sort of influence by enabling log buffering. Nginx writes buffer content material to log when the buffer measurement reaches its restrict. Buffering may be enabled by including buffer parameters with measurement values to the entry log directive.

access_log /var/log/nginx/entry.log important buffer=16k;

Alternatively, you may disable the entry log (if it’s not required) within the following method.

access_log off;

Setting Timeout Limits

Nginx’s efficiency will enhance if the timeout worth is decreased. Nginx will anticipate the consumer’s physique and header requests for the required period of time. If they don’t obtain the reply information in a well timed method, Nginx causes the consumer to day out. The next directive can be utilized to regulate the time-out worth. Copy and paste the directive under into the HTTP part to set the timeout period.

client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 13;
send_timeout 10;

Shopper physique and header timeout is the period of time Nginx takes to learn the header and physique from the consumer request. If the request shouldn’t be completed in time, it’s terminated with a time-out error. Keepalive timeout is the period of time that the keep-alive connection stays open after nginx closes the consumer connection. The ship timeout specifies how lengthy the consumer should anticipate Nginx’s response.

Open the File Cache

When the open file cache perform is invoked, the file descriptor and all often visited information are cached to the server. Utilizing open file cache, particularly when serving static HTML information, will enhance Nginx pace by opening and storing cache in reminiscence for a specified interval. To begin the caching, add the open file cache directive to the HTTP part.

http {
open_file_cache max=1024 inactive=10s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;


These are the eight strategies for enhancing Nginx efficiency by straightforward modifications to the Nginx configuration file. I hope that studying this text will help you in beginning the Nginx efficiency enchancment.

offshore vps