Cobra Forum

Plesk Panel => Webserver => Topic started by: Suhitha on Sep 23, 2025, 06:56 AM

Title: How to enable leverage browser caching for nginx?
Post by: Suhitha on Sep 23, 2025, 06:56 AM
Serve static files directly by nginx and Proxy mode options in Domains > example.com > Apache & nginx Settings.

(When it is enabled, the location directive is added to nginx configuration and further specified location directives (like in Additional nginx directives) with regular expressions will be ignored. Refer to official nginx documentation for details)

2. Website's docroot does not protected with password in Domains > example.com > Password-Protected Directories.


Warning: Any customization made is done at your own will and risk and it is bound to be possibly overwritten by a Plesk update/upgrade process

Leverage browser caching can be enabled for a domain or server-wide.

To enable leverage browser caching for a single domain

1.Log into Plesk.

2.Go to Domains > example.com > Apache & nginx Settings.

3.Check the Enable nginx caching checkbox, disable the Serve static files directly by nginx checkbox and click OK/Apply.

Note: the aforementioned directives will set etag header. That means that the content will be stored on the client side and not be refreshed until it is modified on the server. Alternatively, the expiration time can be specified explicitly 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";
}

Where expires directive means how long cache will be stored. The value can be specified in days (expires 30d), hours (expires 2h) or minutes (expires 15m).


To enable leverage browser caching for server-wide

1.Log in to the server via SSH as root.

2.Create an additional nginx configuration file:

# touch /etc/nginx/conf.d/expires.global
3.Open this file using text editor and 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";
}
4.Create a directory for custom configuration templates:



# mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/
5.Copy default nginx template to the newly 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 with text editor and 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 should 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, all newly created domains will have leverage browser caching automatically enabled.

7.In order to apply this to all existing domains, it is required to reconfigure all the domains with the following command:

# /usr/local/psa/admin/bin/httpdmng --reconfigure-all
Warning: Consider running the above command during the maintenance time as it may cause some downtime for websites.

Note: the aforementioned directives will set etag header. That means that the content will be stored on the client side and not be refreshed until it is modified on the server. Alternatively, the expiration time can be specified explicitly 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";
}

Where expires directive means how long cache will be stored. The value can be specified in days (expires 30d), hours (expires 2h) or minutes (expires 15m).