Improve Nginx Performance : Quick Tips

Nginx is best alternative to overbearing Apache in terms of serving more numbers requests while consuming low hardware resources. However, Nginx is just like a software that needs to be tweaked to get a better performance. Here are five things we can optimize and take Nginx to a next level. To read more on Nginx vs Apache click here.

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 same architecture called “Worker Processes”. With this command we can instruct Nginx that how much cores are their in processor so that Nginx itself manage the concurrent requests in an optimized way.

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

To find out how many processors you have in your web server just run following command.

grep processor /proc/cpuinfo | wc -l

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

1

The output number shows that we have about 1 core cpu; so we need to set work processes to 1 for greatest performance in Nginx configuration file.

worker_processes 1;

Note: In case you have 2, 3, 4, 5, 6, … and more numbers of cpu cores then you can set respective numbers in Nginx configuration file.

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.

Note: 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,

1024

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 9.79 KiB (17.68% of original size)
6 9.62 KiB (17.37% of original size)
7 9.50 KiB (17.15% of original size)
8 9.45 KiB (17.06% of original size)
9 9.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 of static files like javascript, css, png, jpg, jpeg, gif and ico file to expire after 365 days which is one year. So such files will be cached and remains in users pc for maximum 365 days if he do not clear his cache in the same time period.

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

sudo service nginx restart

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

Conclusion

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.

Share This

Author: AtulHost

Hi, I'm Atul Kumar Pandey, a full time blogger by passion and profession. Since I started working on the web industries many years ago, with my all expertise I blog on atulhost.com, where my main objective is to build and market quality contents and reviews.

2 thoughts on “Improve Nginx Performance : Quick Tips”

  1. 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. Nice Question Anveksha,

      Older browser especially Internet Explorer 6 is somewhere used by few users in the world who are still using Windows XP. So the biggest problem with IE6 is that it doesn’t supports 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 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 which has the gzip issue. Rest other browsers are updating themselves automatically to latest technology so they never face such issue.

Comments are closed.