找不到favicon.ico解决方法

favicon.ico

favicon.ico文件是浏览器收藏网址时显示的图标,当客户端使用浏览器页面时,浏览器会自己主动发起请求获取页面的favicion.ico文件,但是当浏览器请求的favicion.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404错误

解决办法1

让服务器不记录访问日志
修改配置文件

server {
    server_name www.mylinuxops.com;
    access_log /var/log/nginx/access.log access_json;
    location / {
        root /data/www;
        index index.html;
  }
    location = /favicon.ico {       #精确定位ifavicon.ico文件
        log_not_found off;          #找不到文件时日志不记录
        access_log off;             #关闭日志记录
 }
}

启用配置前

[root@www ~]# tail -1 /var/log/nginx/access.log
{"@timestamp":"2019-05-31T20:08:31+08:00",   "host":"172.20.27.10",   "clientip":"172.20.136.96",   "size":162,   "responsetime":0.000,   "upstreamtime":"-",   "upstreamhost":"-",   "http_host":"www.mylinuxops.com",   "uri":"/favicon.ico",   "domain":"www.mylinuxops.com",   "xff":"-",   "referer":"-",   "tcp_xff":"",   "http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",   "status":"404"}

启用配置后

[root@www ~]# tail -1 /var/log/nginx/access.log
{"@timestamp":"2019-05-31T20:02:02+08:00",   "host":"172.20.27.10",   "clientip":"172.20.136.96",   "size":0,   "responsetime":0.000,   "upstreamtime":"-",   "upstreamhost":"-",   "http_host":"www.mylinuxops.com",   "uri":"/index.html",   "domain":"www.mylinuxops.com",   "xff":"-",   "referer":"-",   "tcp_xff":"",   "http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",   "status":"304"}
#此时日志中已经没有404的状态

解决办法2

将图标文件保存到指定的目录访问

server {
    server_name www.mylinuxops.com;
    access_log /var/log/nginx/access.log access_json;
    location / {
        root /data/www;
        index index.html;
  }
    location = /favicon.ico {
        root /data/www/image;
 }
}

[root@www ~]# ls /data/www/image/favicon.ico
/data/www/image/favicon.ico

再次访问后正常

[root@www ~]# tail -1 /var/log/nginx/access.log
{"@timestamp":"2019-05-31T20:25:38+08:00",   "host":"172.20.27.10",   "clientip":"172.20.136.96",   "size":0,   "responsetime":0.000,   "upstreamtime":"-",   "upstreamhost":"-",   "http_host":"www.mylinuxops.com",   "uri":"/favicon.ico",   "domain":"www.mylinuxops.com",   "xff":"-",   "referer":"-",   "tcp_xff":"",   "http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",   "status":"200"}

转载于:https://blog.51cto.com/11886307/2403945

你可能感兴趣的:(找不到favicon.ico解决方法)