How to enable leverage browser caching for nginx?

Follow

Comments

22 comments

  • Avatar
    Atthawut Prathumrat

    nginx not ngnix :P

  • Avatar
    Yulia Plokhotnikova

    @minzkung Thanks! I corrected it :)

  • Avatar
    Horacio Stolovitzky

    Is possible to enable globally?

  • Avatar
    Bulat Tsydenov

    @Horacio Stolovitzky, Hi! Please check this article again, it now contains the steps how to enable it globally.

  • Avatar
    Kironet (Edited )

    Hi, after implementing this(not globally), my dynamic website stopped showing styles, images, etc.., and loading has time increased from 1.5s to +-10s. What can cause that? 

  • Avatar
    Thanhkieu2022

    Leverage Browser Caching you should use the plugin: Leverage Browser Caching
    link down: https://wordpress.org/plugins/leverage-browser-caching/

    I used for website: http://www.thuocmaxman.vn/ and get 99/100 points. only ... / google analytics.js (2 hours)

  • Avatar
    Robert Asilbekov

    @Bogdan Dovgopol

    Instructions in the article cannot increase loading time.  It looks like some rewrite directives are missing. 

  • Avatar
    Ivan Postnikov

    @Thanhkieu2022

    Thank you for sharing this information. This is another way of enabling Leverage Browser Caching.

    Other Pleskians may find it useful.

  • Avatar
    Sharul Hafiz

    Tried on Wordpress. Wordpress is not compatible with this. Will result in 404.

  • Avatar
    Ivan Postnikov

    @Sharul

    For Wordpress try this plugin.

  • Avatar
    Miomir Besarabic

    @Thanhkieu2022, @Ivan Postnikov

    This plugin is only for Apache but this is article about leverage browser caching for nginx. Please don't post wrong information.

     

     

  • Avatar
    Ivan Postnikov

    @Miomir

    Thank you for the notice about the plugin.

    I have double checked it. It is applicable for Apache only.

     

  • Avatar
    Miomir Besarabic (Edited )

    This works if you add under Apache & nginx Settings in Additional nginx directives:

    Add Expires Headers

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
    }
    location ~* \.(pdf|html|swf)$ {
    expires 90d;
    }

    Add Cache-Control Headers

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 90d;
    add_header Cache-Control "public, no-transform";
    }

    I have tested with Wordpress and it works great. If Serve static files directly by nginx option is enabled in Domains > example.com > Apache & nginx Settings, it is required to disable it.

    Pingdom and GTmetrix honor this.

  • Avatar
    Markus Wernecke

    Thank you very much for all amazing and helpful articles.

    I use nginx only in subscription settings (service plans)

    Please, please, for all those who use this environment it would be the greatest gift if you could write also support articles for using

    only nginx

    Wish you all the best. PLESK is awesome...

  • Avatar
    Ivan Postnikov

    Hello @Markus!

    Thank you for the feedback.

    Sure, new articles are driven by demand. If there will be questions or issues regarding using Nginx only, the corresponding articles will be created.

  • Avatar
    Alexandr Redikultsev

    Hello,  @Peter Shaw.

    Please try combining the directives with the ones specified in the following knowledge base article:

    A WordPress website on a Plesk server shows "404 Not Found" on all pages except index.php, when running on FPM by nginx

     

  • Avatar
    Lenor

    Any disadvantages to enable this for all Customers? 

    Does it make more CPU / RAM Load? 

  • Avatar
    Robert Asilbekov

    @Lenor

    > Does it make more CPU / RAM Load?

    No

    > Any disadvantages to enable this for all Customers?

    It might affect some customers websites. For example, if the content was changed but the web browser may keep using the old version from the local cache as the content is yet to be expired.

  • Avatar
    Tomasz Paluszkiewicz

    in your article about enabling gzip compression, you advise to create

    /etc/nginx/conf.d/gzip.conf 

    (which will be automatically picked up because of wildard include in nginx.conf)
    but in this article you advise creating

    /etc/nginx/conf.d/expires.global 

    and then adding include rule inside

    /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php

    why the difference? what are the (dis-)advantages of one approach over the other?

  • Avatar
    Robert Asilbekov

    @Tomasz Paluszkiewicz 

    Because "location" directive is not allowed in "http" context.

  • Avatar
    Tomasz Paluszkiewicz

    @Robert Asilbekov 

    but then, gzip compression could also be applied the same way as expiry rules, right?

  • Avatar
    Robert Asilbekov

    @Tomasz Paluszkiewicz 

    Correct, gzip compression could be also enabled this way. But it is better to enable gzip globally in "http" context and later it can be turned off for a particular subscription.

     

    Because gzip contexts are: httpserverlocationif in location

     

     

Please sign in to leave a comment.

Have more questions? Submit a request