How to enable leverage browser caching for nginx?

Follow

Comments

47 comments

  • Avatar
    Bulat Tsydenov

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

    1
    Comment actions Permalink
  • Avatar
    Andre Utesch

    hey i have a strange bug and maybe someone knows the answer how to fix this ->

    we are using only nginx and have the exact same config on 2 different servers - same plesk etc.

    the additional nginx config is 100% injected - but every location directive is not working for some reason

    location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
    expires 30d;
    add_header Pragma "public";
    add_header Cache-Control "public";
    }

    -> 0 effect

    it still sets the headers etc as if "serve static files directly through nginx" is activated

    but when i add for example

    add_header X-debug-message "A static file was served" always;

    this is set correctly

    as i said same config works on a different server ?

    1
    Comment actions Permalink
  • 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.

    1
    Comment actions Permalink
  • Avatar
    Atthawut Prathumrat

    nginx not ngnix :P

    1
    Comment actions Permalink
  • Avatar
    Markus

    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...

    1
    Comment actions Permalink
  • Avatar
    Robert Asilbekov

    @Tomasz Paluszkiewicz 

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

    0
    Comment actions Permalink
  • 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?

    0
    Comment actions Permalink
  • Avatar
    Yulia Plokhotnikova

    @minzkung Thanks! I corrected it :)

    0
    Comment actions Permalink
  • Avatar
    Peter Shaw (Edited )

    This works well for my WordPress installation and sets the expires headers that I'm after, however URLs for "virtual files" that don't physically exist on the server in WordPress [such as robots.txt] now return a 404 page. I believe this is because no further Nginx processing takes place after the headers are set with the location directive.

    Much advice on the internet suggests the use of try_files directive but the solution I am looking for is more along the lines of "for any file that doesn't physically exist on the server, ask Apache for it". Is something along these lines possible?

    I give robots.txt as an example but the solution needs to be generic so it can also handle secure files that are stored outside of the webroot with rewriting carried out by Apache.

     

    ***EDIT***

    From digging into the NGINX docs I see that what I actually want is a nested location directive - the initial block is lifted from the default nginx config within plesk, the second block adds expires headers to relevant files:

    location / {
        proxy_pass http://77.68.75.xxx:7080;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;

        location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|woff|woff2|svg|ttf)$ {
            etag on;
            if_modified_since exact;
            add_header Pragma "public";
            add_header Cache-Control "max-age=1209600, public";
        }
    }

    I can test the above by manually editing the /vhosts/system/[domain]/conf/nginx.conf file and restarting nginx systemctl restart nginx.

    This directive can't be applied using the Additional Nginx Directives option within plesk as a location directive for / already exists in the template, so any changes will be overwritten each time nginx.conf is recreated. How/where can I amend the plesk nginx template so that the individual vhost conf files that are generated use this modified directive?

    0
    Comment actions Permalink
  • Avatar
    Robert Asilbekov

    Maxie Jazz, put these directives into "Additional nginx directives". 

     

    0
    Comment actions Permalink
  • Avatar
    Robert Asilbekov

    Maxie Jazz, the error you get from Apache, means that you put directives into Apache additional directives, not into Nginx section. Also, review your additional apache directives maybe there are some mistakes. 

    0
    Comment actions Permalink
  • Avatar
    Maxie Jazz

    that's what I did and I get that error AH00526

    0
    Comment actions Permalink
  • Avatar
    Bharat Mahajan (Edited )

    Hi,

    my server is running on apache with Reverse Proxy Server (Nginx).

    what I need is configuration browser caching for only apache? or

    only Nginx, or both?

    for work as well.

    0
    Comment actions Permalink
  • 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.

    0
    Comment actions Permalink
  • Avatar
    Horacio Stolovitzky

    Is possible to enable globally?

    0
    Comment actions Permalink
  • Avatar
    Taha AK (Edited )

    Leverage doesn't have browser caching

    Help pls website: https://gokkusagikoltukyikama.com

    NGINX 

    location ~* .(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
    expires max;
    add_header Pragma "public";
    add_header Cache-Control "public";
    }

    0
    Comment actions Permalink
  • Avatar
    Tomasz Paluszkiewicz

    @Robert Asilbekov 

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

    0
    Comment actions Permalink
  • Avatar
    Greg Sevatos (Edited )

    Hello Ivan Postnikov

    This solutions works when adding to domain > Apache & nginx Settings, however when trying to add to a service plan, the rules are not added when synced with all subscriptions.

    Can you please advise on fix without having to alter web server templates (other supplied method)?

    Running Plesk Obsidian Version 18.0.26 on CloudLinux 7.8.

    Thank you.

    0
    Comment actions Permalink
  • Avatar
    Sharul Hafiz

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

    0
    Comment actions Permalink
  • Avatar
    Karl May

    Is here a urgent directive missing? tutorial wrote:

    location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
    expires 30d;

    but before second line need this line?

    try_files $uri @fallback;

    Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

    Example: image.png can be a generated image like a "CAPTCHA". So its needed that "image.png" to refresh every page request. With try_files its check its a static file or not, when not its do nothing. So I understood this.

    0
    Comment actions Permalink
  • Avatar
    Lenusch

    Any disadvantages to enable this for all Customers? 

    Does it make more CPU / RAM Load? 

    0
    Comment actions Permalink
  • Avatar
    Ivan Postnikov

    Hello Greg Sevatos

    Please, try this instruction: https://support.plesk.com/hc/en-us/articles/213903105

    0
    Comment actions Permalink
  • 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.

    0
    Comment actions Permalink
  • Avatar
    Linus Holtstiege

    The location directive should be prefixed with a backslash to enforce a dot before file extension.

     

    location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
    expires 30d;
    add_header Pragma "public";
    add_header Cache-Control "public";
    }
    0
    Comment actions Permalink
  • Avatar
    Unknown User (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? 

    0
    Comment actions Permalink
  • Avatar
    Bulat Tsydenov

    Hi Karl May
    Have you tried the suggested solution? Doesn't it break the caching for the specified types of files at all?

    0
    Comment actions Permalink
  • Avatar
    Myhost.ie Support

    Note this breaks SitePro if done server wide.

    0
    Comment actions Permalink
  • 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.

     

     

    0
    Comment actions Permalink
  • Avatar
    Robert Asilbekov

    @Bogdan Dovgopol

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

    0
    Comment actions Permalink
  • Avatar
    Daria Gavrilova

    Hello @Taha AK,

    As I can see from the provided screenshot the leverage browser caching does not work for Google Analytics only, as it does not host on your server.

    If you would like to get rid of this warning, I recommend you to check the possibility of hosting Google analytics.js file locally.
    For more information on this matter, please contact Google Analytics Support or search for possible solutions around the Internet.

    0
    Comment actions Permalink

Please sign in to leave a comment.

Have more questions? Submit a request