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 best alternative to overbearing Apache in terms of serving more numbers requests while consuming low hardware resources.

To read more on Nginx vs Apache click here.

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. Read ahead one by one.

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 number of processors you have in your web server just by running 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.

*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,

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 - 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 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.