Question
How to enable leverage browser caching (cache expiration) for nginx?
Answer
Leverage browser caching for nginx can be enabled by adding directives for webserver. Check out the following “Requirements and limitations” before proceed with enabling.
Requirements and limitations:
- Depending on the type of content that is served, it might be required to enable caching for Apache too since different types of content is served by either nginx or Apache: How to enable leverage browser caching for Apache in Plesk
- The main conditions for enabling nginx leverage browser caching are:
1. Disabled 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. - If Google PageSpeed Insights extension is used on the server and leverage browser caching for nginx is enabled globally, then the functionality of Google PageSpeed Insights is being broken.
- The explicit expiration time for nginx caching may not be applied when PHP handler is set as FPM application served by nginx details can be found here: [BUG] Cache expiration header in additional Nginx directives does not get applied
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
-
Go to Domains > example.com > Apache & nginx Settings
-
Add the following in Additional nginx directives field:
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";
} -
Go to Domains > example.com > Apache & nginx Settings and disable Serve static files directly by nginx option.
To enable leverage browser caching for server-wide
-
Log in to the server via SSH as root.
-
Create an additional nginx configuration file:
# touch /etc/nginx/conf.d/expires.global
-
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";
} -
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…