Nginx is a free open source high performing and lightweight web server that is used as the load balancer, reverse proxy, HTTP cache, and mail proxy. Although Nginx is quite new as compared to other web servers, its popularity is rising due to its high performance. With your default Nginx configuration, you might get fast performance but we can boost the Nginx performance at its best by changing some configurations.

In this article, you will get to know 8 different best ways to boost Nginx for better performance. To demonstrate the example in this article I have installed Nginx on the Ubuntu 20.04 LTS system.

Modify Worker Processes

All web server requests in Nginx are processed by a worker process. In Nginx worker processes are architect as multiple workers processes to process the request and one master process is responsible for managing all the worker processes as well as analyzing the configuration. In the default configuration of Nginx, the worker process parameter is set to auto which spawns the worker process according to the available CPU core. As recommended by the official docs of Nginx it’s the best way to keep the worker process according to the available CPU core so auto is recommended parameter. If you are curious about how many cores your processors have simply run the following command.

$ grep processor /proc/cpuinfo | wc -l

Get CPU info

You can change the default value of the worker process from the Nginx config file which is located at /etc/nginx/nginx.conf. If your server is experiencing higher traffic and you need to add more worker processes it’s better to upgrade the server to more core processors.

Configure worker processes

Enhancing Worker Connections Limit

Worker connection is the total number of simultaneous connections each available worker process can manage. By default, the worker process can manage 512 connections at a time. Before modifying the worker connection value you must check the max connection system to allow using the following command to update the connection config according to it.

$ ulimit -n

Ulimit

To enhance the Nginx to its full potential set the worker connection value to the max connection system allow by the system in nginx.conf file.

Configure worker connections

Implementing Content Compression

For web content compression Nginx uses gzip to increase content delivery time and decrease network bandwidth usages. In the configuration, you could find the gzip config in the commented state but you can uncomment and modify the gzip according to your need. As gzip compression process uses system resources if you have limited resources modify the configuration according to it such as compressing only a specific type of file, compression level, etc.

Content compression

Caching Static Content

In this modern-day web development, most of the contents are statically served to the browser or client so caching the static files will load the content faster. It will also decrease the connection request to the Nginx as contents are loaded from the cache. To start the caching process add the following directive to your Nginx virtual host config file.

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

The above directive caches the resource file for 30 days. You can set the cache expiry date according to your need.

Buffering

Buffering can make the communication between client and server more efficient as its holds part of the response until the buffer fills. If the response is too high than the actual buffer size the Nginx will then write the response to the disk which may lead to a performance issue. You can update the following directive to adjust the buffer size according to your requirement.

Client_body_buffer_size: It determines the actual buffer size that is used to hold client response data.

Client_header_buffer_size: It manages the size of the client header. Normally setting the value to 1k is good enough.

Client_max_body_size: It limits the max body response allowed to the client. If the body size exceeds its value, Nginx will throw the error with “Request Entity Too Large”.

To adjust the buffering size add the following directive within the http section.

http {
…
client_body_buffer_size 80k;
client_max_body_size 9m;
client_header_buffer_size 1k;
...
}

Access Log Buffering

Logging is one of the pivotal roles in debugging the issue and auditing. As logging stores every request data which affects both I/O cycles and CPU that result in performance issues. You can reduce this kind of impact by enabling buffering to the log. Once the buffer size reaches its limit, Nginx writes buffer content to log. You can enable buffering by adding buffer parameters with size values to the access log directive.

access_log /var/log/nginx/access.log main buffer=16k;

Or you can disable the access log (if not needed) in the following way.

access_log off;

Limiting Timeout Values

Limiting the timeout value will enhance Nginx’s performance. Nginx will wait for the client’s body and header request for the given time period. If they do not receive the response data in time, Nginx triggers a time-out for the respective client. The time-out value can be managed by the following directive. To set the timeout duration, copy-paste the directive given below within the http section.

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

Client body and header timeout is a time period Nginx to read header and body from the client request. If not completed in time the request is terminated with time out error. Keepalive_timeout is the duration after nginx close the client connection keep-alive connection stay open. Send_timeout is the duration for which the client must receive the response sent by Nginx.

Open File Cache

In Linux almost everything is a file, when open_file_cache is used, the file descriptor and all the frequently accessed files are cached to the server. Especially when serving the static Html files using open file cache will enhance the Nginx performance as it open and store cache in memory for a given interval. Put the following directive of open_file_cache in the http section to initiate the caching.

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

Conclusion

These are the 8 ways to increase the Nginx performance by simple modification of the Nginx config file. I hope reading this article will help you to initiate the Nginx performance boost.

Nginx Performance Tuning

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn