Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx load css files as a text/plain

Tags:

css

nginx

mime

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?

like image 742
Dawid Żurawski Avatar asked Feb 18 '19 14:02

Dawid Żurawski


People also ask

What is Sendfile on Nginx?

By default, NGINX handles file transmission itself and copies the file into the buffer before sending it. Enabling the sendfile directive eliminates the step of copying the data into the buffer and enables direct copying data from one file descriptor to another.

Does nginx support CSS?

Save this question.

What is MIME types Nginx?

MIME types are used to identify the type of information that a file contains. This document shows you how to add a MIME type to your Nginx configuration.

What is Default_type application octet stream?

The default_type only applies to file extensions that have not been defined in the mime. types file. If the file extension is missing from the mime. types file, it's fairly safe to assume application/octet-stream , which most browsers will treat as a binary file and download it rather than attempting to render it.


3 Answers

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;
    }
}
like image 75
Dawid Żurawski Avatar answered Oct 22 '22 12:10

Dawid Żurawski


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

like image 23
desw Avatar answered Oct 22 '22 12:10

desw


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.

like image 2
Adrian Madaras Avatar answered Oct 22 '22 14:10

Adrian Madaras