在图像主机脚本中,如何测量图像的带宽消耗?
我正在增强 PHP 图像托管脚本,但我遇到了一个问题:客户端请求了一个函数来将每个图像的使用带宽量限制为 100MB/天 这包括缩略图
到目前为止,缩略图已直接提供,无需使用 PHP(使用 nginx) 我尝试过使用 php 脚本而不是直接的 .jpg,并使用大量缓存的代码(使用 APC 的缓存来存储所使用的带宽量,并使用 X-Sendfile 标头提供图像),但服务器只是阻塞由于连接数量非常非常多,
是否有一种更有效的方法来测量缩略图使用的带宽而不阻塞 CPU 和 RAM? 通常如何解决这个问题?
谢谢
I'm enhancing a PHP image hosting script, but I'm facing a problem: the client has requested a function to limit the amount of used bandwidth, for each image, to 100MB / day
This includes thumbnails
Up to now, thumbnails have been served directly without using PHP (with nginx)
I've tried using a php script instead of a direct .jpg, with a VERY heavily cached code (using APC's cache to store the amount of bandwidth used, and serving the image with the X-Sendfile header), but the server just chokes due to the very,very high amount of connections
Is there a more efficient way to measure bandwidth used by thumbnails without choking the CPU and RAM?
How is this problem usually tackled?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解析服务器访问日志。我相信 nginx 它们位于 /var/log/nginx/access.log 。您可能需要更改权限以允许访问,或者如果您不习惯这样做,则可以使用 cron 定期将该文件复制到可访问的文件夹中。
我想如果你缓存了结果,并且每小时只检查一次或其他什么,那么你可以使用 grep -c 来做到这一点。
我很想知道,如果您选择一个已知已被访问的文件,则运行需要多长时间:
然后您可以简单地将该计数乘以文件大小以获得该图像的带宽。
Parse the server access logs. I believe in nginx they are at /var/log/nginx/access.log . You'll likely need to change permissions to allow access, or you could use cron to regularly copy that file into an accessible folder if you aren't comfortable doing that.
I guess that if you cached results, and only checked once an hour or something then you could do this using grep -c.
I'd be interested to know, if you pick a file you know has been accessed, how long does it take to run:
You could then simply multiply that count by the filesize to get the bandwidth for that image.