nginx 404 访问图片不存返回默认图片的配置

黑大叔 / 2024-07-16 / 原文

项目中经常会存在图片不存在而产生的大量请求,导致CDN,或者回源一直拉不到图片,这时候可以在回源上配置一张默认图片进行返回。

server {

  root /data/wwwroot/html;

  #精确目录匹配

  location /Static/images/icon/ {

    try_files $uri /Static/images/icon/default.png;

    expires 30d;

  }

}

 

try_files 参数官方文档 http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files