【发布时间】:2019-07-11 22:58:55
【问题描述】:
我正在将 nginx 配置为仅加载静态文件,但我不知道为什么 .css 文件被解释为文本/纯文本 - 最后浏览器无法加载它。
Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:13000/styles.css".
当我在 css 文件的 Web 浏览器中检查响应头时:
Content-Type: text/plain
我知道在堆栈上我们有很多问题,我已经阅读了它们但仍然无法正常工作。
在 html 文件中我刚刚导入 css:
<link href="styles.css" rel="stylesheet" type="text/css"/>
我的 /etc/nginx/nginx.conf 是:
worker_processes 1;
events {
worker_connections 512;
}
http {
server {
listen 80;
server_name 0.0.0.0:80;
include /etc/nginx/mime.types;
root /project/app;
location ~* ^.+\.(js|css)$ {
expires 10d;
}
}
}
我尝试不使用任何位置部分或尝试使用:
location ~ \.css {
add_header Content-Type text/css;
}
在其他线程的一些回复中,我看到这部分是必需的:
default_type application/octet-stream;
include /etc/nginx/mime.types;
我在http 部分添加了它,然后在server 和location 中添加了它,仍然没有帮助我。
我还能做些什么来解决它?
【问题讨论】:
-
在 w3c 文档中找到:
In HTML5, the type attribute is no longer required for CSS。没有type,你还有问题吗? (网址:w3schools.com/tags/att_style_type.asp) -
在更改为
<link href="styles.css" rel="stylesheet"/>后,浏览器中的错误消失了,但来自 nginx 的 css 文件仍然是纯文本 - css 无法正常工作 -
你使用 fastcgi 吗?如果是这样,这个答案可能会对您有所帮助:stackoverflow.com/questions/10075304/…(即使问题似乎出在
text/html而不是text/plain) -
我没用过,有问题的 nginx.conf 就是我所拥有的一切,它只是没有后端的前端应用程序