116

I've recently decided to switch from Apache2 to Nginx. I installed Nginx on my CentOS server and setup a basic configuration. When I tried to load my site in browser (FF/Chrome) I noticed that css file is not loaded. I checked the error console and saw this message:

Error: The stylesheet http://example.com/style.css was not loaded because its MIME type, "text/html", is not "text/css".

I checked Nginx configuration and everything seems to be fine:

http {
    include /etc/nginx/mime.types;
    ..........
}

The mime type for css files is correctly set in /etc/nginx/mime.types.

text/css css;

Everything seems to be well configured but my css files are still not loaded. I have no explanation.

Another thing worth mentioning. Initially I installed Nginx using epel repositories and i got an old version: 0.8... It appeared to me that my problem was a bug in that version so I uninstalled 0.8 version, added nginx repository to yum and then installed latest version: 1.0.14. I thought the new version will solve my problem, but unfortunately it didn't so I am running out of ideas.

I appreciate any help.

Configuration files:

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
         root    /usr/share/nginx/html;
         index  index.html index.htm index.php;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
         include        fastcgi_params;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

/etc/nginx/mime.types

types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/x-javascript              js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;
    ..........................................
    other types here
    ..........................................
}
h7r
  • 4,944
  • 2
  • 28
  • 31
user337620
  • 2,239
  • 3
  • 19
  • 19
  • please paste in your config code. usually you've handled other other types well, and its skipping over your public files part which causes assets like css and images to return 404 errors, or in your case, mime type errors – Kristian Apr 09 '12 at 15:14
  • 1
    For my case, your question became an answer. Cheers. – Vassily Mar 12 '19 at 19:39

16 Answers16

138

