nginx通过配置empty_gif解决请求favicon 404的问题

背景介绍

因为一些浏览器在访问网站时会默认去请求网站的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上添加如下配置:

1
2
3
location ~ ^/(apple-touch-icon.png|apple-touch-icon-precomposed.png|favicon.ico){
empty_gif;
}

测试

Wget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[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

1
2
3
4
5
6
7
8
9
10
[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]#

  

posted @   That's_it  阅读(2249)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
点击右上角即可分享
微信分享提示