ha.nnes.be

Web Performance

Better Pagespeed score on Plesk sites using compression & leveraging browser cache in Apache & Nginx

Posted on .

Better Pagespeed score on Plesk sites using compression & leveraging browser cache in Apache & Nginx

Introduction

Compression

In Apache using deflate module

To configure compression in Apache, check if the module deflate exists first:

httpd -M | grep deflate

If this returns the following, the module is available.

deflate_module (shared)

Then configure it by creating /etc/httpd/conf.d/deflate.conf with this configuration.

<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts

AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xml+rss
AddOutputFilterByType DEFLATE image/bmp
AddOutputFilterByType DEFLATE image/gif
AddOutputFilterByType DEFLATE image/jpeg
AddOutputFilterByType DEFLATE image/png
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/tiff
AddOutputFilterByType DEFLATE image/vnd.wap.wbmp
AddOutputFilterByType DEFLATE image/webp
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/x-jng
AddOutputFilterByType DEFLATE image/x-ms-bmp
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/mathml
AddOutputFilterByType DEFLATE text/plaintext/css
AddOutputFilterByType DEFLATE text/vnd.sun.j2me.app-descriptor
AddOutputFilterByType DEFLATE text/vnd.wap.wml
AddOutputFilterByType DEFLATE text/x-component
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/plain

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

Reload Apache to apply

sudo systemctl reload httpd

Nginx

If you use Nginx as a reverse proxy, you can configure compression as well. Create /etc/nginx/conf.d/gzip.conf with this configuration.

gzip on;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
gzip_proxied any;
gzip_types application/atom+xml application/javascript application/rss+xml application/vnd.ms-fontobject application/x-font application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml application/xml+rss font/opentype font/otf font/ttf image/bmp image/gif image/jpeg image/png image/svg+xml image/tiff image/vnd.wap.wbmp image/webp image/x-icon image/x-jng image/x-ms-bmp text/css text/html text/javascript text/mathml text/vnd.sun.j2me.app-descriptor text/vnd.wap.wml text/x-component text/xml;
gzip_vary on;

Restart Nginx to apply.

sudo systemctl restart nginx

Leverage browser cache

Apache

To configure cache expiration in Apache, check if the module expires_module exists first:

httpd -M | grep expires_module

If this returns the following, the module is available.

expires_module (shared)

Then create config file /etc/httpd/conf.d/expires.conf

ExpiresActive on
ExpiresDefault "access plus 14 days"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/html "access plus 1 days"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"

Add the following at the end of /etc/httpd/conf/httpd.conf:

FileETag None

Reload Apache to apply.

sudo systemctl reload httpd

Nginx

For Nginx you need to add the following in the Plesk admin on Domain level –> Apache & nginx Settings –> Additional nginx directives.

You may set this on Service Plan level as well in Web Server –> nginx directives.

location ~* \.(?:ico|css|js|gif|jpe?g|png|svg|pdf|htm?)$ {
  expires 30d;
  add_header Pragma public;
  add_header Cache-Control "public";
}
There are no comments.

Leave a Reply

View Comments (0) ...
Navigation