Alexandru Cancescu
Alexandru Cancescu

Reputation: 998

Tell ngix to send file from within node app with reverse proxy

I have a node express app that is proxied by nginx. Nginx already handles most public static files, on /static

However, I have some files that need restricted access. After nginx proxies the HTTP request to my node app, on /restricted, I need to run my authorization logic and then, somehow, let nginx know that it should serve a specific file, from a directory that is not public.

I don't want to send the files directly from node as they are big and will block the main thread.

Upvotes: 1

Views: 73

Answers (1)

Richard Smith
Richard Smith

Reputation: 49752

  • Generate a response from your app containing an X-Accel-Redirect HTTP response header which specifies a URI beginning with /restricted/
  • Nginx will intercept the response and internally rewrite the URI
  • Configure a location block containing an internal directive

For example:

location /restricted {
    internal;
    alias /path/to/files;
}

See this document for more.

Upvotes: 1

Related Questions