如何使用Nginx通过文件RIFF头设置MIME? [英] How to set MIME by file RIFF header using Nginx?

查看:144
本文介绍了如何使用Nginx通过文件RIFF头设置MIME?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件以其哈希名称保存,没有扩展名.但是我确定它是WAV,MP3之类的RIFF文件.如何使用nginx依赖于它的RIFF标头来添加MIME?

My Files are saved with the name of its hash without extension.But I'm sure it's RIFF files like WAV,MP3.How can I add MIME depend on it's RIFF header using nginx?

该文件大部分为mp3格式,因此我使用下面的位置块.

Most of the file is mp3 format, so I use the location block below.

config
location /audio/ {
     #default_type "audio/mpeg";
}

推荐答案

如何使用nginx依赖RIFF标头添加MIME?

How can I add MIME depend on it's RIFF header using nginx?

不能. Nginx不支持检查文件,然后根据其内容发送适当的标题.

You can't. Nginx doesn't have any support for inspecting files and then sending the appropriate headers based on their contents.

不过,您可以选择几种方法:

However there are a couple of options you could do:

1)将请求发送到您的网络服务器,检查代码中的文件,然后使用发送 X-Accel标头到nginx header("X-Accel-Redirect: ".$filenameToProxy);

1) Send the request to your webserver, inspect the file in your code and then use send the an X-Accel header to nginx header("X-Accel-Redirect: ".$filenameToProxy);

这将使nginx只需很少的处理时间即可检查文件.

That would leave nginx serving the file with only a small amount of processing time to inspect the file.

2)即使文件扩展名没有扩展名,也要在文件的URL中保留扩展名.提供指向文件的链接时,应保留扩展名的附件,因为扩展名使用户可以查看将要提供的文件类型,并且还允许nginx提供适当的mime类型. (我认为尽管我仍在对此进行测试)

2) Leave the extension on in the URL for the file, even if it's stored without an extension. When you serve a link to a file you should leave the extension attached, as that allows the user to see what file type is going to be served, and it also allows nginx to serve the appropriate mime type. (I think though I'm still testing this)

location ~* /audio/(.*)(wav|mp3|avi|ani) {
    add_header Content-Type $content_type
    try_files  /audio/$1
}

3)只需为所有文件保留扩展名.严重地.为什么要剥离文件扩展名?

3) Just leave the extension on for all files. Seriously. Why would you strip off the extension of a file?

这篇关于如何使用Nginx通过文件RIFF头设置MIME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