Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

Nginx Performance Tuning Guide for Beginners (Skyrocket Performance)

SelvakumarSelvakumar

A lot of people are using Dedicated server and VPS.

There are three reasons for hiring and running a server.

  1. Traffic is high on their site.
  2. They at least have a basic knowledge of the server.( You might have advanced knowledge too)
  3. They need high performance.

All the above reasons come under Server Performance.

I know you would choose your server configuration based on your requirement and prediction.

You can decide the server configuration of your site based on existing traffic.

But:

It always doesn't work. Sometimes you will need more than that.

You will even need to upgrade your server.

This will lead you to spend money.

When we talk about hosting and website traffic, we can't avoid web server as they play major roles.

Nginx is one of the best web servers which unleash the high performance even with minimal resources.

Usually, all web servers are performing mainly based on the server configuration.

Not all the times are same for a website.

When the traffic to the website increases and goes beyond the server capacity, then your server will be down.

There are some certain ways which we can use to optimize the web server so that it will give a higher performance to the existing resources.

Here I am going to show you,

  • Worker Processes
  • Worker connection
  • Buffers
  • Timeouts
  • Gzip compression
  • Static File caching
  • Logging
Prerequisites:
  • Here we are going to use Debian 7 droplet.
  • The server should have Nginx installed

Worker Processes and Worker connection

There are two main directives that we are going to discuss here.

They are worker processes and worker connection. The worker processes work as the backbone of the server.

This directive let the virtual server know to allocate the workers once it is connected to the IP address and port.

The default worker processes allocation is 1 worker processes per core.

However, you can use more than one worker processes.

To know how much worker processes you need, you have to check your server configuration.

Based on the number of cores you can set the worker_processes.

If you want to know how many processes your server has, just execute the following command.

$ grep processor /proc/cpuinfo | wc -l

If it returns value 2, then it means your server has two cores.

The worker_connections tells the worker_processes about how much simultaneous visitors the Nginx server can serve.

The default simultaneous connection is 768.

Since each browser opens two connections with the server, the exact count will be half of the default connection.

So, we have to adjust the worker connections to unleash the full potential.

To check the core limitation use the following command.

ulimit -n

If your server has single core, you will see the following output

1024

We have to update the configuration.

sudo nano /etc/nginx/nginx.conf

worker_processes 1;
worker_connections 1024;

The number of clients can be multiplied by the number cores. For this configuration, the server can serve 1024 clients/second.

Buffers:

We have to make some changes in the buffer.

if the buffer size is low, the server has to do read and write at hard disk.

Here are the some of the directives which we have to know before making any changes.

client_body_buffer_size: This directive handles the client buffer size.

client_header_buffer_size: This directive handles the client header size. The 1K is sufficient for the header.

client_max_body_size: If the client request is too large to handle the Nginx will return the 413 error or Request Entity too large.

large_client_header_buffers: this directive contains the maximum buffer size and number 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;

Timeout:

This directive also improves the performance.

The client_body_timeout and client_header_timeout directories are making the server to wait for some particular time to receive the client header and client body to be received.

If the time exceeds, the Nginx will respond, "Request Timeout or 408 error".

The keepalive_timeout: This directive makes the connection to last up to the mentioned time. After that time, the server will disconnect the client.

send_timeout: If the send timeout expires, the server will disconnect the connection to the client.

client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;

Gzip Compression

Gzip helps the Nginx server reduce its network transfer amount. You should not set the gzip_comp_level too high as it will waste the 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:

You can set the expire header for web files which are not changed often and served frequently. This will let the Nginx load the required content only.

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

You can add and remove the files in the above lines which the Nginx server will serve.

Logging:

The Nginx will record each visit to your server. If you are using any other tool to watch the traffic, you may want to turn off this future.

For that, you have to edit the access_log directive.

access_log off;

Now, save and close the file. After that run

sudo service nginx restart

Conclusion

If you are a system admin, make sure you are tunning the Nginx for maximum performance.

These settings are not constant. According to the requirement, you have to tweak the settings to get more performance.

If you have any doubt in execution, please let us know that in the comment section.

We will help you. Also, don't forget to signup for the next tutorial.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments