In an image host script, how can I measure an image's bandwidth consumption?
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 / 开发者_Go百科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
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:
grep -c filename.jpg /var/log/nginx.access.log
You could then simply multiply that count by the filesize to get the bandwidth for that image.
精彩评论