zoukankan      html  css  js  c++  java
  • 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上添加如下配置:

    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]#
    

      

  • 相关阅读:
    B16-高可用OpenStack(t版)集群分布式存储Ceph部署
    B15-openstack高可用(t版)-cinder计算节点集群
    B14-openstack高可用(t版)-cinder控制节点集群
    B13-openstack高可用(t版)-horazion计算节点集群
    B12-openstack高可用(t版)-Neutron计算节点集群
    B11-openstack高可用(t版)-Neutron控制/网络节点集群
    mysql(windows 10 安装)
    docker 容器 centos + tomcat + jdk
    docker 安装
    docker 把镜像打包成文件
  • 原文地址:https://www.cnblogs.com/thatsit/p/6361903.html
Copyright © 2011-2022 走看看