Browser caching for static files

Plesk supports the configuration of browser caching for static resources for individual or all domains. Please note that an unmanaged server with SSH root access is required for the central setup of all domains.



Setup for one domain

First select the domain for which you want to activate browser caching. Then navigate to the Apache & nginx settings of the domain.


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;
    add_header Cache-Control "public, max-age=31536000";
}


Google recommends at least 365 days for static resources.

Source: https://developer.chrome.com/docs/lighthouse/performance/uses-long-cache-ttl?hl=de



Setup for all domains


An unmanaged server with SSH root access is required for the setup


Log in to the server via SSH and create the following file:

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


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;
    add_header Cache-Control "public, max-age=31536000";
}


Then create a new directory for the virtual templates:

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


Copy the standard NGINX template into the new directory:

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


Edit the standard NGINX template and add the new global configuration file at the end:


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

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

# End of the file
<?php if (is_file($VAR->domain->physicalHosting->customNginxConfigFile)): ?>
include "<?php echo $VAR->domain->physicalHosting->customNginxConfigFile ?>";
<?php endif ?>
}


New domains are automatically rolled out with this configuration so that browser caching via NGINX is active by default. To ensure that existing domains also receive the correct new configuration, the following command must be executed:

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



Example Max-Age values

Header Value Description
Cache-Control public, max-age=31536000 365 days
Cache-Control public, max-age=15552000 180 days
Cache-Control public, max-age=7776000 90 days
Cache-Control public, max-age=5184000 60 days
Cache-Control public, max-age=2592000 30 days
Cache-Control public, max-age=604800 7 days



Check browser caching

To check the adjustments to the caching behavior, execute the following command:


curl -I https://www.creoline-demo.com/static/styles.css

HTTP/2 200
server: nginx
date: Thu, 08 Aug 2024 12:18:50 GMT
content-type: text/css
content-length: 25500
last-modified: Thu, 08 Aug 2024 12:18:50 GMT
etag: "4107c187-125zc"
cache-control: public, max-age=31536000


Check the HTTP response to see whether both the Cache-Control header contains the public value and the max-age value. In addition, an etag header must also be displayed.