Nginx try_files (folders + files) backup

Given this folder structure:

root folder + default + settings1.txt + settings2.txt ... + settingsN.txt + user00001 + settings1.txt ... ... + userN + settings1.txt ... 

And this url example:

 domain.com/user00009/settings1.txt 

or

 domain.com/xavi/somefile.txt 

I would like to write a rule that allows me to do:

 folder exists ? check file : 404 file exists ? serve users file : serve default file 

I tried using try_files, but I think I can only use $ uri, I get the whole url, it would be great if I could work with bullets ($ 1 = user00009 and $ 2 = settings1.txt)

Then maybe I could put:

 location / { root /... try_files $1/$2 default/$2 =404; } 

Any idea?

Note. I know that I can file servers from outside nginx (in this case django), but Im trying to speed things up

+6
source share
1 answer
 location ~ ^(/[^/]+)(/.+)$ { root ...; if (!-d "$document_root$1") { return 404; } try_files $1$2 /default$2 =404; } 
+8
source

Source: https://habr.com/ru/post/908480/


All Articles