nginx Content-Type MIME

时间:2025-03-16 07:19:56

nginx配置中有一般会有配置:

include ;

用来引入文件类型和MIME类型映射表,在nginx安装目录的conf下有配置文件,打开配置可见:

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/.-descriptor      jad;
    text/                      wml;
    text/x-component                      htc;

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

    ......
}

例如:text/html, text表示主类型,html表示子类型

作用就是nginx会根据被代理文件的具体类型,如:html、png、txt 等,自动地在http响应头中自动添加
Content-Type : text/html、Content-Type : image/png、Content-Type : text/plain。让浏览器可以根据协议MIME——Multipurpose Internet Mail Extension,自动在浏览器本地找到对应的插件打开图片、音乐、视频等

比如,我们再导出excel时候,会加入这句:
("Content-Type", "application/octet-stream"),而文件中可见:
application/octet-stream   bin exe dll;
说明告诉浏览器,导出是一个二进制文件流,不需要找插件自动打开,而是后续由本地电脑安装的excel程序单独打开

又比如,当代理一个前端VUE、REACT程序时,如果不加include ; 代理到浏览器的静态资源(css、js、图片等)就没有对应Content-Type字段,页面不能被浏览器正常显示(很多css样式就没有)