加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

php-与Postgres一起使用的图像/媒体服务器

发布时间:2020-12-13 20:53:27 所属栏目:Nginx 来源:网络整理
导读:我最近有一个nginx php-fpm服务器,该服务器提供如下图像: header('Content-Type: image/png');echo file_get_contents('example_image.png');exit(); 我开始意识到,无论是否缓存了映像,服务器上的性能都受到了极大的打击. CPU利用率极高,只有最少的连接数才

我最近有一个nginx php-fpm服务器,该服务器提供如下图像:

header('Content-Type: image/png');
echo file_get_contents('example_image.png');
exit();

我开始意识到,无论是否缓存了映像,服务器上的性能都受到了极大的打击. CPU利用率极高,只有最少的连接数才达到100%.因此,我开始将图像卸载到CDN上,并立即获得了性能上的提高,但是在某些情况下,我仍然要求通过服务器来提供图像,这使我想到了图像/媒体服务器的概念.

我的问题是,是否应该使用特定类型的服务器?可以与数据库通信以找到图像位置并提供服务的数据库吗?文件系统类型?或者我还是最好只启动另一个Nginx php-fpm实例并创建一个类似于CDN的结构实现,其中:

media.example.com 

仅指向该服务器,因此对Web服务器没有性能影响吗?

最佳答案
您的问题是您将图像视为字符串而不是流.绝对没有必要使用file_get_contents()将其全部加载到php中并继续回声.查找诸如readfile()和与PG相关的LOB功能之类的内容:

header('Content-Description:?File?Transfer');
header('Content-Type:?application/octet-stream');
header('Content-Disposition:?attachment;?filename='.basename($file));
header('Content-Transfer-Encoding:?binary');
header('Expires:?0');
header('Cache-Control:?must-revalidate');
header('Pragma:?public');
header('Content-Length:?'?.?filesize($file));
ob_clean();
flush();
readfile($file);
exit;

http://php.net/manual/en/function.readfile.php

$db?=?new?PDO('pgsql:dbname=test?host=localhost',?$user,?$pass);
$db->setAttribute(PDO::ATTR_ERRMODE,?PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt?=?$db->prepare("select?oid?from?BLOBS?where?ident?=??");
$stmt->execute(array($some_id));
$stmt->bindColumn('oid',?$oid,?PDO::PARAM_STR);
$stmt->fetch(PDO::FETCH_BOUND);
$stream?=?$db->pgsqlLOBOpen($oid,?'r');
header("Content-type:?application/octet-stream");
fpassthru($stream);

http://php.net/manual/en/pdo.pgsqllobopen.php

类似地,查看与缓存控制相关的标头.无需重新发送浏览器缓存中已存在的图像.尝试在可能的情况下发送304未修改:

Make PHP page return “304 Not Modified” if it hasn’t been modified

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读