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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效