Nginx is a free and open-source, high performance web server software used for its stability, simplicity in configuration which uses low resource consumption.
As with any other server or software, Nginx also needs fine tuning for attaining better performance. This tutorial will cover the basics of optimizing an nginx configuration.
System Pre-Requisites
A droplet with Debian 7 server setup already done
Nginx server already installed and configured.
A basic understanding of Linux.
Tuning Worker Processes and Worker Connections
First we will need to tune two directives – worker processes and worker connections. Let’s get to the bottom of these directives control. Worker_processes is an important directive that controls a number of worker processes Nginx is already running. Worker processes is the one that helps in letting your virtual server know about how many workers to be spawn after it has bound to the actual IP and ports. Usually 1 worker process is run per core. Giving any number above this will probably leave any idle processes lying around.
For understanding which number you will need to set worker_processes to, just analyse the number of cores you have on your set up. If you are using a 512MB set up, then it will be only one core. You can check your cores and adjust the number accordingly.
if you grep the cpuinfo:
grep processor /proc/cpuinfo | wc -l
Suppose, it returned the value 1. That number is the number of cores on your set up.
Now, let’s see what worker connections is all about. The worker connection command will inform the worker processes about how many people can be served by nginx simultaneously. By default, the value is 768. But, assuming that every browser makes at least 2 connections, this number can be halved. Hence, it’s recommended to adjust the worker connections in order to attain its full potential.
Now, let’s check the core’s limitations using the ulimit command:
ulimit -n
In this tutorial, since we are using a 512MB droplet, the number returned is 1024. We need to update the config file:
worker_processes 1; worker_connections 1024;
Note: the number of clients that can be served is multiplied by the amount of cores. In this case, we can serve 1024 clients per second. However, this figure can be altered using the keepalive_timeout directive.
Tweaking the Buffer Size
Another important tweak that we need to do is to the buffer size. Having a very low buffer size will require Nginx to write to a temporary file, thereby wanting the disk to read and write constantly.
Before tweaking the buffer size, let’s understand a few more directives:
- client_body_buffer_size – This directive handles the client buffer size. POST actions will be sent to nginx. POST actions are usually the form submissions.
- client_header_buffer_size – This directive handles client header size. Usually, it will have a size of about 1K.
- client_max_body_size – this is the maximum size allowed for a client request. It is responsible for throwing a 413 error (request entity too large) if the maximum size is exceeded.
- large_client_header_buffers – This is the maximum number and size of buffers for large client headers.
client_body_buffer_size 10K; client_header_buffer_size 1k; client_max_body_size 8m; large_client_header_buffers 2 1k;
Setting Timeouts
Setting the timeouts also can improve the performance drastically. The two directives – client_body_timeout and client_header_timeout will decide the time for a server to wait for a client body or client header to be sent after request. In case if the body or header is not sent, the server will issue a 408 error or request time out error.
The keepalive_timeout will assign the timeout for keeping connections alive with the client. After the stipulated time, nginx will close connections with the client.
The send_timeout directive – after this time, the client will not take anything and nginx will shut down the connection.
client_body_timeout 12; client_header_timeout 12; keepalive_timeout 15; send_timeout 10;
Gzip Compression
Another way for optimizing performance is by reducing the amount of network transfer nginx deals with. However, increasing the gzip_comp_level to a high value will result in wasting cpu cycles.
gzip on; gzip_comp_level 2; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/x-javascript text/xml text/css application/xml;
Static File Caching
Files that do not change and are regularly served can be set an expire header. The directive will have to be added to the nginx server block.
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; }
You can update the file types to match your requirements.
Turning Off Logging Functionality
Whenever a request hits the VPS, nginx will log every request to a log file. You may wish to turn off this functionality off. You may already be using analytics for monitoring.
You can set the access_log directive off.
access_log off
Once the changes are done, save and close the file. Then Restart:
sudo service nginx restart
Note: Here, we have covered few ways to optimize the nginx configuration. You may wish to research more about improving your machine performance with load balancing and horizontal scaling.