nginx 加载 css 文件作为文本/纯文本 [英] nginx load css files as a text/plain

查看:305
本文介绍了nginx 加载 css 文件作为文本/纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 nginx 配置为仅加载静态文件,但我不知道为什么 .css 文件被解释为文本/纯文本 - 最后浏览器无法加载它.

I'm configuring nginx to load only static files and I don't know why .css files are interpreted as text/plain - finally browser couldn't load it.

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:13000/styles.css".

当我在 css 文件的 Web 浏览器中检查响应头时:

when I check response header in web browser of css file:

Content-Type: text/plain

我知道在堆栈上我们有很多问题,我已经阅读了它们但仍然不起作用.

I know that on stack we have a lot of issues with it, I've already read them but still doesn't work.

在 html 文件中我刚刚导入了 css:

in html file I've just import css:

<link href="styles.css" rel="stylesheet" type="text/css"/>

我的/etc/nginx/nginx.conf 是:

my /etc/nginx/nginx.conf is:

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;
        }
    }
}

我尝试不使用任何位置部分或尝试使用:

I tried without any location part or tried with:

location ~ \.css {
 add_header Content-Type text/css;
}

在其他线程的一些回复中,我看到这部分是必需的:

In some responses in other threads I saw that this part is required:

default_type  application/octet-stream;
include       /etc/nginx/mime.types;

我在 http 部分添加了它,然后在 serverlocation 中添加,仍然没有帮助我.

I added it in http part and after that in server and then in location, still didn't help me.

我还能做些什么来修复它?

Is there anything else what can I do to fix it?

推荐答案

以防万一有人遇到同样的问题并使用 docker.这是关键词 - 我将 docker 与图像 nginx:latest 一起使用,这会导致问题,我已将其更改为 nginx:1.14.2 并且一切正常.

Just in case somebody has the same problem and use docker. This is key word - I use docker with image nginx:latest which causes problems, I've changed it to nginx:1.14.2 and everything is working fine.

在html中,导入css:

in html, import css:

<link href="styles/styles.css" rel="stylesheet" type="text/css"/>

我的 nginx.conf 配置:

my nginx.conf configuration:

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;
    }
}

这篇关于nginx 加载 css 文件作为文本/纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