1

I have a very simple case of serving static files via nginx yet I can't figure it out.

I want all URLs beginning with /static/ to serve static files from directory /foo/bar/dir1 and if the file isn't there, serve from /foo/bar/dir2, if not there, return 404.

So for example when handling URL /static/some/file.png I want nginx to first try

/foo/bar/dir1/some/file.png 

and then

/foo/bar/dir2/some/file.png 

I know I should probably use something like this

location /static/ { try_files .... something ..... } 

but the documentation on try_files is very unclear to me. I tried a lot of combinations but nothing seems to work. Multiple alias directives would do the job but it won't work. I think the solution must be very simple but I cant get it right. It's kind of hard to debug how nginx resolves all these locations and files...

1 Answer 1

2

You can customize the root (make sure to update the try_files after). And also make sure there is no root directive in location /

location ~* ^/static/(.+)$ { root /; try_files /foo/bar/dir1/some/$1 /foo/bar/dir2/some/$1 =404; } 

Edit: Removed the need of the static folder.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.