背景介绍

因为一些浏览器在访问网站时会默认去请求网站的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]#

  

nginx通过配置empty_gif解决请求favicon 404的问题的更多相关文章

  1. nginx 本地配置(解决跨域问题)

    前端做跨域本身就是扯淡的事情.后台人员不配合说什么都是白搭.索性整理了一下心得,(可以直接部署自配置)发不多说上代码: 1 #user nobody; 2 worker_processes 1; 3 ...

  2. nginx session 配置失效解决

    nginx 反向代理后台web服务器session path导致的session 失效,特此总结下配置方法: 配置如下: location ^~ /2016tyjf_dev/djwechat { pr ...

  3. thinkphp nginx 上配置 并解决get获取到数据现象

    server { listen 80; server_name XXXX.funova.net XXX.funova.com; root /opt/newgm; index index.php; lo ...

  4. Windows中Nginx配置nginx.conf不生效解决方法(路径映射)

    Windows中Nginx配置nginx.conf不生效解决方法 今天在做Nginx项目的时候,要处理一个路径映射问题, location /evaluate/ { proxy_pass http:/ ...

  5. Nginx 高级配置--关于favicon.ico

    Nginx 高级配置--关于favicon.ico 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浏览器会默认帮咱们访问官网的图标 1>.浏览器访问网站"htt ...

  6. 解决Nginx+Tomcat下客户端https请求跳转成http的问题

    Nginx上开启https,  后端使用Tomcat,  两者间走http协议, 但发现如果tomcat应用存在跳转时, 则客户端浏览器会出现400 Bad Request的错误, 通过抓包发现原因是 ...

  7. nginx中配置404错误页面的教程

    什么是404页面如果网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是NGI ...

  8. NGINX: 配置跨域请求

    说明: 内容全部来自 SegmentFault Developer Nginx 配置跨域请求 跨域请求失败, nginx 报错: 403 No 'Access-Control-Allow-Origin ...

  9. nginx+php-fpm配置后页面显示空白的解决方法以及用nginx和php-fpm解决“502 Bad Gateway”问题

    For reference, I am attaching my location block for catching files with the .php extension: location ...

随机推荐

  1. Spring集成Kafka-注解,xml配置2种方式实现

    准备工作: 1.安装kafka+zookeeper环境 2.利用命令创建好topic 参考官网 http://kafka.apache.org/documentation/ 一XML配置文件方式实现

  2. 《杜增强讲Unity之Tanks坦克大战》5-子弹

    5 子弹 本节的任务是创建子弹的Prefab   image 首先从Model/Shell找到子弹的模型,拖入Hierarchy中,添加刚体组件,所有属性默认值. 添加Capsule Collider ...

  3. python的字符串格式化

    1.python到底有那几种字符串格式化模块? python有3种格式化字符串的方法: 传统的%字符串格式符 str.format函数 字符串模版template 新的python 3.6+还提供了新 ...

  4. ADO.net中DataTable的应用

     一.思维导图 二.知识点描述 (1)构造函数 DataTable() 不带参数初始化DataTable类的新实例 DataTable(string tableName) 用指定的表名初始化DataT ...

  5. Annotation 使用备忘

    title: Annotation 使用备忘 date: 2016-11-16 23:16:43 tags: [Annotation] categories: [Programming,Java] - ...

  6. Java时间格式的使用,bug难时真是坑

    很简单的问题,尤其是新手开发,要多自己动手写代码,都说程序猿大都是程序的搬用工,其实不然,好的写手,和差的写手,区别就在是不是会花时间读读代码,并且自己动手实践一下,其实一个程序范这样的错误,绝对是低 ...

  7. split命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/lkforce/article/details/71547313 Linux中的文件,特别是日志文件,特别大了不好打开,可以 ...

  8. js备忘录4

    for (var key in obj) { console.log('对象属性名:' , key); if (obj[key] instanceof Object) { sayName(obj[ke ...

  9. 第十一次PSP

  10. CS小分队第二阶段冲刺站立会议(6月1日)

    昨日成果:完成了,文件图标的获取,以及代码删除获取文件的图标的功能:实现了从桌面拖动一个应用到窗体进行添加的功能: 遇到问题:获取的文件图标太小了,比较模糊:从外面拖动文件到窗体时,图标一直获取错误, ...