Web部署到Nginx后静态文件加载不正常(MIME TYPE问题)
来源:互联网 发布:windows xp 截图工具 编辑:程序博客网 时间:2024/06/03 15:30
问题:
在没有将网站部署到nginx之前,一切运行正常,将网站部署到nginx之后,网页可以打开,但样式全都没有加载,浏览器里按下F12键,console有报错:
Resource interpreted as Stylesheet but transferred with MIME type text/plain
证明文件已经被找到,但是解析的格式不正确,由于这个问题是部署到nginx之后才发生的,因此从nginx的配置上找问题。
原因:
经过研究,发现需要引入mime.types这个文件,什么是MIME TYPES?
The Multipurpose Internet Mail Extensions (MIME) type is a standardized way to indicate the nature and format of a document. It is defined and standardized in IETF RFC 6838. The Internet Assigned Numbers Authority (IANA) is the official body responsible to keeping track of all official MIME types, and you can find the most up-to-date and complete list at the Media Types page.
Browsers often use the MIME type (and not the file extension) to determine how it will process a document; it is therefore important that servers are set up correctly to attach the correct MIME type to the header of the response object.
因此,有了MIME type,浏览器才知道该以何种方式处理文档,通过查阅官方文档,我们知道常用的两种mime type是:
Two primary MIME types are important for the role of default types:
text/plain
is the default value for textual files. A textual file should be human-readable and must not contain binary data.application/octet-stream
is the default value for all other cases. An unknown file type should use this type. Browsers pay a particular care when manipulating these files, attempting to safeguard the user to prevent dangerous behaviors.
如果不指定mime type,则默认会以text/plain的形式处理,也就是说,浏览器会以纯文本的形式来处理css和js文件,所以无法正常加载样式。
解决方法:
在Nginx的配置文件中指定默认的MIME type,从上面的引用中,我们得知一般指定为application/octet-stream即可,在CentOS7下,修改配置文件/etc/nginx/nginx.conf, 在http{ }中添加下面两行:
include /etc/nginx/mime.types;
default_type application/octet-stream;
重启nginx服务:
service nginx restart
再加载网站即可恢复正常了。
- Web部署到Nginx后静态文件加载不正常(MIME TYPE问题)
- Web Service获取文件的MIME type
- 解决Nginx反向代理后无法加载静态文件的问题
- 部署静态页面到nginx
- nginx升级到 0.8.53 之后,使用老的配置出现duplicate MIME type "text/html"问题的解决办法
- 文件MIME type类型
- 文件的MIME-TYPE
- Browser MIME Type 问题
- 升级到OSX10.11后cocopods不正常的问题
- centos下配置nginx+uwsgi部署多站点python以及静态文件的加载
- maven管理的web项目部署到tomcat后web-info的lib文件夹下不加载jar包的问题
- django+uwsgi+nginx部署,静态文件
- Java获取文件Content-Type(Mime-Type)
- nginx gzip duplicate MIME type “text/html”
- 部署静态web项目到Tomcat
- nginx加载伪静态文件.htaccess
- nginx中CSS重定向解析有问题——mime.type详解
- 阿里云上部署 django+uwsgi+nginx,admin静态文件问题
- TRPO(Trust Region Policy Optimization)(1)
- OkHttpGET和POST请求
- perceptron与多层网络
- 棋盘上的距离
- 实验四
- Web部署到Nginx后静态文件加载不正常(MIME TYPE问题)
- 开始写博客,开始新的学习方式
- HikariCP maxLifeTime
- 简单的angular过滤器
- RxJava(一:functional reactive programming)
- Angular模糊查询
- 负数的四则运算(重载运算符)
- strut2的ognl的s标签获取下拉框并选中
- Java 多线程设计模式之Guarded Suspension