背景介绍
因为一些浏览器在访问网站时会默认去请求网站的favicon,但是我的网站(Tengine)上并没有这些icon图片,因此在访问日志里会出现大量的404错误,会触发一些没必要日志告警。
我们可以通过配置nginx empty_gif来给请求返回一个1x1,大小为43字节的空白图片来解决这个问题。这个给请求返回一个空白图片的做法经常用在网站统计等需求里。
empty_gif模块的官方链接地址:http://nginx.org/en/docs/http/ngx_http_empty_gif_module.html
配置
我统计我的日志里访问icon的请求,请求的目标文件总共有三个,所以对这些请求全部做返回空白图片处理。
nginx上添加如下配置:
location ~ ^/(apple-touch-icon.png|apple-touch-icon-precomposed.png|favicon.ico){ empty_gif; }
测试
Wget
[root@thatsit vhosts]# cd /tmp/ [root@thatsit tmp]# wget http://www.thatsit.cc/favicon.ico --2017-01-09 11:35:32-- http://www.thatsit.cc/favicon.ico Resolving www.thatsit.cc... 10.10.10.101 Connecting to www.thatsit.cc|10.10.10.101|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 43 [image/gif] Saving to: “favicon.ico” 100%[=============================================================================================>] 43 --.-K/s in 0s 2017-01-09 11:35:32 (8.85 MB/s) - “favicon.ico” saved [43/43] [root@thatsit tmp]# [root@thatsit tmp]# wget http://www.thatsit.cc/apple-touch-icon-precomposed.png --2017-01-09 11:36:29-- http://www.thatsit.cc/apple-touch-icon-precomposed.png Resolving www.thatsit.cc... 10.10.10.101 Connecting to www.thatsit.cc|10.10.10.101|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 43 [image/gif] Saving to: “apple-touch-icon-precomposed.png” 100%[=============================================================================================>] 43 --.-K/s in 0s 2017-01-09 11:36:29 (12.3 MB/s) - “apple-touch-icon-precomposed.png” saved [43/43] [root@thatsit tmp]# [root@thatsit tmp]# wget http://www.thatsit.cc/apple-touch-icon.png --2017-01-09 11:36:47-- http://www.thatsit.cc/apple-touch-icon.png Resolving www.thatsit.cc... 10.10.10.101 Connecting to www.thatsit.cc|10.10.10.101|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 43 [image/gif] Saving to: “apple-touch-icon.png” 100%[=============================================================================================>] 43 --.-K/s in 0s 2017-01-09 11:36:47 (12.3 MB/s) - “apple-touch-icon.png” saved [43/43] [root@thatsit tmp]# ll total 24 -rw-r--r-- 1 root root 43 Sep 28 1970 apple-touch-icon.png -rw-r--r-- 1 root root 43 Sep 28 1970 apple-touch-icon-precomposed.png -rw-r--r-- 1 root root 43 Sep 28 1970 favicon.ico [root@thatsit tmp]#
Curl
[root@thatsit tmp]# curl -XGET -I http://www.thatsit.cc/apple-touch-icon.png HTTP/1.1 200 OK Server: Tengine Date: Mon, 09 Jan 2017 03:39:32 GMT Content-Type: image/gif Content-Length: 43 Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT Connection: keep-alive [root@thatsit tmp]#