css - nginx 加载 css 文件作为文本/纯文本

标签 css nginx mime

我正在配置 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/

相关文章:

CSS,具有动态内容的固定位置 Div - 如何在它下面定位另一个 Div?

php - Nginx connect() 失败错误

使用 Google Cloud Kubernetes TCP 负载均衡器的 Nginx 状态代码 499

email - MIME Con​​tent-Type 头字段的最大长度?

linux - 如何在 Linux 中获取 URL 的 Mime

css - Wordpress 标题图片没有响应

php - Yii Booster - 包含进度条时的 td 单元格高度

javascript - 有没有办法在 HTML5/CSS 中制作一个滚动的 "viewport"框?

Nginx WebSocket 反向代理限制

c - 如何使用 libetpan 在 HTML mime 消息中嵌入图像