Nginx is a free and open-source web server that acts as a load balancer, reverse proxy, HTTP cache, and mail proxy. Although Nginx is a relatively new web server in comparison to others, its popularity is growing due to its outstanding performance. While you may receive quick speed with the default Nginx configuration, we can optimize Nginx performance by altering a few variables.
This post will discuss eight alternative methods for optimizing Nginx’s performance. I installed Nginx on an Ubuntu 20.04 LTS server to demonstrate the scenario in this post.
Modification of Worker Processes
A worker process processes all web server requests in Nginx. In Nginx, worker processes are divided into many worker processes that each process a request, with a master process in charge of controlling all the worker processes and evaluating the settings. The worker process parameter is set to auto by default in Nginx, which spawns the worker process based on the available CPU cores. As advised by Nginx’s official documentation, this is the optimal way to maintain the worker process in accordance with the available CPU cores, and so auto is the preferred setting. If you’re curious about the number of cores in your processor, execute the following command.
$ grep processor /proc/cpuinfo | wc -l
The default value for the worker process can be changed in the Nginx configuration file found at /etc/nginx/nginx.conf. If your server is experiencing more demand and you need to add additional worker processes, it is recommended that you upgrade to a multi-core CPU.
Boosting Worker Connections Limit
The amount of simultaneous connections that each available worker process can manage is referred to as the worker connection count. By default, the worker process is limited to managing 512 connections. Before altering the worker connection value, you must first check the system’s maximum connection limit to ensure that the following command updates the connection configuration appropriately.
$ ulimit -n
Set the worker connection value to the maximum number of connections allowed by the system in the nginx.conf file to maximize the performance of Nginx.
Content Compression Implementation
Nginx employs gzip to compress web content in order to reduce network bandwidth utilization and boost content delivery time. The gzip configuration is commented in the configuration, but you can uncomment it and adjust it to suit your needs. Because the gzip compression process takes system resources, if you have limited resources, alter the configuration to compress only a specified type of file, compression level, and so on.
Static Content Caching
In today’s web development, the majority of the material is statically supplied to the browser or client, so caching the static files will make the content load faster. It will also reduce the number of connection requests to Nginx as the material is loaded from the cache. To begin caching, add the following directive to your Nginx virtual host configuration file.
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {expires 30d;}
The resource file is cached for 30 days by the directive above. You can customize the cache expiry date to suit your needs.
Buffering
Buffering can improve the efficiency of client-server communication by storing a portion of the response until the buffer is full. If the response exceeds the actual buffer size, Nginx will write the response to disk, which may cause a performance issue. You can change the buffer size in the following directive to suit your needs.
Client body buffer size: This parameter specifies the size of the buffer used to store client response data.
Client header buffer size: This property controls the size of the client header. Normally, setting the value to 1k suffices.
Client max body size: This property restricts the client’s maximum body response. If the body size exceeds its limit, Nginx will throw an error message stating “Request Entity Too Large.”
To change the buffering size, include the following directive in the HTTP section.
http { … client_body_buffer_size 80k; client_max_body_size 9m; client_header_buffer_size 1k; ... }
Buffering for Access Logs
One of the most important functions in debugging and auditing is logging. Because logging retains all request data, it affects both I/O cycles and CPU performance, resulting in performance concerns. You can decrease the impact of this type of impact by enabling log buffering. Nginx writes buffer content to log when the buffer size reaches its limit. Buffering can be enabled by adding buffer parameters with size values to the access log directive.
access_log /var/log/nginx/access.log main buffer=16k;
Alternatively, you can disable the access log (if it is not required) in the following manner.
access_log off;
Setting Timeout Limits
Nginx’s performance will improve if the timeout value is reduced. Nginx will wait for the client’s body and header requests for the specified amount of time. If they do not receive the answer data in a timely manner, Nginx causes the client to time out. The following directive can be used to control the time-out value. Copy and paste the directive below into the HTTP section to set the timeout duration.
client_body_timeout 10; client_header_timeout 10; keepalive_timeout 13; send_timeout 10;
Client body and header timeout is the amount of time Nginx takes to read the header and body from the client request. If the request is not finished in time, it is terminated with a time-out error. Keepalive timeout is the amount of time that the keep-alive connection remains open after nginx closes the client connection. The send timeout specifies how long the client must wait for Nginx’s response.
Open the File Cache
When the open file cache function is invoked, the file descriptor and all frequently visited files are cached to the server. Using open file cache, especially when serving static HTML files, will improve Nginx speed by opening and storing cache in memory for a specified interval. To start the caching, add the open file cache directive to the HTTP section.
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 eight methods for improving Nginx performance by easy changes to the Nginx configuration file. I hope that reading this article will assist you in starting the Nginx performance improvement.
very useful thanks
thanks very useful broo