How to enable leverage browser caching for nginx?

Follow

Comments

42 comments

  • Avatar
    Remigio Ruberto (Edited )

    I use nginx in proxy mode.

    Applied this great articles for server-wide, but I've this nginx error:
    17815#0: *23099 open() "/var/www/vhosts/100asa.it/httpdocs/44541-home_default/epson-tm-t88v-ihub-ethernet-epos-nero.jpg" failed (2: No such file or directory)

    and no images was visible on my website.

    After enabled  Serve static files directly by nginx on Domains > 100asa.it > Apache & nginx Settings all back to normal and all images back to visible.

    Why this?

    0
    Comment actions Permalink
  • Avatar
    Daria Gavrilova

    Hello @Remigio Ruberto,

    The cause of such behavior is not clear.
    We have the corresponding article on this matter as well: Website is shown without static content on Plesk server

    0
    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
    Denis Bykov

    @Andre Utesch

    Maybe checking out all 'location' directives server-wide will provide some tips regarding possible configuration conflicts?

    # nginx -T | grep location
    0
    Comment actions Permalink
  • Avatar
    Linus

    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
    Francisco Garcia

    Thanks Linus,

    I've added that information to the article :)

    0
    Comment actions Permalink
  • Avatar
    Myhost

    Note this breaks SitePro if done server wide.

    0
    Comment actions Permalink
  • Avatar
    Ivan Postnikov

    Hello Myhost

    Thank you for the valuable warning.

    It will be useful for other Pleskians.

    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
    Ivan Postnikov

    Hello Greg Sevatos

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

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

Please sign in to leave a comment.

Have more questions? Submit a request