nginx配置都媒体文件Content-Type

发布时间 2023-04-04 11:39:17作者: 熊先生不开玩笑

用nginx做代理用来做多媒体文件的下载,因为有些文件名后缀类型没有匹配。所以返回个请求端的响应头会是:
Content-Type: application/octet-stream,给请求端造成困扰。例如:acc 文件需要是: Content-Type: audio/aac,wav 文件需要是:Content-Type: audio/x-wav。一种临时的解决办法是nginx配置文件里加上类型对应:

vim /etc/nginx/mime.types;

加入如下配置,也可以按需增加:

types {
     # Audio
     audio/midi mid midi kar;
     audio/mp4 aac f4a f4b m4a;
     audio/mpeg mp3;
     audio/ogg oga ogg opus;
     audio/x-realaudio ra;
     audio/x-wav wav;
     # Images
     image/bmp bmp;
     image/gif gif;
     image/jpeg jpeg jpg;
     image/png png;
     image/svg+xml svg svgz;
     image/tiff tif tiff;
     image/vnd.wap.wbmp wbmp;
     image/webp webp;
     image/x-icon ico cur;
     image/x-jng jng;
     # JavaScript
     application/javascript js;
     application/json json;
     # Manifest files
     application/x-web-app-manifest+json webapp;
     text/cache-manifest manifest appcache;
     # Microsoft Office
     application/msword doc;
     application/vnd.ms-excel xls;
     application/vnd.ms-powerpoint ppt;
     application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
     application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;
     # Video
     video/3gpp 3gpp 3gp;
     video/mp4 mp4 m4v f4v f4p;
     video/mpeg mpeg mpg;
     video/ogg ogv;
     video/quicktime mov;
     video/webm webm;
     video/x-flv flv;
     video/x-mng mng;
     video/x-ms-asf asx asf;
     video/x-ms-wmv wmv;
     video/x-msvideo avi;
     # Web feeds
     application/xml atom rdf rss xml;
     # Web fonts
     application/font-woff woff;
     application/font-woff2 woff2;
     application/vnd.ms-fontobject eot;
     application/x-font-ttf ttc ttf;
     font/opentype otf;
     # Other
     application/java-archive jar war ear;
     application/mac-binhex40 hqx;
     application/pdf pdf;
     application/postscript ps eps ai;
     application/rtf rtf;
     application/vnd.wap.wmlc wmlc;
     application/xhtml+xml xhtml;
     application/vnd.google-earth.kml+xml kml;
     application/vnd.google-earth.kmz kmz;
     application/x-7z-compressed 7z;
     application/x-chrome-extension crx;
     application/x-opera-extension oex;
     application/x-xpinstall xpi;
     application/x-cocoa cco;
     application/x-java-archive-diff jardiff;
     application/x-java-jnlp-file jnlp;
     application/x-makeself run;
     application/x-perl pl pm;
     application/x-pilot prc pdb;
     application/x-rar-compressed rar;
     application/x-redhat-package-manager rpm;
     application/x-sea sea;
     application/x-shockwave-flash swf;
     application/x-stuffit sit;
     application/x-tcl tcl tk;
     application/x-x509-ca-cert der pem crt;
     application/x-bittorrent torrent;
     application/zip zip;
     application/octet-stream bin exe dll;
     application/octet-stream deb;
     application/octet-stream dmg;
     application/octet-stream iso img;
     application/octet-stream msi msp msm;
     application/octet-stream safariextz;
     text/css css;
     text/html html htm shtml;
     text/mathml mml;
     text/plain txt;
     text/vnd.sun.j2me.app-descriptor jad;
     text/vnd.wap.wml wml;
     text/vtt vtt;
     text/x-component htc;
     text/x-vcard vcf;
}

重载nginx配置,用curl -I , 命令查看响应头。

另外:以上只是一种很普通的做法。实际如果我们是自己做文件存储服务,需要需要读取文件的文件头获得文件类型。而不是根据文件命名后缀。