我正在配置 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
,仍然没有帮助我。我还能做些什么来修复它?
最佳答案
以防万一有人遇到同样的问题并使用docker。
这是关键词 - 我将 docker 与图像 nginx:latest 一起使用,这会导致问题,我已将其更改为 nginx:1.14.2 并且一切正常。
在 html 中,导入 css:
<link href="styles/styles.css" rel="stylesheet" type="text/css"/>
我的 nginx.conf 配置:
worker_processes 1;
events {
worker_connections 512;
}
http {
include mime.types;
sendfile on;
server {
listen 80;
server_name 0.0.0.0:80;
root /project/app;
}
}
关于css - nginx 加载 css 文件作为文本/纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749319/