• Plesk: How to enable leverage browser caching for nginx?

How to enable leverage browser caching (cache expiration) for nginx?

Warning: it will not work if Serve static files directly by nginx option is enabled in Domains > example.com > Apache & nginx Settings.

For single domain:

Log into Plesk.

Go to Domains > example.com > Apache & nginx Settings and add in 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 add this directive on a service plan level, go to Service Plans > plan_name > Web Server > nginx directives.

If Serve static files directly by nginx option is enabled in Domains > example.com > Apache & nginx Settings, it is required to disable it. 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. See official nginx documentation for details.

To test if the directive is applied successfully run curl command with any static file:

curl -I -c - "https://example.com/example.txt" -k

For the server-wide:

Log in to the server via SSH

Create an additional nginx configuration file:

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

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";
}

Run the following command to create a directory for custom configuration templates:

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

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/

Open /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php with any text editor(e.g. nano or vi) 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 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 ?>
}

As a result, all newly created domains will have leverage browser caching automatically enabled.

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 above command during the maintenance time as it may cause some downtime for websites.


This article was last modified: Aug. 30, 2018, 2:57 p.m.

0 Comments

Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.

Share

Hacker News

Top