首页 > 解决方案 > 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.

标签: node.jsnginxreverse-proxy

解决方案


  • 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.


推荐阅读