NStal
NStal

Reputation: 999

How to set MIME by file RIFF header using Nginx?

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?

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

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

Upvotes: 3

Views: 1988

Answers (3)

cnst
cnst

Reputation: 27218

What about the following? Does it not produce desired effects?

location /audio/ {
    default_type audio/mpeg;
}

Or the following?

location /audio/ {
    default_type audio/wav;
}

Upvotes: 1

Danack
Danack

Reputation: 25701

How can I add MIME depend on it's RIFF header using 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) 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);

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

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) Just leave the extension on for all files. Seriously. Why would you strip off the extension of a file?

Upvotes: 4

Jordan
Jordan

Reputation: 3022

RIFF is a type of WAV file so you should be able to use:

wav audio/wav
wav audio/x-wav

Upvotes: 1

Related Questions