apachenginxreverse-proxywsgix-sendfile

How to setup authorization for proxy request a-la x-sendfile?


In a setting of apache + mod_wsgi and nginx + uwsgi, what could be the way to setup web-server to proxy big "intranet" files requests?

What I am thinking about is a way a la x-sendfile, but where the wsgi application points to a file URL "intranet" location in its response, web-server downloads and uploads the file to the original requester without revealing it's "intranet" location. Of course, nothing happens if there is no authentication and access rights check on wsgi application side.

It's very hard to find this kind of setup by googling, not even sure what term to use.

By "intranet" I mean files, accessible via HTTPS requests from the proxy server, which may have its own credentials to them, but not from public internet or by local filesystem (like is the use case with x-sendfile)


Solution

  • If using mod_wsgi in daemon mode, you can return an empty HTTP 200 response with Location response header and when that is seen by the Apache process proxying to the mod_wsgi daemon process, it will evaluate that as a sub request. The path in that could be mapped to a new URL handler in Apache configuration which is actually a proxy setup which sends the request to another downstream backend server. The response from that will then be proxied back to the client. If you don't want that secondary URL handler to be visible outside, ie., someone can't request it direct if they work out the URL path, you need to use a mod_rewrite rule to reject any request if it isn't a sub request.

    So you might have something like:

    RewriteCond %{IS_SUBREQ} false
    RewriteRule ^/hidden/stuff/ - [F]
    
    ProxyPass /hidden/stuff/ http://backend.example.com/
    

    The WGSI response would then be empty HTTP 200 response with Location header of:

    Location: /hidden/stuff/some-file-name
    

    The sub request request would end up being:

    http://backend.example.com/some-file-name
    

    against backend server with response proxied back to client.