Join to Get Free Linux Server Hacks & cPanel Tips

Top 10 Nginx Tips to increase web server performance

Top 10 Nginx Tips to increase web server performance
4.7 (93.16%) 38 votes

Nginx is my favourite web server.  I started testing Nginx on 2010, and started writing Nginx tutorials around 2012, when it was being adopted only by a few big companies. Today everybody uses Nginx because it is fast and 100% compatible with all kind of web programming languages, by far the best alternative to Apache 2. Let’s find out the best ways to improve loading time for your sites powered by Nginx.

nginx tips

Nginx Tips To Increase Nginx Speed

Nginx out the box works really well supporting large number of heavy traffic (the nginx team did a great job working on the basic nginx web performance) keeping it stable and fast above all, however, there are a few optimizations that can be done in order to increase nginx speed and website load time.

On this article we will cover how to optimize Nginx to achieve the best performance, follow this quick and handy Nginx tips to make your sites load faster than ever.

The following white paper tips are useful for any Nginx scenario, they will help to you improve your Nginx server application performance on standalone server implementation using common http methods like load balanced & reverse proxy (nginx used as reverse proxy server) scenarios.

Let’s start, optimize nginx in just a couple of minutes step by step. Let’s start step by step the tips to improve performance of nginx web server.

1. Configure Nginx Worker Process

Lot of developers usually set nginx worker_processes variable to 4, 8 or 12 without knowing the real value of this directive. One of the best Nginx tips I can give you: worker_processes should always be set to auto, as recommended by Nginx developers. Since Nginx 1.9.10 worker_processes can be set to auto bind all worker processes automatically to use all available CPUs:

Edit nginx.conf (usually located at /etc/nginx/nginx.conf on Centos and RHEL) file and set:

worker_processes auto;

This is also useful to avoid problems while migrating to different CPU model servers (example: single core to dual core CPU)

nginx tips to improve server performance

2. Configure worker connections

Nginx worker connections determine the max number of connections each worker process will handle. If worker processes are set to 4, and your worker connections are set to 10 it will handle 40 connections max.  For 90% of the websites, set worker_processes to auto, and then set 512 or 1024 value for worker_connections.

Edit Nginx configuration file nginx.conf and set:

worker_connections 1024;

3. Disable Access Logs and Restrict error_log

Nginx logs save tons of data inside, and most if you are having a mid-large busy website. Unless you have an application that analyzes the data from your visitors in real time, then there is no need to have access_log enabled at all, it will eat your disk space, and will allow additional load to the server because it is constantly writing new data.

Error log can also be configured to only log critical errors, and avoid general debug and warning messages.

Edit nginx.conf file and add this two variables:

error_log  logs/error.log crit;
access_log off;

This is the ideal log configuration to increase application performance, disabling access logging will make a big difference on intense I/O enviroments (very common on php servers with high traffic proxy server, load balanced visitors and reverse proxy server configurations with lot of connections per ip).

4. Enable Browser Cache

One of the most useful Nginx tips is to enable browsing caching. Nginx has the ability to set a cache configuration for all your static files (javascript, images, icons, etc). Place this inside your Nginx main configuration for the virtual host file:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 120d;
add_header Pragma public;
add_header Cache-Control "public";

This will give you a 120 days period for the local cache, and set Pragma and Cache-Control headers.
If you want even a simple rule, you can set the cache to 365 days:

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

Enabling caching on Nginx will give you instant benefits:

  • It will make your website content load fast from visitors client computers, as the static files will be stored on their local hard drives, accelerating each new request in the future when they visit the site again.
  • It will save bandwidth from your site, as there are less requests than usual, will decrease the load of your web server for sure.

5. Enable Gzip Compression

Gzip compression is one of the most used techniques to increase website speed and save gbs of BW on the server side. When gzip compression is active, Nginx will transfer the data x3 times faster than without compression. It is a must for all Nginx users.

Enable Gzip on Nginx (ngx) by editing nginx.conf and add this lines:

gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;

You can test if your gzip compression is working by using tools like Gzip test.

6. Configure Timeouts

Nginx includes a few important timeout directives that can directly impact your web server and website performance.

Edit nginx.conf and set this timeouts as you see below:

