Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify mimetype of files with no extension in NGINX config?

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?

like image 636
Dmitriy Lezhnev Avatar asked Nov 22 '12 09:11

Dmitriy Lezhnev


People also ask

Where is MIME type stored?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.

What is MIME type in Nginx?

Nginx allows you to map file extensions to mime types. As the documentation says, it even comes with a pre built list of mime types (pasted at the end of the question). I've always trusted this list, and things work great, but now I've noticed that some types are missing.

What is MIME type in Nginx?

It is a standard that indicates the nature and format of a document, file, or assortment of bytes. All web browsers use the MIME type to determine how to process a URL. Hence, it is essential that Nginx send the correct MIME type in the response’s Content-Type header.

Why does Nginx report text/plain as the default Content Type?

Nginx will report text/plain if you don't define a default content type. As new content types are invented or added to web servers, web administrators may fail to add the new MIME types to their web server's configuration.

How to override content type in Nginx?

Reload/restart the Nginx service. For example, GNU/Linux user can run: Another option is to add the following directly to config file: It is also possible to override content type for given URL pattern. For example, I edited /etc/nginx/domains/cyberciti.biz/default.conf and added the following in server context: Save and close the file.

How to add a new MIME type in Linux?

For adding a new MIME type, all it takes is editing the /etc/mime.types file. Let's say you want to add MIME type with extension .btc, then 1. Check If MIME type already exists Open a command line and enter the line below (replace btc with your extension) Now, this command will output a line, If that MIME type is already added.


1 Answers

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;
   }
}
like image 70
Mickaël Le Baillif Avatar answered Oct 12 '22 23:10

Mickaël Le Baillif