3

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.

Farshid Ashouri
  • 11,423
  • 5
  • 40
  • 56
  • 2
    Haven't used it, but something that goes into this direction is [ngx_http_auth_request_module](http://nginx.org/en/docs/http/ngx_http_auth_request_module.html). If your auth API returns correct HTTP status codes, this might work. – StephenKing Nov 13 '15 at 22:14
  • Thank you. You can write down this and I will accept it as answer. – Farshid Ashouri Nov 13 '15 at 22:39

1 Answers1

2

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;
   }
Farshid Ashouri
  • 11,423
  • 5
  • 40
  • 56