Applicable to:
- Plesk for Linux
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.
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.
-
Go to Domains > example.com > Apache & nginx Settings
-
Add the following in Additional nginx directives field:
CONFIG_TEXT: 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.
-
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:
CONFIG_TEXT: 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/custom/domain/nginxDomainVirtualHost.php
with text editor and add the following line in the bottom of the file:CONFIG_TEXT: 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:CONFIG_TEXT: . . .
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 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:
CONFIG_TEXT: 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
).
Comments
47 comments
I use nginx in proxy mode.
Applied this great articles for server-wide, but I've this nginx error:
17815#0: *23099 open() "/var/www/vhosts/100asa.it/httpdocs/44541-home_default/epson-tm-t88v-ihub-ethernet-epos-nero.jpg" failed (2: No such file or directory)
and no images was visible on my website.
After enabled Serve static files directly by nginx on Domains > 100asa.it > Apache & nginx Settings all back to normal and all images back to visible.
Why this?
Hello @Remigio Ruberto,
The cause of such behavior is not clear.
We have the corresponding article on this matter as well: Website is shown without static content on Plesk server
hey i have a strange bug and maybe someone knows the answer how to fix this ->
we are using only nginx and have the exact same config on 2 different servers - same plesk etc.
the additional nginx config is 100% injected - but every location directive is not working for some reason
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";
}
-> 0 effect
it still sets the headers etc as if "serve static files directly through nginx" is activated
but when i add for example
add_header X-debug-message "A static file was served" always;
this is set correctly
as i said same config works on a different server ?
@Andre Utesch
Maybe checking out all 'location' directives server-wide will provide some tips regarding possible configuration conflicts?
The location directive should be prefixed with a backslash to enforce a dot before file extension.
Thanks Linus,
I've added that information to the article :)
Note this breaks SitePro if done server wide.
Hello Myhost.ie Support
Thank you for the valuable warning.
It will be useful for other Pleskians.
Hello Ivan Postnikov
This solutions works when adding to domain > Apache & nginx Settings, however when trying to add to a service plan, the rules are not added when synced with all subscriptions.
Can you please advise on fix without having to alter web server templates (other supplied method)?
Running Plesk Obsidian Version 18.0.26 on CloudLinux 7.8.
Thank you.
Hello Greg Sevatos
Please, try this instruction: https://support.plesk.com/hc/en-us/articles/213903105
Is here a urgent directive missing? tutorial wrote:
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;
but before second line need this line?
try_files $uri @fallback;
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Example: image.png can be a generated image like a "CAPTCHA". So its needed that "image.png" to refresh every page request. With try_files its check its a static file or not, when not its do nothing. So I understood this.
Hi Karl May
Have you tried the suggested solution? Doesn't it break the caching for the specified types of files at all?
I get this message when trying to implement browser caching on Nginx (Vultr Plesk):
Invalid Apache configuration: AH00526: Syntax error on line 1 of /var/www/vhosts/system/mydomain.com/conf/vhost.conf: Invalid command 'location', perhaps misspelled or defined by a module not included in the server configuration
this is what I've added:
```
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";
}
```
Maxie Jazz, put these directives into "Additional nginx directives".
that's what I did and I get that error AH00526
Maxie Jazz, the error you get from Apache, means that you put directives into Apache additional directives, not into Nginx section. Also, review your additional apache directives maybe there are some mistakes.
Hi,
my server is running on apache with Reverse Proxy Server (Nginx).
what I need is configuration browser caching for only apache? or
only Nginx, or both?
for work as well.
Please sign in to leave a comment.