dongpin3794 2013-12-05 21:04
浏览 141
已采纳

Nginx proxy_pass到动态主机

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?

  • 写回答

1条回答 默认 最新

  • duanrenzou1619 2013-12-05 21:51
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog