Nginx performance tuning

In this tutorial, we’ll see Nginx Performance Tuning tips for all major Linux distributions like Debian, CentOS, Ubuntu, and many more. Nginx is the best alternative to overbearing Apache in terms of serving more numbers of requests while consuming low hardware resources. To read more on Nginx vs Apache click here.

However, Nginx is just like software that needs to be tweaked to get better performance.

Here are five things we can optimize and take Nginx to a next level. Read ahead one by one.

Improve NGINX Performance

Adjust worker processes.

Just like new processors which has multiple cores in it to perform more numbers of activities in single point of time. Nginx is also packed with similar architecture called “Worker Processes”. With this command we can instruct Nginx to know how much cores are there in system so that Nginx manage and optimize itself to handle any concurrent requests.

The default Nginx configuration path is : /etc/nginx/nginx.conf

Based on your hosting plan you would know what number of CPU core/s you got. If not; find out the number of processors you have in your web server just by running the following command.

grep processor /proc/cpuinfo | wc -l

The output of above command will always be in numbers only… like,


The output number shows that my test system has exactly 2 CPU Cores; so we need to set work processes to 2 for the greatest performance in the Nginx configuration file.

worker_processes 1;

Note: In case you have 1, 2, 3, 4, 5, 6, … and more numbers of cpu cores then you can set respective numbers in Nginx configuration file. The newer version of Nginx supports auto command too.

Adjust worker connections.

The worker connection is effectively used limits the numbers of processes can be managed by each worker process at one time.

By default a single processor can handle up to a maximum 1024 requests at a time. In case you have multiple cpu cores then you need to multiply it with 1024.

Alternatively you can run following command also to identify maximum worker connection your web server can handle.

ulimit -n

The output will always be in numbers only… like,


Now add the same value in Nginx configuration file.

worker_connections 1024;

This way you can adjust your worker connections to its full potentials.

Enable gzip compressions.

GZip compression is the most awesome feature which you should use in any system; it not only cuts down the transfer of large amount of data but saved the download time too.

If you see any gzip command over there with hash tag in beginning then just remove then to activate GZip on Nginx server or else you can add following command there inside the http {…} block.

gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Here, we have disable GZip for Internet Explorer’s old version because it does not support GZip. Also set gzip_comp_level to 6.

GZip compress level ranges from 1 to 9; where 1 is considered as least compress file and 9 is considered as most compressed file. You can set it to some where at 1 to 6 based on hardware resources you have. If you are not cheaper VPS with less powered cpu then set it to 1 or 2. Level 6 is an optimal level after this hardly any file’s size decreases.

Here we have a sample output of a file with various gzip compression levels.

File: text/html: phpinfo()
levels file-size (% of original file)
0 - 55.38 KiB (100.00% of original size)
1 - 11.22 KiB (20.26% of original size)
2 - 10.89 KiB (19.66% of original size)
3 - 10.60 KiB (19.14% of original size)
4 - 10.17 KiB (18.36% of original size)
5 - 09.79 KiB (17.68% of original size)
6 - 09.62 KiB (17.37% of original size)
7 - 09.50 KiB (17.15% of original size)
8 - 09.45 KiB (17.06% of original size)
9 - 09.44 KiB (17.05% of original size)

Using more levels of GZip compression would be just a waste of compression and cpu cycles.

Disable logs.

Nginx logs each and every action in a log file named as access.log so if do not requires it better to disable it to save a small amount additional process.

Just write condition off next to access_log syntax like below.

access_log off;

It will not log all the not required logs and you will save a bunch of hard drive space too.

Enable cache for static file.

Another thing you can do to optimize Nginx performance is to tell computer’s browser to cache the web page’s static file in local computer system for a fast accessibility. Which can be done using adding following command lines inside actual Nginx server block.

The default location for this is: etc/nginx/sites-available/sitename

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

You can also add more file extensions which you want to be cached for a longer time.

Here we set most static files like javascript, css, png, jpg, jpeg, gif, and ico files to expire after 365 days which is one year. So such files will be cached and remain in the user’s browser for a maximum of 365 days if he does not clear his cache in the same time period. You can even set it to max instead of 365d.

Now you can restart Nginx to see the results in action.

systemctl restart nginx

This way we have optimized Nginx to a higher level now you will get more performance out of same hardware.


The main objective of tweaking Nginx configuration to make ultimate use of all available resources. If you are a system admin editing such files would be a fun.

Category: .

Leave a Reply

Your email address will not be published. Required fields are marked *


  1. Anveksha Avatar

    Hi Atul,

    Thanks for sharing an amazing post on improving the nginx performance with just few tweaks in its configuration file. However I found that you suggested to disable the gzip for msie6 using this command gzip_disable “msie6”. May I know why you disabled it. Is there any harm of not disabling gzip on msie6.

    1. Atul Kumar Pandey Avatar

      Nice Question Anveksha,

      Older browsers especially Internet Explorer 6 is somewhere used by a few users in the world who are still using Windows XP. So the biggest problem with IE6 is that it doesn’t support web page compression techniques. If you do not disable it; then you may face some issues like incorrect page rendering or no page-load also on Internet Explorer 6. That is why it is a better side to disable it and deliver same users a non gzip file so that no issue occurs from old browsers.

      Internet Explorer 6 is the only browser that has the gzip issue. Rest other browsers are updating themselves automatically to the latest technology so they never face such issues.