Putting the include /etc/nginx/mime.types; under location / { instead of under http { solved the issue for me.

Amnon
  • 2,708
  • 2
  • 22
  • 21
  • 3
    also note, that if you are starting the config from scratch - except for the mime types maybe - `include mime.types;` does its job, since the (at least on windows, nginx 1.5.2) it is just relative to other config files. – omilke Jul 10 '13 at 07:11
  • 26
    also note, that you should fully refresh the site in your browser e.g. using ctrl+f5 to refresh to avoid getting cached files with incorrect headers. – CarelZA Jan 23 '14 at 08:09
  • 1
    This indeed works, but why? in http should be enough! in fact, it was working like that for over an year on a dev box and today it just stopped working without doing any change (no nginx update or even restart). – upsfeup May 12 '16 at 16:47
  • I was able to put the `include` inside `http`, as `include mime.types;` See https://gist.github.com/nishantmodak/d08aae033775cb1a0f8a – edan Sep 29 '22 at 21:53
48

I found an workaround on the web. I added to /etc/nginx/conf.d/default.conf the following:

location ~ \.css {
    add_header  Content-Type    text/css;
}
location ~ \.js {
    add_header  Content-Type    application/x-javascript;
}

The problem now is that a request to my css file isn't redirected well, as if root is not correctly set. In error.log I see

2012/04/11 14:01:23 [error] 7260#0: *2 open() "/etc/nginx//html/style.css"

So as a second workaround I added the root to each defined location. Now it works, but seems a little redundant. Isn't root inherited from / location ?

user337620
  • 2,239
  • 3
  • 19
  • 19
  • 2
    Is this a nginx bug? This is the only way I could get it to work. By the way I'm using Arch Linux, nginx 1.4.1-3. – tprk77 May 30 '13 at 02:53
  • @tprk77 not a bug, the accepted answer is a work-around, for a proper solution see my answer http://stackoverflow.com/a/23282158/1481489 – zamnuts Jun 25 '14 at 16:07
26

style.css is actually being process via fastcgi due to your "location /" directive. So it is fastcgi that is serving up the file (nginx > fastcgi > filesystem), and not the filesystem directly (nginx > filesystem).

For a reason I have yet to figure out (I'm sure there's a directive somewhere), NGINX applies the mime type text/html to anything being served from fastcgi, unless the backend application explicitly says otherwise.

The culprit is this configuration block specifically:

location / {
     root    /usr/share/nginx/html;
     index  index.html index.htm index.php;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
     include        fastcgi_params;
}

It should be:

location ~ \.php$ { # this line
     root    /usr/share/nginx/html;
     index  index.html index.htm index.php;
     fastcgi_split_path_info ^(.+\.php)(/.+)$; #this line
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # update this too
     include        fastcgi_params;
}

This change makes sure only *.php files are requested from fastcgi. At this point, NGINX will apply the correct MIME type. If you have any URL rewriting happening, you must handle this before the location directive (location ~\.php$) so that the correct extension is derived and properly routed to fastcgi.

Be sure to check out this article regarding additional security considerations using try_files. Given the security implications, I consider this a feature and not a bug.

zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • 1
    this should be the accepted answer, see also: http://forum.nginx.org/read.php?2,155222,155261#msg-155261 – Alfred Bez Mar 13 '15 at 09:40
  • 1
    It appears that this is still an issue, more than 4 years after the original question. How does the correct configuration look like if you only serve static content, i.e. **no** PHP? – rob May 19 '16 at 23:36
12

I ran into this issue too. It confused me until I realized what was wrong:

You have this:

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

You want this:

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

there appears to either be a bug in nginx or a deficiency in the docs (this could be the intended behavior, but it is odd)

Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72
7
  1. In your nginx.conf file, add mime.types to your http body like so:

    http {
        include /etc/nginx/mime.types;
        include /etc/nginx/conf.d/*.conf;
    }
    
  2. Now go to the terminal and run the following to reload the server:

    sudo nginx -s reload
    
  3. Open your web browser and do a hard reload: Right click on the reload button and select hard reload. On Chrome you can do Ctrl+Shift+R

RobC
  • 22,977
  • 20
  • 73
  • 80
GilbertS
  • 591
  • 8
  • 12
6

I followed some tips from the rest answers and discovered that these odd actions helped (at least in my case).

1) I added to server block the following:

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

I reloaded nginx and got this in error.log:

2015/06/18 11:32:29 [error] 3430#3430: *169 open() "/etc/nginx/html/css/mysite.css" failed (2: No such file or directory)

2) I deleted the rows, reloaded nginx and got working css. I can't explain what happend because my conf file became such as before.

My case was clean xubuntu 14.04 on VirtualBox, nginx/1.9.2, a row 127.51.1.1 mysite in /etc/hosts and pretty simple /etc/nginx/nginx.conf with a server block:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include /etc/nginx/mime.types;

    server {
        listen 80;
        server_name mysite;

        location / {
            root /home/testuser/dev/mysite/;
        }
    }
}
unVerum
  • 61
  • 1
  • 2
  • Ahah, same as you xD No mime-type, add the rule, reload, get the 404, remove the rule, reload, it works with good mime type... Ubuntu 19.10, Nginx 1.16.1 – Doubidou Jan 30 '20 at 11:49
3

I also face this issue, I tried a lot of solutions, but none really worked for me

Here's how I solved it;

A. Grant ownership of the domain document root directory (say my root directory is/var/www/nginx-demo) to the Nginx user (www-data) to avoid any permission issues:

sudo chown -R www-data: /var/www/nginx-demo

B. Confirm that your virtual host server block conforms to this standard (say I am using localhost as my server_name and my root as /var/www/nginx-demo/website)

server {
   listen 80;
   listen [::]:80;

   server_name localhost;

   root /var/www/nginx-demo/website;
   index index.html;

   location / {
            try_files $uri $uri/ =404;
   }
}

C. Test the Nginx configuration for correct syntax:

sudo nginx -t

If there are no errors in the configuration syntax the output will look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

D. Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

E. Hard refresh your website in your browser to avoid cached files with incorrect headers using the keyboard keys Ctrl + F5 or Ctrl + Fn + F5.

That's all.

I hope this helps.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
1

I had the same problem in Windows. I solved it adding: include mime.types; under http{ in my nginx.conf file. Then it still didn't work.. so I looked at the error.log file and I noticed it was trying to charge the .css and javascript files from the file path but with an /http folder between. Ex: my .css was in : "C:\Users\pc\Documents\nginx-server\player-web\css\index.css" and it was taking it from: "C:\Users\pc\Documents\nginx-server*html*\player-web\css\index.css" So i changed my player-web folder inside an html folder and it worked ;)

Nicola Munz
  • 73
  • 14
  • I have exactly the same issue. But moving to an html folder is not an option for me. Do you know why it is pre-pending with /html? – Gurnard Mar 22 '21 at 16:25
  • I had similar issue. Did you found the solution and reason behind it? – iheathers Apr 12 '21 at 05:30
  • I ran into a similar issue where /wordpress/ was appending to the path. So I edited wp-config.php file by following https://wordpress.org/support/article/changing-the-site-url/ to solve the issue. – iheathers Apr 12 '21 at 06:15
1

I actually took my time went through all the above answers on this page but to no avail. I just happened to change the owner and the permissions of directory and sub-directories using the following command.I changed the owner of the web project directory in /usr/share/nginx/html to the root user using:

chown root /usr/share/nginx/html/mywebprojectdir/*

And finally changed the permissions of that directory and sub-directories using:

chmod 755 /usr/share/nginx/html/mywebprojectdir/*

NOTE: if denied , you can use sudo

Young Emil
  • 2,220
  • 2
  • 26
  • 37
1

For me this was ad blocker installed on web browser. It was not letting load the style.css

Sanjeev
  • 11
  • 1
1

The same problem came up with Nginx 1.14.2 on Debian 10.6.

It can be resolved by setting the charset variable. By adding to the server block, beneath the server_name directive the following:

charset utf-8; # Use the appropriate charset in place of "utf-8"
Craig
  • 188
  • 1
  • 6
0

I was having the same issue and none of the above made any difference for me what did work was having my location php above any other location blocks.

location ~ [^/]\.php(/|$) {
fastcgi_split_path_info  ^(.+\.php)(/.+)$;
fastcgi_index            index.php;
fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
include                  fastcgi_params;
fastcgi_param   PATH_INFO       $fastcgi_path_info;
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
** The below is specifically for moodle **
location /dataroot/ {
internal;
alias <full_moodledata_path>; # ensure the path ends with /
}
Alex M
  • 106
  • 6
0

After also trying for hours my case was different from all others here. It was a dot in one of the folders of the path that I used as a hidden folder for the resources:

<link type = "text/css" href="../../.res/css/bootstrap.min.css" rel="stylesheet">

That was causing that the styles weren't rendered in the browser although they were being loaded with no errors from nginx. Only Firefox (not Chrome) complained about MIME types of css not being css but text.

Jorge
  • 1,350
  • 2
  • 10
  • 19
0

I know this IS late, but after scouring the internet for solutions and while all were concentrated on the config, actually the html itself is as Importnant

on the config side and after you've defined your project root, you should point to your statrics especially css in this case like this for example - I have my css in root/src

location ~*/src/.css {  
    include /etc/nginx/mime.types;
    add_header Content-type text/css;
}

and in you HTML: make sure to use the rel and the type properly

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

hope this helps others in the future

Majd
  • 328
  • 4
  • 12
0

I had to add the asterisk for it to work

location /* {include /usr/local/etc/nginx/mime.types;}

hasdal
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 01 '23 at 21:31
-7

add this to your ngnix conf file

add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://assets.zendesk.com; font-src 'self' https://themes.googleusercontent.com; frame-src https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'";
touzani
  • 1
  • 1