redirect丢失的图像在一个特定的目录

我在/img/有一个图像目录。 当对该目录中不存在的图像提出请求时,我想提供/img/default.jpg 。 我将如何添加到我的nginxconfiguration?

做这个:

 location /img { try_files $uri /img/default.jpg; } 

这不会将请求redirect到default.jpg,而是将其请求的文件内容replace为default.jpg内容。

更多信息: http : //nginx.org/en/docs/http/ngx_http_core_module.html#try_files

redirect会非常混乱,需要使用if指令。 你也可以这样做。 更多信息: http : //nginx.org/en/docs/http/ngx_http_rewrite_module.html#if