主题
MIME 类型配置
MIME 类型用于告诉浏览器如何处理不同类型的文件,Nginx 通过 mime.types 文件管理静态资源的 Content-Type。
一、基本配置
Nginx 默认包含 mime.types 文件:
nginx
http {
include mime.types;
default_type application/octet-stream;
}- include mime.types:加载常用 MIME 类型映射
- default_type:未匹配文件类型时使用的默认 MIME
二、常用 MIME 类型示例
nginx
types {
text/html html htm shtml;
text/css css;
text/javascript js;
application/javascript js;
image/gif gif;
image/jpeg jpeg jpg;
image/png png;
image/svg+xml svg;
application/json json;
application/xml xml;
}三、自定义 MIME 类型
可在 server 或 location 块中单独设置:
nginx
location /downloads/ {
types {
application/octet-stream exe;
application/pdf pdf;
}
default_type application/octet-stream;
}- types:定义文件扩展名与 MIME 类型映射
- default_type:未匹配时使用的默认类型
四、优化建议
- 保持
mime.types文件最新,支持常用静态文件 - 对静态资源启用浏览器缓存和 gzip 压缩,提高加载效率
- 对特殊文件类型可在 location 块单独设置 MIME 类型
通过正确配置 MIME 类型,Nginx 能确保浏览器正确识别和处理不同文件,提高静态文件服务的可靠性和性能。
是否直接整理完整打包?