本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

Nginx(16)之响应MIME类型设定

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


types 指令可将请求文件扩展名映射到对应响应的 MIME 类型,扩展名不区分大小写,多个扩展可以映射到一种类型。其使用规则如下:

语法: types { ... }
默认: types {
        text/html  html;
        image/gif  gif;
        image/jpeg jpg;
     }
可使用的上下文: http, server, location

例:

types {
    application/octet-stream bin exe dll;
    application/octet-stream deb;
    application/octet-stream dmg;
}

即将扩展名为 .bin.exe.dll.deb.dmg 的 MIME 类型都映射为 application/octet-stream


其实 Nginx 默认就为我们做了很全面的映射,该配置在 /etc/nginx/mime.types 下,内容如下:

$ cat /etc/nginx/mime.types

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    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-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    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;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

并且该配置在主配置文件中默认就被 include 了,如下:

$ grep -R 'mime.types' /etc/nginx/
/etc/nginx/nginx.conf:    include             /etc/nginx/mime.types;

所以没有特殊情况我们是不用去配置什么的。


还有一项要说的就是 default_type 指令,它用来设定访问一个没有通过 types 指令定义对应映射关系的资源时默认的响应 MIME Type。

何时用它呢?这里提一个要求,如要使用 Nginx 搭建一个下载服务器,即不管浏览器访问什么类型的文件都默认执行下载操作
那么此时该怎么设定?难道把所有的扩展名的文件的 MIME Type 都映射到 application/octet-stream

当然不可能这么傻乎乎,,这时候就可以使用 default_type 指令了,,看如下示例:

server {
        listen 80;

        root /nginx/;
        types {}
        default_type application/octet-stream;
}

在上例配置中,types {} 清空了默认的映射配置,然后通过 default_type 指令设定了默认的响应类型,即此时所有请求的资源都是找不到对应的 MIME Type 的,所以默认响应的也就是 application/octet-stream 啦。。

当服务端响应的 MIME Type 是 application/octet-stream 时浏览器默认会启用下载操作。

# Nginx  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×