Is it possible to restrict download by MIME type/content type in nginx?

In standard nginx installations, there is a file called mime.types, which maps file extensions to MIME types.

So, for requests to static files, nginx checks the extension of the file to be sent to the client and then looks up from mime.types what Content-Type header to send to the client.

So, there cannot be any MIME magic in location blocks.

If you meant that nginx would pick up the MIME type based on the file content, that would be very inefficient.

Currently, when nginx is serving the request, it will look up the MIME type from the table and add the MIME type header. Then it will open the file and basically it simply copies it to the client socket.

However, if one wants nginx to select the MIME type based on the file content, in the worst case it has to read and interpret through the whole file in order to find out the correct file type. This would require much more CPU, and would make the performance much worse. That is the reason there is no such feature in nginx.

The best approach is to have proper extensions for the files you are sending to the clients.

If that is not possible, then you have to implement this file content inspection yourself in your preferred language.