大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

I have the following stanza in my nginx virtual host config. The goal is to only allow direct (deep link) downloads of only video content types, and return a 404 or some other error for anything else: location ~* ^/webdata/.+(\.m…

问题原文

原帖链接 · Rouben · CC BY-SA 3.0

I have the following stanza in my nginx virtual host config. The goal is to only allow direct (deep link) downloads of only video content types, and return a 404 or some other error for anything else:

location ~* ^/webdata/.+(\.mp4|\.m4v|\.mpg|\.mpeg|\.mpg|\.mts|\.avi)$ { root /srv/data; }

As you can see the above achieves this rather crudely by matching the end of the requested URL. If it doesn't end in .mp4, .m4v, .mpg, etc. the stanza isn't triggered and the client doesn't get the file.

Ideally I'd like to be able to be able to do something like this (this obviously doesn't work, since $content_type refers to the content-type HTTP reqest header from the client, and not the content-type of the requested resource, if I am not mistaken):

location /webdata { if ($content_type =~ 'video/.*') { root /srv/data/; } }

Is there a way to do this?

Comment: Based on question 577471, it seems like nginx relies purely on filenames/URI, rather than using something like MIME magic to figure out the content type based on actual file content headers, like most modern Linux desktop environments do. If this is true, then this may not be possible.

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。