How to Enable Leverage Browser Caching for Nginx

June 10, 2024 / How-to Guide

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.

Leverage browser caching can be enabled for a domain or server-wide. In this article, we will see both methods.

Let us follow the steps:

  1. To enable leverage browser caching for a single domain:
    1. Log into Plesk.
    2. Select the “Websites & Domains” sub-option from the left-hand side menu.
      websites & domains
    3. Select the domain and click on the “Apache & Nginx” settings.
      Apache & nginx
    4. Select the “Enable nginx caching” checkbox, and uncheck the “Serve static files directly by nginx” checkbox. enable
    5. Click “OK/Apply”.
  2. To enable leverage browser caching for server-wide:
    1. Log in to the server through SSH as root.
    2. Make an extra nginx configuration file-
      # touch /etc/nginx/conf.d/
    3. 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";
    4. Generate a directory for custom configuration templates-
      # mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/
    5. 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/
    6. 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/;
      Therefore, the footer of /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php file must look like-

      . . .
      include /etc/nginx/conf.d/;
      <?php if (is_file($VAR->domain->physicalHosting->customNginxConfigFile)): ?>include "<?php echo $VAR->domain->physicalHosting->customNginxConfigFile ?>";
      <?php endif ?>

      As an outcome, leverage browser caching will be automatically enabled for all recently generated domains.

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

    8. Note- The aforementioned directives will set the ‘Etag’ header, meaning that the content will be stored on the client side and not refreshed until modified on the server. Alternatively, the expiration time can be specified using the following 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)$ {
      expires 30d;
      add_header Pragma "public";
      add_header Cache-Control "public";

      The expires directive specifies the cache duration, which can be 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.

Spread the love