[How to] Enable cache expiration in nginx

Created:

2016-11-16 12:49:38 UTC

Modified:

2017-08-16 16:20:34 UTC

10

Was this article helpful?


Have more questions?

Submit a request

[How to] Enable cache expiration in nginx

Applicable to:

  • Plesk 12.5 for Linux
  • Plesk 11.x for Linux
  • Plesk 12.5 for Windows

Question

How to enable cache expiration in nginx?

Answer

The following directive enables cache expiration in 1 minute:

    location ~* ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon)$ {
expires 1m;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
try_files $uri @fallback;
}

To change cache expiration to 1 hour or 1 day instead, use:

    1h;
1d;

Also it is needed to exclude files format from location (in this example css|js|gif|jpeg|png ) in Serve static files directly by nginx , if it is enabled.

It is not possible to enable cache expiration server-wide. Instead, Plesk allows custom settings at a service plan and website (virtual host) levels.

Website:

Add above directive in Subscriptions > example.com > Apache & nginx Settings > Additional nginx directives

Settings defined in Subscriptions > example.com > Apache & nginx Settings > Additional nginx directives are written in /var/www/vhosts/system/example.com/conf/vhost_nginx.conf file.

Service Plan:

Add above directive in Service Plans > plan_name > Web Server > nginx directives

Service plan level settings will be valid for all subscriptions under the plan.

Have more questions? Submit a request

7 Comments

  • 0
    Avatar
    Raymond Hawkins

    Note that this will not work if "Serve static files directly by nginx" is checked because it comes first and overwrites this rule.

    Edited by Raymond Hawkins
  • 1
    Avatar
    Vitaly Zhidkov

    @Raymond, this is expected and files format specified in location should be excluded from list of static files served by nginx.

  • 0
    Avatar
    Piotr Kubala

    Note that @fallback location in this example is undefined and it brake system virtual directories, e.g. awstats-icon (and maybe others). If you enter this directive as advised then your AW statistics will be broken. The proxy_error_log contains lines like this:

    could not find named location "@fallback", client: ..., server: ..., request: "GET /awstats-icon/other/vk.png HTTP/1.1"

    So you need to correct the location statement as follow:

    location ~* ^(?!/awstats-icon/).*\.(css|js|gif|jpeg|png)$

     

  • 0
    Avatar
    Pavel Rozental

    @Piotr Kubala, Thank you for your comment. Article was fixed.

  • 0
    Avatar
    A Sieber

    I have set

    Serve static files directly by nginx
    from: ac3 avi bmp bz2 cue dat doc docx dts eot exe flv htm html img iso mkv pdf ppt pptx qt rar rm swf tar tgz ttf txt wav woff woff2 xls xlsx zip

    to: ac3 avi bmp bz2 css cue dat doc docx dts exe flv gif gz htm html ico img iso jpeg jpg js mkv mp3 mp4 mpeg mpg ogg pdf png ppt pptx qt rar rm swf tar tgz txt wav xls xlsx zip

    and 'Additional nginx directives':

    location ~* ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon)$ {
    expires 1m;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    try_files $uri @fallback;
    }

    # Media: images, icons (xml removed)
    location ~* \.(jpeg|jpg|gif|png|ico|cur|gz|svg|svgz|webp)$ {
        expires 1m;
        access_log off;
        add_header Cache-Control "public";
    }

    Is it the correct syntax ?

    The Google PageSpeed Insights reports that the short freshness lifetime is set to 60 seconds for some .jpg and .png files. AS I have set before, the time should be 1 month.

     

     

  • 0
    Avatar
    Artyom Baranov

    @A Sieber,

    Hi! I can see that you have set `expires` value to 1m. Here `m` states for `minutes`.

    If you would like to increase cache expiration to 1 month, use `30d` value, where `d` stands for `days`

  • 0
    Avatar
    A Sieber

    @Artyom Baranov

    Correct. I have changed it to 30d and the Google PageSpeed Insights index is slightly better.

Please sign in to leave a comment.