How to Enable Leverage Browser Caching in NGINX


leverage browser caching

Browser caching not only improves website speed but also reduces server load. NGINX supports various types of caching. Here’s how to enable leverage browser caching in NGINX.

 

How to Enable Leverage Browser Caching in NGINX

Here are the steps to enable Leverage browser caching in NGINX.

 

1. Open NGINX config file

Open NGINX config file in a text editor


$ sudo vim /etc/nginx/nginx.conf

You can even add the following code in /etc/nginx/sites-enabled/default

 

2. Enable Leverage Browser Caching

Add the following block of code inside an existing server block to enable leverage browser caching


location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
  expires max;
}

The above block of code will enable caching for css, js, ico, gif, jpeg, jpg, webp, png, svg, eot, otf, woff, woff2, ttf, ogg. You can change it according to your requirement.

If you want to limit the cache expiry to a specific number of days, such as, 30 days, you can update the expires clause as shown below


location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
  expires 30d;
}

 

3. Restart NGINX Server

Restart NGINX web server to apply changes


$ service nginx restart

 

That’s it! Browser caching will now be enabled in your NGINX Server

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!