13

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".

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.

in html file I've just import css:

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

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;

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?

Matthias
  • 13,607
  • 9
  • 44
  • 60
Dawid Żurawski
  • 519
  • 1
  • 5
  • 18
  • Found on w3c documentation : `In HTML5, the type attribute is no longer required for CSS`. Do you still have the problem without `type`? (URL : https://www.w3schools.com/tags/att_style_type.asp) – Fefux Feb 18 '19 at 14:54
  • after change into ```` error disappeared in browser but css file from nginx is still as a plain/text - css just doesn't work without errors – Dawid Żurawski Feb 18 '19 at 14:58
  • Do you use fastcgi ? If so, this answer may help you : https://stackoverflow.com/questions/10075304/nginx-fails-to-load-css-files/23282158#23282158 (even if the problem seems to be for `text/html` instead of `text/plain`) – Fefux Feb 18 '19 at 15:07
  • I don't use it, nginx.conf in question is everything what I have, it's simply frontend application without backend – Dawid Żurawski Feb 18 '19 at 15:24

4 Answers4

27

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.

in html, import css:

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

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;
    }
}
Dawid Żurawski
  • 519
  • 1
  • 5
  • 18
  • 8
    String "include mime.types;" should be inside the http section, not the server. In the new config, this is the case and therefore works correctly. – Nikmoon Jan 27 '20 at 19:57
  • 7
    just adding : "include mime.types;" and "sendfile on;" worked for me – dubem uzuegbu Jul 01 '20 at 16:47
3

I solved the issue for myself. The problem on my side was the actual nginx configuration, let me explain.

Not working (before):

  • my dockerfile contained this line of code: "COPY deployment/nginx.conf /etc/nginx/nginx.conf"
  • my nginx.conf contained the "http {" part

How I fixed it:

  • I updated my docker file to: "COPY deployment/nginx.conf /etc/nginx/conf.d/default.conf" (check the new path where I am copying)
  • my nginx.conf did not contain any more "http {" block and just the "server {" block. (This works because I am adding a new config file).

Voila! Hope it helps! All in all the culprit was where I was copying the config file and the content of my conf file itself.

Adrian Madaras
  • 347
  • 3
  • 13
1

I had a similar issue once on my testing server. what i found out was that nginx was doing every thing in the right way. The problem was the referencing of the files. The browser could find the resources but could not load them from the described base.

location / { # default base
    root /var/www/html/myfiles; # my root folder
    try_files $uri /index.html;
    include  /etc/nginx/mime.types;
}

changed the base to...

location /myfiles { # changed base
    root /var/www/html; # default root folder
    try_files $uri /index.html;
    include  /etc/nginx/mime.types;
}

it worked seamlessly

desw
  • 13
  • 1
  • 3
0

I had this problem. In order to solve it for both my static javascript and css files was to add type AND uic-remove as followed:

<link rel="stylesheet" uic-remove href="/index.css" type="text/css">
<script uic-remove type="application/javascript" src="./index.js"></script>

Note: The Javascript link was not throwing any warning or error like the css's warning, however it was nonetheless not working either and I thank the heavens that the fix worked for both. Good Luck!

GiselleMtnezS
  • 105
  • 1
  • 2
  • 13