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 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站