3

My nginx setup contains the following location rules:

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

    location /data/ {
            root /mnt/data;
    }

The problem is, when I try to access one of the following files (jpg | jpeg | png | gif | ico | css | js | pdf) in the /data/ folder I get 404 Not Found error, cause the first location rule overrides the second. I tried to do something like this:

    location ~* /data/.*\.jpg$ {
            root /mnt/data;
            expires 7d;
    }

But this doesn't seems to work. Could you please help me what solutions are available for me in this case?

3

Solutions are:

#1 Rule duplication

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

location /data/ {
    root /mnt/data;

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

#2 Symlink

You can create a symlink: $document_root/data -> /mnt/data.

  • Thank you for the solution, but in this scenario, the files located in /data/ won't be cached at all? – Mike Nov 2 '17 at 19:19
  • Well, no. location ~* has precendence, so /data/image.jpg will be served with expires 7d; from the $root/data directory, not the /mnt/data. Where are the assets located? – Paweł Tatarczuk Nov 2 '17 at 19:24
  • The data located on dedicated volume, mounted under /mnt/data/. – Mike Nov 2 '17 at 19:42
  • Sorry, I've misunderstood the problem. – Paweł Tatarczuk Nov 2 '17 at 19:53
  • That's okay, thank you very much for the solution! – Mike Nov 2 '17 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.