Enable Leverage Browser Caching in Nginx

Posted on October 24th, 2018

Enable Leverage Browser Caching in Nginx

Browser caching plays a paramount role in cache mechanism for increasing page speed. Static files like css, js, jpeg, png, etc that are used for the website can be saved on the visitor’s computer for future access. Whenever the visitor encounters a new page on your website, the above files can be accessed from the visitor’s computer instead of your provided server, which will tremendously increase the page load speed. So one of the best ways to enhance speed is to enable leverage browser cache. In this tutorial, you can implement leverage browser caching in nginx on Linux and Plesk control panel. In addition to that, we can use it in single or multiple domains as well.

 

To enable leverage caching for a single domain:

1) Log into Plesk > Go to Domains > example.com > Apache & nginx Settings

2) Add the following in the Additional nginx directives:

location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {

etag on;

if_modified_since exact;

add_header Pragma “public”;

add_header Cache-Control “max-age=31536000, public”;

}

 

To enable leverage caching for all your domains:

You should log in to the server via SSH and please follow the following steps

1) Create an additional nginx configuration file:

# touch /etc/nginx/conf.d/expires.global

2) Then paste following directives into it:

location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {

etag on;

if_modified_since exact;

add_header Pragma “public”;

add_header Cache-Control “max-age=31536000, public”;

}

3) Then next step is to run the following command in which you will create a directory for custom configuration templates:

# mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/

4) After creating the directory you need to copy the default nginx template to this newly created directory. The following operation is the copying of the same:

# cp -p /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php /usr/local/psa/admin/conf/templates/custom/domain/

5) Open the default nginx template using any text editor.

# vi /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php

6) Now add the following line in the bottom of the file:

include /etc/nginx/conf.d/expires.global;

So, the bottom of /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php file will look like:

include /etc/nginx/conf.d/expires.global;

<?php if (is_file($VAR->domain->physicalHosting->customNginxConfigFile)): ?>

include “<?php echo $VAR->domain->physicalHosting->customNginxConfigFile ?>”;

<?php endif ?>

}

Now you will have leverage browser caching for all your domains and also the newly created domain automatically

7) Run the following command, so that it will reconfigure with all the existing domains

# /usr/local/psa/admin/bin/httpdmng –reconfigure-all

 

If you need any further assistance please contact our support department.

 

Leave a Reply