5 Best Practices for Tuning Nginx for Best Performance

Nginx is a lightweight and high-performance web server which is completely open-source. Currently, it is the second most popular web server after Apache running around 40% of the total websites online. It offers a wide variety of features such as load balancing, caching, and reverse proxy. Its event-driven architecture allows it to handle thousands of concurrent connections easily. This architecture makes it possible for Nginx to outperform its main competition, Apache.

For most cases NginX works well with the default settings. However, with a few tweaks in it’s configuration you can bring out the maximum performance Nginx has to offer. Why do we need this extra performance? Many surveys conducted by website owners have shown that poor performance can lead to loss of revenue and customers. A case study conducted by ecommerce companies has shown that for every 100ms increase in response time can result in 1% loss in revenue. You can see now how performance impacts your business. It has also been found out that the websites that load faster are preferred more by search engines such as Google. So having a faster website also plays a key role in SEO.

If you’re someone who has multiple websites running on Nginx, then this article is for you. All the tips mentioned in this article should help you optimize Nginx and get the best performance. Make sure to change or modify the server configurations only if you’ve already done it before. Because one small mistake can shut down your entire website or your business. In case you are not familiar with it, try to seek out professional help. You will find many system admins on freelancing websites such as Upwork or Fiverr who are willing to help you for a few bucks. However, if you already have some experience building web applications then changing Nginx configurations shouldn’t be a big issue to you. Let’s get started.

Content Compressions

Content compression is a major factor when it comes to performance. It can save you a lot of bandwidth costs and can also reduce the response time of your web application. When a web browser sends a request to a web server, the webserver does not directly send the web page in raw bytes to the browser. Instead, it compresses the page into small chunks and then sends it to the browser. The browser then decodes these chunks, combines them together and you get the original web page. This is how most modern web browsers and web servers work.

Content compression is not enabled with the default configuration of Nginx. You have to manually enable it by editing the configuration file. With proper compression settings, you might be able to reduce anywhere between 20-35% of the actual size of the webpage. This is very useful when you have a very popular website which gets thousands or even millions of visitors a month. Suppose you are the owner of such a website. If the website gets so many visitors per month then definitely it would consume a lot of bandwidth. Suppose you pay $1 per Gb of bandwidth used(usually the cost is much less than this amount) and say you consume 100Gbs of bandwidth per month. Without compression, it would cost you $100 easily. Adding content compression can reduce this bandwidth to maybe 80Gb or even less. Usually, the minimum is around 20%. So now your cost will be reduced to $80. As you can see content compression definitely helps a lot in terms of bandwidth cost.

Content compression won’t be of much help to you if you own a website that gets a few thousands of views a month. But for big websites such as Amazon, Google, or Netflix it can save millions. With Nginx, you can either use Gzip compression or Brotli compression. Gzip compression has been around for quite some time. It is supported by almost all modern browsers. Brotli is a new compression format that was introduced by Google a few years back. Brotli is ~20% more efficient than Gzip. Brotli is not yet supported by all modern browsers and client-side libraries. So you might have to switch Gzip and Brotli accordingly. After modifying the configuration files with appropriate settings you should be able to see the changes reflect back in your browsers.

Browser Caching

Browser caching is yet another factor that can affect your response time and page speed. Static files such as css files, js files and image files don’t change a lot over time. There is no point in requesting them again and again if they remain the same. This is where Browser caching comes into play.

Instead of requesting the same resources again and again, the browser saves the most frequently requested resources in it’s cache. Next time when you visit the same site, the browser first checks if the files are available in the cache. If they are available in the cache it then checks for the expiration timestamp of the cache. If the files are within the expiration timestamp they loaded directly from the cache. Not only does browser caching save bandwidth but also the computing resources on the server.

If you have a website where most of you visitors are returning visitors then you should definitely consider browser caching. When caching for a specific resource the browser looks for an expiration tag on that resource. This expiration tag is not present by default. You can enable it adding the following lines of code in your configuration file. This will add an expiration tag on static files such as images, css and js files.

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

Server-Side Caching

Server-side caching is a type of caching technique where content is cached on the server-side for a very short period of time. While browser-side caching is used to cache static resources, Server-side caching is used to cache dynamic resources. Server-side caching can help you reduce the number of database calls significantly. Database calls are not cheap. Adding the following lines in your configuration file will enable server-side caching.

proxy_cache microcacheapi;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
proxy_cache_background_update on;
proxy_cache_lock on;

SSL Session optimization

Having SSL installed on your website can sometimes increase the load time of your website. This is because a lot of additional steps are performed before requesting any resource from the webserver. For example, before any data can be exchanged between the server and the client, an SSL session must be negotiated between them. This session is negotiated by exchanging public keys and cipher between the client and the server. This exchange adds an overhead to the overall response time.

Creating a session cache can reduce the number of TLS handshakes between the server and the client. Session caching can be achieved by adding the ssl_session_cache tag in the Nginx configuration file.

Worker Connections

There is a certain limit to the number of requests the webserver can handle at any point in time. This number is called worker connections. Basically, it tells us the total number of clients that can be simultaneously served by a web server. With the default configuration, you will find that the worker connections are set to 768. In order to improve the performance of the web server, you should increase the worker connections to the maximum limit allowed by your VPS.


These optimization tips should help you to increase the performance of Nginx significantly from the default settings. We have only scratched the surface of Nginx in this article. You can go even deeper and tweak Nginx according to your needs. Make sure you take a backup of your server before changing any configuration files. One small wrong change can mess up your entire website if not done correctly.

Leave a Comment

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