Introduction
In this fast paced world where everything is getting online, you can’t afford downtime. Speed and optimization is the most challenging part of ever-evolving computer age. Performance is directly proportional to user experience. You yourself will close the website if it is taking too much time to load. Nginx is one of the widely used web server and it is an alternative of Apache 2. It is popular for handling heavy traffic and for its stability. It is very user friendly and easy to configure. In this article we will see how we can optimize Nginx to give its best performance. Here are some of the useful tips and tricks you can apply on your Nginx hosts to load your sites faster.
Cache Resources
Every website has pages, images and other stuff which remains mostly unchanged during the visitor’s session on the site. Almost 30 percent data on the modern day web pages is static, such content should be cached in order to improve performance of Nginx. Caching will give you two benefits.
1) It will load the content faster as the static data on the page will be cached to the browser or to the nearby caching server which will reduce the load time of page as the request will be served without Nginx involvement.
2) Second benefit is less connection requests on Nginx server as the data will be loaded by the cache server so eventually it will decrease the load on your server. You can set the following directive in Nginx block in order to enable caching.
If the content remain unchanged , then this parameter will cache it for 365 days.
Adjust Worker Process
Servers now are multi-threaded and multi processes; Nginx has the ability to meet these modern day techniques. The default settings don’t allow Nginx to handle multiple workloads but you can make certain changes in the configuration file to make it work as multi-threaded web server. There is “worker_processes” parameter in nginx which will make it use multiple processors available in the system.
Open the file /etc/nginx/nginx.conf and change the “worker_processes auto;” parameter to :
Increase worker connections
Worker connection is related to worker process; i.e how many connections each worker processes can maintain. If you have enabled 1 worker process and you have set this value to be 10 then Nginx will be able to handle 10 connections. You can change the value depending upon the intensity of traffic on your website. All the connections exceeding your value will be queued or timed out so this parameters should be set keeping in mind all the aspects.
As shown in the following screenshot, we have set 1024 connections for each process.
Optimization of Timeout values
Timeout factor can improve the performance of your web server. There are several timeout parameters which can be set and each parameter has its own functionality. Client_body_timeout and client_header_timeout are the parameters which wait for the header and body of client request; and if it’s not received in this time value then the client’s connection gets timed out. Keepalive_timeout is the polling time in which server polls the client connection after specific time to check its availability and if there is no response received in the defined time, it gets timed out. You can add these parameters to your Nginx configuration.
client_header_timeout 12;
keepalive_timeout 15;
Compression
Gzip compression technique has became popular in web servers because the transfer of the compressed data is much faster as compared to the normal one. Following parameters need to be set in the nginx configuration for enabling and using Gzip compression for your web content.
gzip_comp_level 2;
gzip_min_length 1000;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;
Best approach is to enable it for large files as it consumes CPU resources as well.
Buffers
Buffer size tweaking is an important task as low value setting for this parameter will make Nginx start writing the data to temporary file; which will result in increase of disk I/O. Lets discuss all such parameters one by one.
1) Client_body_buffer_size: This is the buffer size of the post requests which visitor is posting on the website. A good choice is around 128k.
2) Client_max_body_size: This parameter sets the maximum body of buffer size. Large requests appear to be dangerous sometimes so setting this parameter will display “Request Entity Too Large” to the client.
3) Client_header_buffer_size: This option is to restrict header size of a client request. Normally 1000 (1k) is a very good choice.
You should add the above mentioned parameters in /etc/nginx/nginx.conf.
client_max_body_size 10m;
client_header_buffer_size 1k;
Disable Access Logs
Nginx logs have too much information coming in them as each and every action is being logged. To improve its performance you can disable these logs and it will eventually save disk space also. Nginx configuration file should contain following parameter if you are looking to disable access logs.
TCP_nodelay & TCP_nopush
These parameters are important on the network level as every packet is being analyzed on core level, here are some details about these parameters.
1) TCP_nodelay: This parameter allows you to prevent your system from buffering data-sends and all the data will be sent in small bursts in real time. You can set this parameter by adding the following line
2) TCP_nopush: This parameter will allow your server to send HTTP response in one packet instead of sending it in frames. This will optimize throughout and minimize bandwidth consumption which will result in improvement of your website’s loading time.
Open_file Cache
On Linux based operating systems everything is being done using files. Open_file_cache parameter allows the server to cache file descriptors and all the frequently accessed files. You can enable this tweak by adjusting the following parameters.
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
Connection Queue
You can make some kernel changes for optimization of Nginx. The parameter “net.core.somaxconn” and “net.ipv4.tcp_max_tw_buckets” allows you to change the length of connections which need to be accepted by Nginx. If you set these parameters too high it will be queued in the operating system before they are being handed over to Nginx. Following are the sample configurations done in /etc/sysctl.conf
net.ipv4.tcp_max_tw_buckets = 1440000
Conclusion
Nginx is a very powerful web server and can serve websites of any magnitude and traffic. However, it is always recommended to tweak it to your needs so that your websites should respond in timely manners. The above mentioned tweaks, if followed, should make your web server capable to cope with medium to high size traffic sites.