At the moment I have a PHP script set up with a chunk of code that reads a file from a dynamic location (which comes from a database) and echo's it out to the user in chunks. It's basically a file streaming script to hide the location of the original file.
However, this comes with a large overhead as PHP is clogging up my server's processes for each download. Instead I'd like to use nginx, and I'm wondering if it's possible to dynamically set the proxy_pass
directive to a value in my database.
For example, if my website has a page at http://example.com/download?hash=abcd
, I need to look up the download URL for the hash abcd
from my MySQL database and then serve that file using nginx and proxy_pass
.
Does anyone know how I can achieve this?
You can not connect to mysql direct from nginx, but you can add location for proxying like
location ~ /proxy/(?<domain>[^/]*)(?<url>/.*) {
internal;
proxy_pass http://$domain$url;
}
and use X-Accel-redirect header in your application which will connect to database, get download url by hash and set for example "X-Accel-Redirect: http://your.domain.com/proxy/other.domain.com/path/to/file.txt".