Applicable to:
- Plesk for Linux
Question
How to enable leverage browser caching in Plesk?
Answer
- Enable mod_expires: Plesk > Tools & Settings > Apache Web Server, tick expires > OK
- Go to Subscriptions > example.com > Apache & nginx Settings, add below to Additional directives for HTTP and Additional directives for HTTPS sections:
CONFIG_TEXT: <IfModule mod_expires.c>
ExpiresActive On
FileETag None
ExpiresDefault "access plus 14 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 1 days"
</IfModule>
First of all, connect to the server using SSH.
For RHEL-based operating systems (CentOS, RHEL, CloudLinux, Virtuozzo Linux 7)
-
Comment the line
LoadModule expires_module modules/mod_expires.so
in the file/etc/httpd/conf.modules.d/00-base.conf
. -
Create the configuration file
/etc/httpd/conf.d/expires.conf
:# vi /etc/httpd/conf.d/expires.conf
-
Insert the following content in it:
CONFIG_TEXT: LoadModule expires_module modules/mod_expires.so
ExpiresActive on
ExpiresDefault "access plus 14 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 1 days" -
Add the following line to the file
/etc/httpd/conf/httpd.conf
:CONFIG_TEXT: FileETag None
-
Apply the configuration changes:
# service httpd reload
-
Validate the Apache configuration:
# httpd -t
Syntax OK -
Check that the module is loaded:
# httpd -M | grep expires
expires_module (shared)
For Debian and Ubuntu
-
Add the following content into the file
/etc/apache2/mods-available/expires.load
:CONFIG_TEXT: ExpiresActive on
ExpiresDefault "access plus 14 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 1 days" -
Make a symbolic link to that file in the directory
/etc/apache2/mods-enabled/
:# cd /etc/apache2/mods-enabled/ && ln -s ../mods-available/expires.load expires.load
-
Add the following line to the file
/etc/apache2/apache2.conf
:CONFIG_TEXT: FileETag None
-
Apply the configuration changes:
# service apache2 reload
-
Validate the Apache configuration:
# apache2ctl -t
Syntax OK -
Check that the module is loaded:
# apache2ctl -M | grep expires
expires_module (shared)
Additional information
Depending on the type of content that is served, it might be required to enable caching for nginx too since different types of content is served by either nginx, or apache:
Comments
19 comments
what about mod_deflate?
I found the following tutorial but the line is not found.
https://iserversupport.com/enable-compression-leverage-browser-caching-plesk-apache-nginx/#comment-125087
https://linuxtechme.wordpress.com/2012/11/04/enable-http-compression-leverage-browser-caching-on-a-plesk-linux-server/
https://alpha-computer-services.acshostings.com/tag/plesk-onyx-leverage-browser-caching/
Can you give a tutorial on this? also this tutorial did not work as apache failed to start
@iamkingsleyf, you can enable mod_deflate module from Plesk > Tools & Settings > Apache Web Server
> also this tutorial did not work as apache failed to start
Can you share the error message?
Because after adding your code apache failed to start with an error
@iamkingsleyf, what exact message was shown? You can find it under /var/log/httpd/error_log
its passed now so i can't recall
Alexander, I'm running Plesk Onyx on Centos, and I followed your instructions and it works fine, thank you. However, I am wondering about "ETag none" as I'm trying to find best way to always serve image files that have been modified yet cache may still be holding unmodified version. I just was reading about using ETag and I remembered that I turned it off by following this instruction. So, will these instructions still work if I remove "ETag none" ? or do you have suggestion using Apache to always serve modified content?
@Stuart de Usoz,
Hello! You may remove "FileETag None" from Apache config file. As far as I know, 'mod_expires' should not conflict with 'ETag'
Error on first step "path must precede expression"
See attachment.
@1and1 Internet,
Hello! You should use only the command from step #1:
# find / -name mod_expires.so
And `/usr/lib64/httpd/modules/mod_expires.so` is it's output.
Hi I have a Ubuntu enviroment and it does not work.
If I try the second thing I get already a error: # find / -name mod_expires.so
find: '/sys/kernel/security/ima': Permission denied
find: '/sys/kernel/security/apparmor': Permission denied
find: '/sys/kernel/debug': Permission denied
nevermind it is in /usr/lib/apache2/modules/mod_expires.so
#3 would be # vi /etc/apache2/plesk.conf.d/expires.conf
#4 interestingly there you have the right path
#5 tried with that string and without
#6 and #7 no error works fine
#8 mod_expires does not apear in the list of loaded Apache modules
any idea what I can do next?
cheers, Armin
just found the solution: move /etc/apache2/plesk.conf.d/expires.conf to /etc/apache2/conf-available/expires.conf and than link it in /etc/apache2/conf-enabled/expires.conf to the available conf file restart apache2 and it works fine
@Armin Wucherer
I am glad the solution is found.
The solution for Ubuntu will be added to this article
This KB guide us to enable "Leverage browser caching" on server level. However we want to make it domain level.
How can we achieve domain level "Leverage browser caching"?
@Ashkan Nasiri
I have added this information to the article, check it.
Where is the guide "For particular subscription" ??? Because I don't think this is correct :
For RHEL-based operating systems (CentOS, RHEL, CloudLinux, Virtuozzo Linux 7)
Comment the line
LoadModule expires_module modules/mod_expires.so
in the file/etc/httpd/conf.modules.d/00-base.conf
.Create the configuration file
/etc/httpd/conf.d/expires.conf
:@lenala thank you for noticing, article was fixed!
what about the mode with nginx
Hello El fadili bouchta
Here is the link to requested instruction: https://support.plesk.com/hc/en-us/articles/115001374153-How-to-enable-leverage-browser-caching-for-nginx-
To enable leverage browser caching on Apache, please follow these simple steps:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
Please sign in to leave a comment.