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?

1 Answer 1

4

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.

5
  • Thank you for the solution, but in this scenario, the files located in /data/ won't be cached at all? Commented Nov 2, 2017 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? Commented Nov 2, 2017 at 19:24
  • The data located on dedicated volume, mounted under /mnt/data/. Commented Nov 2, 2017 at 19:42
  • Sorry, I've misunderstood the problem. Commented Nov 2, 2017 at 19:53
  • That's okay, thank you very much for the solution! Commented Nov 2, 2017 at 20:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.