霜天部落 | 关注LAMP高性能、高并发架构的设计与研究

uchome头像调用产生大量错误日志

用了 uchome 一段时间以后,发现apache 错误日志文件越来越大,几乎达到两个G了,于是打开 error_log 查看了一下,里面90%都是 File does not exist 这种记录。

[Tue Jul 12 13:19:36 2011] [error] [client 192.168.1.220] File does not exist: /home/www/wwwroot/uc/data/avatar/001/50/70/58_avatar_small.jpg, referer: http://xxx.xxx.com/home/space.php
[Tue Jul 12 13:19:36 2011] [error] [client 192.168.1.220] File does not exist: /home/www/wwwroot/uc/data/avatar/000/18/89/21_avatar_small.jpg, referer: http://xxx.xxx.com/home/space.php
[Tue Jul 12 13:19:39 2011] [error] [client 192.168.1.228] File does not exist: /home/www/wwwroot/uc/data/avatar/001/43/02/53_avatar_middle.jpg, referer: http://xxx.xxx.com/home/space.php?do=home
[Tue Jul 12 13:19:39 2011] [error] [client 192.168.1.228] File does not exist: /home/www/wwwroot/uc/data/avatar/001/50/70/58_avatar_small.jpg, referer: http://xxx.xxx.com/home/space.php?do=home
[Tue Jul 12 13:19:43 2011] [error] [client 192.168.1.220] File does not exist: /home/www/wwwroot/uc/data/avatar/001/60/55/87_avatar_small.jpg, referer: http://xxx.xxx.com/home/space.php?do=topic&topicid=16

这些错误是由 uchome 页面调用个人头像时由于用户没有上传头像而产生的。

解决这个错误可以使用 apache rewrite 模块。代码如下:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^uc/data/avatar/[0-9]+/[0-9]+/[0-9]+/[0-9]+_avatar_(big|middle|small).(jpg|gif|png)$ uc/images/noavatar_$1.gif [L]