client_body_timeout 15;
client_header_timeout 15;
keepalive_timeout 20;
send_timeout 15;

Explanation of each directive:

  • client_body_timeout and client_header_timeout directives set the max time the Nginx web server will wait for client body or header after the initial request.
  • keepalive_timeout on the other side set’s keepalive timeout for client connections, Nginx will close the connections after the timeout period set.
  • send_timeout is used to set an Nginx timeout while transmitting information to the client, but only between two consecutive write attempts.

This works for most normal apps, but maybe your should have higher timeout values, test your web apps and adjust as you need.

7. TCP_nodelay & TCP_nopush

Networking is crucial if you want to achieve best performance / speeds for all your websites powered by Nginx. tcp_nodelay and tcp_nopush are two critical networking directives that most of the time need to be configured, this is one of the crucial nginx tips you must never forget while configuring Nginx.

Edit nginx configuration file (nginx.conf) and set:

sendfile on;
tcp_nopush on;
tcp_nodelay on;

Explanation of each directive:

  • tcp_nopush: this options is only activated if sendfile is active inside Nginx configuration. tcp_nopush lets your Nginx server send HTTP response in one packet instead of sending it in frames, this is specially useful to reduce bw usage and improve throughout.
  • tcp_nodelay: this option is used when a connection is in keep-alive state, tcp_nodelay prevents system from using buffering data-sends, the server data will be send in small bursts in real time instead.
  • sendfile is required if you are enablingtcp_nopush.

8. Configure Buffers

Nginx buffering helps to improve web server and website performance, specially with slow remote clients. When Buffer size is too low, Nginx will start writing data into disks, generating unnecessary disk (I/O) activity. When Nginx buffering is active, you will notice quick serving times. Let’s see the best values for standard nginx buffering (this is similar to the proxy buffer settings)

Edit nginx configuration file (nginx.conf) file and set:

client_body_buffer_size 15m;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;

Explaining each buffer directive:

client_body_buffer_size:  if a request exceeds the configured value, then it will be written into disk using a temporary file.
client_header_buffer_size: similar to client_body_buffer_size, clientheaderbuffersize  1k or larger is used to handle client header size.
large_client_header_buffers: this sets the maximum # and size of buffers for large client headers.

While I’ve found clientheaderbuffersize 4k to be ok for most of my apps, you should start working with clientheaderbuffersize 1k, and increase it if needed.

9. Open_file Cache

Unix and Linux like systems are based on files, this means files are involved in every task of the system.

Nginx’s open_file_cache directive takes advantage of that and can be used to cache file file descriptors from the most accessed files, increasing accessing speed and avoiding unnecessary I/O activity. open_file_cache is used can cache information about files like:  open file size, modification times and file descriptor, directory existence information and also for file lookup errors.

Edit nginx.conf file and set this values:

open_file_cache max=10000 inactive=10m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;

This will set 10k as max number of cached files, for 2 minutes max, cached files will remain inside the cache only if are used at least 1 time. It is one of the crucial nginx tips to increase file access information speed.

10. Nginx systcl.conf tweaking

sysctl.conf file can be tweaked to optimize Linux tcp kernel related networking parameters. There are two particular directives that can make a huge difference while tweaking Nginx performance. net.core.somaxconn and net.ipv4.tcp_max_tw_buckets will allow your Nginx web server to set custom size for the length of new connections setting higher limits for the size of Linux queue.

Edit sysctl.conf file in your system, and make sure to add:

net.core.somaxconn = 65536
net.ipv4.tcp_max_tw_buckets = 1440000

After modifying this values, make sure to watch your kernel logs, if you find any errors, make sure to increase the values until they stop.


As you see, there are many ways to get better Nginx performance. Follow this 10 Nginx tips and I’m totally sure that your nginx speed and overall server performance (specially for php servers) will increase a lot, having less load average by reducing I/O by caching information from file descriptor for both encrypted (using ssl certificate) and non encrypted ssl pages, also tweaking your buffers size will help a lot.

Your visitors will be more than happy because your website will also load way faster than before, loading time will be faster, something really important if you want to have a better ranking in Google results.

If you are an Nginx user and need advanced (not covered in this white paper article) Nginx optimization and tips to improve your app & server performance, get in touch with us.