How to serve static files in nginx conditionally based on proxy response?

679 Views Asked by At

I have a folder /static/uploads/limited and I need to config nginx to serve some files to specific users. I also have an API /api/auth/canIDownloadThis which responses a json like

{
  "result:"true"
}

How can I make nginx to check the response of my API proxy_pass before serving that specific folder?

the pseudo code I need is something like this:

location /static/uploads/limited{
    IF /api/canIdownloadThis IS TRUE
    THEN alias /my_secret_folder_path
} 

Since I don't want to use basic_auth. Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

Thanks to @stephenKing, I need to compile nginx with --with-http_auth_request_module and then create an API that responses 403 or 200

location ~* /(\w+)/static/limited/(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ 
{
    auth_request /IC/contents/can_i_access_limited;
    alias /home/A/iB-system/C/$1/static/uploads/$2;
   }