This article explains how to enable leverage browser caching for a single domain. Enabling leverage browser caching for a single domain significantly improves website performance by reducing load times and decreasing server requests for returning visitors.
You can enable leverage browser caching for a specific domain or across the entire server. In this article, we will see both methods.
Let us follow the steps:
- To enable leverage browser caching for a single domain:
- Log into Plesk.
- Select the “Websites & Domains” sub-option from the left-hand side menu.
- Select the domain and click on the “Apache & Nginx” settings.
- Select the “Enable nginx caching” checkbox, and uncheck the “Serve static files directly by nginx” checkbox.
- Click “OK/Apply”.
- To enable leverage browser caching for server-wide:
- Log in to the server through SSH as root.
- Make an extra nginx configuration file-
# touch /etc/nginx/conf.d/expires.global
- Open this file with the text editor and paste subsequent 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"; }
- Generate a directory for custom configuration templates-
# mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/
- Copy the default nginx template to the recently created directory-
# cp –p /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php /usr/local/psa/admin/conf/templates/custom/domain/
- Open- /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php using the text editor and add the succeeding line at the end of the file-
include /etc/nginx/conf.d/expires.global;
Therefore, the footer of /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php file must 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 ?> }
As a result, the system automatically enables leverage browser caching for all newly created domains.
- To apply this to all present domains, reconfigure all the domains using the succeeding command-
# /usr/local/psa/admin/bin/httpdmng --reconfigure-all
Warning- Run the above command during maintenance time as it may cause some downtime for websites.
- Note: The aforementioned directives set the ‘Etag’ header, meaning that the content stores on the client side and does not refresh until modified on the server-
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)$ { expires 30d; add_header Pragma "public"; add_header Cache-Control "public"; }
The expires directive specifies the cache duration, which administrators can set in days (expires 30d), hours (expires 2h), or minutes (expires 15m).
Should you encounter any issues, feel free to contact our support staff at any difficult moment.
If you’re managing Nginx caches, you might also need to clear them. Check out our guide on How to Clear Nginx Cache for User from WHM