uk en ru
php

How to enable caching and compression of static files

One of the recommended ways to improve website performance is to configure file compression and caching.

  • Compression is used to transfer static files - javascript, css styles, fonts, and more.  With compression enabled, the web server transmits the files in compressed form, and adds the Content-Encoding header

how-cache-and-zip-static.3

  • When caching is enabled, the server sends the Expires header, which tells the browser that the downloaded file can be saved in the cache. When re-accessing the file, the browser will not download it, but will take it from the cache. Client-side caching applies to javascript, css files, images and other static files. This feature increases the speed of loading and displaying the site in the browser significantly.

how-cache-and-zip-static.5

For any site, the optimal configuration of compression and caching of static files is already defined by default.

Sample nginx config:

    location / {
        proxy_pass      http://X.X.X.X:8080;
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|odt|ods|odp|odf|tar|wav|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ {
            root           /home/user/web/domain.com/public_html;
            access_log     /var/log/apache2/domains/domain.com.log combined;
            access_log     /var/log/apache2/domains/domain.com.bytes bytes;
            expires        max;
            try_files      $uri @fallback;
        }
    }
    gzip                on;
    gzip_static         on;
    gzip_vary           on;
    gzip_comp_level     6;
    gzip_min_length     1024;
    gzip_buffers        16 8k;
    gzip_http_version   1.1;
    gzip_types          text/plain text/css text/javascript text/js text/xml application/json application/javascript application/x-javascript application/xml application/xml+rss application/x-font-ttf image/svg+xml font/opentype;
    gzip_proxied        any;
    gzip_disable        "MSIE [1-6]\.";

However, if you need more precise customization, you can override the types of static files for which this configuration will be applied. For this:

The setting should be done in the PHP hosting control panel https://php.realhost.pro:8083

1. Select a domain in the WEB section

how-cache-and-zip-static.12

2. Click the "More options" button

how-cache-and-zip-static.14

3. In the "Proxy Processing" field, specify the list of file types for caching

how-cache-and-zip-static.16

4. When finished, click the "Save" button

how-cache-and-zip-static.18