Nginx – Recreating functionality of nginx’s X-Accel-Redirect on apache httpd (able to use URLs instead of absolute paths)

apache-2.2nginx

I currently have a web app set up behind nginx and need to move it over to Apache httpd.

One feature that is currently missing that I really want to re-implement is a X-Accel-Redirect based feature.

Unlike X-Sendfile, the value for X-Accel-Redirect is treated as an URL, so I can perform all of the same functions on it that I can any other location. In this case, I'm using it to get the actual file from an external server rather than directly from the local server.

Here's the relevant configuration:

location ~* ^/secure-download(.*) {
    internal;

    set $file_info  'attachment; filename=${upstream_http_filename}';
    add_header      Content-Disposition $file_info;

    # proxy configuration code here

    proxy_cache             cachename;
    proxy_cache_valid       200 302 60m; # cache successful responses for 60min
    proxy_cache_valid       404     1m; # cache missing responses for 1min

    resolver                172.31.0.2 8.8.4.4 8.8.8.8 valid=300s;
    resolver_timeout        10s;

    proxy_pass              'https://proxy-server.example.org/$1';

}

There are a lot of advantages to this:

  • I don't have to store the content locally
  • I can manually set the filename downloaded
  • The browser doesn't have to redirect to a different URL
  • I don't have to make the web application download and then serve the content, which is slower.

The problem, as far as I can tell, is there isn't a similar functionality in apache httpd — one that basically says, "Stop serving all of the content I just created and instead serve the URL located in this header".

If this isn't possible I'll probably just rewrite the web app so it downloads the content of the file from the external URL and then serves it, even though that's really a non-ideal situation.

Best Answer

Submitting comment as answer for future reference.

There is no directly comparable functionality for apache.

There is an x-sendfile module for apache, but it does not support non-filesystem urls.

What one could do is use httpfs, a fuse filesystem module, to "mount" remote http servers into the local filesystem, and then x-sendfile from those mount points.

That's a "use at one's own risk" solution.

One alternative is as mentioned- change the app to redirect to an external URL.

Another that was not discussed is to use some webserver supported authorization- check for the presence of a cookie or similar- in front of an ordinary remote proxy configuration. This is more sympathetic to the usage of webservers, though may not work for reasons as yet not discussed.