如何在NGINX配置中指定没有扩展名的文件的mimetype?

Dmi*_*nev 17 nginx mime-types

我有一个nginx服务器.我有很多图像 - pngs和jpgs保存为没有扩展名的文件(如"123123123_321312").

当我在html页面中使用标签"img"时,我会在控制台中收到这些消息:

Resource interpreted as Image but transferred with MIME type application/octet-stream: "http://xxxx/images/1350808948_997628". jquery.js:2
Resource interpreted as Image but transferred with MIME type application/octet-stream: "http://xxxx/images/1343808569_937350". 

有没有办法让nginx添加标头与所请求文件的正确mimetype?

Mic*_*lif 17

您应该使用该default_type指令:

server {
   ...
   default_type text/html;

   location /images/png {
      default_type image/png;
   }

   location /images/jpg {
      default_type image/jpeg;
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果其他人对此感到困惑(就像我一样),请确保在设置default_type并重新加载nginx配置后在浏览器中进行完全刷新.清除缓存或Chrome中的shift-F5,否则浏览器将获得304并假设原始内容类型仍然有效. (3认同)