How to specify mimetype of files with no extension

2020-05-26 08:32发布

I have a server with nginx. And I have a lot of images - pngs and jpgs saved as files with no extension (like "123123123_321312").

When I use tag "img" in html page, I get theese messages in console:

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". 

Is there a way to make nginx add header with correct mimetype of the requested file?

2条回答
你好瞎i
2楼-- · 2020-05-26 09:19

You should use the default_type directive :

server {
   ...
   default_type text/html;

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

   location /images/jpg {
      default_type image/jpeg;
   }
}
查看更多
劳资没心,怎么记你
3楼-- · 2020-05-26 09:27

I also have to add.

Mickaël Le Baillif solution also works if your web page is downloaded instead of executed. Because nginx thinks that it should be default_type application/octet-stream; even though the official documentation says:

To make a particular location emit the “application/octet-stream” MIME type for all requests, the following configuration can be used

And points to location /download/ This also gives:

Resource interpreted as Image but transferred with MIME type application/octet-stream: "http://xxxxx.com/"

Nginx version: 1.6.2

查看更多
登录 后发表回答