-->

Set MIME type for server

2019-09-04 13:04发布

问题:

i've seen a bunch of topics about setting the MIME types.

But, no one helped me.

He is my problem:

I have a Rails website with bunch of video in .ogv and .mov formats, located in /public folder. I refer to these files in HTML5 video tag.

There is no problem with .mov files, they are played nice in WebKit browsers.

The problem is with .ogv.

I think, it's because wrong MIME type for .ogv.

Here is what i get for .mov (correct):

$ curl -I http:/mywebsite.com/video.mov

HTTP/1.1 200 OK
Date: Sun, 03 Apr 2011 19:57:41 GMT
ETag: "4d98744c-1bb-87563c0"
Last-Modified: Sun, 03 Apr 2011 13:21:16 GMT
Content-Type: video/quicktime
Content-Length: 443

And here is what i get for .ogv:

$ curl -I http://mywebsite.com/video.ogv

HTTP/1.1 200 OK
Date: Sun, 03 Apr 2011 19:22:20 GMT
ETag: "4d987dcf-379884-81c533dc"
Last-Modified: Sun, 03 Apr 2011 14:01:51 GMT
Content-Type: application/octet-stream
Content-Length: 3643524

Instead of "application/octet-stream" i need "video/ogg".

I have a Mongrel server (no Apache as front-end), as i recently got to know. So, there is no way to use .htaccess.

I need to set MIME-type for regular files, not responses from controller etc.

I've tried several ways, described in my previous question: HTML 5 video (ogv) and MIME types

But i does't works. I still get "application/octet-stream".

My Questions are:

  1. How can i set mime types for regular files, not responses from controller ?

  2. Does Mongrel serves files, located in /public directory, or something else ?

回答1:

I've figured it out.

All the methods, described in my previous question are setting MIME-types for Rails environment, not Mongrel.

I need additional MIME-types at Mongrel level, not Rails.

So, by starting mongrel via …

/usr/bin/mongrel_rails,

… I need to pass a YAML file to it, that contains additional MIME-types, i want to declare. This YAML file might look like this (mongrel_mime_types.yml):

.ogv: video/ogg
.ogg: application/ogg
.ogx: application/ogg

I keep it in /config/initializers, for convenience.

So, by starting Mongrel, i need to pass this file:

/usr/bin/mongrel_rails -m /path_to_my_project/http/config/initializers/mongrel_mime_types.yml

Now, if i check with curl, I'm getting correct MIME:

$ curl -I http://mywebsite.com/video.ogv

HTTP/1.1 200 OK
Date: Mon, 04 Apr 2011 05:38:01 GMT
ETag: "4d987dcf-379884-81c533dc"
Last-Modified: Sun, 03 Apr 2011 14:01:51 GMT
Content-Type: video/ogg
Content-Length: 3643524