By user337620

2012-04-09 15:10:42 8 Comments

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 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:


user  nginx;
worker_processes  1;

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

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;


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_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
    #location ~ \.php$ {
    #    proxy_pass;

    # pass the PHP scripts to FastCGI server listening on
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass;
    #    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;


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


@Masih Jahangiri 2019-04-28 21:19:10

you should fully refresh the site in your browser e.g. using ctrl+f5 to refresh to avoid getting cached files with incorrect headers.

@Alex M 2019-03-01 09:46:52

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/ {
alias <full_moodledata_path>; # ensure the path ends with /

@Young Emil 2018-03-18 13:39:32

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

@nachoreimat 2017-04-05 13:45:04

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

@touzani 2016-06-22 15:50:31

add this to your ngnix conf file

add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; frame-src; object-src 'none'";

@unVerum 2015-06-18 11:13:56

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

events {
    worker_connections  1024;

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

    server {
        listen 80;
        server_name mysite;

        location / {
            root /home/testuser/dev/mysite/;

@zamnuts 2014-04-25 00:26:28

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

@Alfred Bez 2015-03-13 09:40:49

this should be the accepted answer, see also:,155222,155261#msg-155261

@rob 2016-05-19 23:36:52

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?

@Jonathan Vanasco 2014-01-04 20:29:55

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)

@zamnuts 2014-04-24 21:40:15

this did not solve the problem on windows with nginx/1.6.0

@Amnon 2012-08-09 01:11:03

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

@omilke 2013-07-10 07:11:48

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.

@CarelZA 2014-01-23 08:09:09

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.

@rclai 2014-09-01 07:47:48

This surprisingly worked! What the??!

@mtyurt 2015-07-21 15:17:16

This is truly fantastic.

@upsfeup 2016-05-12 16:47:43

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

@eav 2017-06-12 09:07:20

u sir, saved my day

@Efe Özazar 2019-08-15 08:18:38

This saved my problem too but i still cant make any sense of it. My problem was my css file is partially loaded. I am using webpack maybe it caused the problem. Don know for sure. Thanks anyway saved my day too :)

@user337620 2012-04-11 18:14:55

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 ?

@tprk77 2013-05-30 02:53:16

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.

@zamnuts 2014-06-25 16:07:31

@tprk77 not a bug, the accepted answer is a work-around, for a proper solution see my answer

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

38 Answered Questions

[SOLVED] How do I vertically center text with CSS?

31 Answered Questions

[SOLVED] Is there a CSS parent selector?

  • 2009-06-18 19:59:36
  • jcuenod
  • 1744768 View
  • 2895 Score
  • 31 Answer
  • Tags:   css css-selectors

34 Answered Questions

[SOLVED] Change an HTML5 input's placeholder color with CSS

3 Answered Questions

nginx change root folder for specyfic url

  • 2013-06-27 07:24:06
  • kabra
  • 13009 View
  • 6 Score
  • 3 Answer
  • Tags:   nginx path root

28 Answered Questions

[SOLVED] How do I give text or an image a transparent background using CSS?

  • 2009-04-30 09:00:02
  • Stijn Sanders
  • 1508146 View
  • 2171 Score
  • 28 Answer
  • Tags:   html css opacity

26 Answered Questions

[SOLVED] Set cellpadding and cellspacing in CSS?

3 Answered Questions

[SOLVED] Share Nginx server configuration

  • 2014-06-11 04:04:32
  • Anam
  • 1557 View
  • 4 Score
  • 3 Answer
  • Tags:   php nginx

16 Answered Questions

[SOLVED] When to use margin vs padding in CSS

  • 2010-02-03 03:20:12
  • Alex Angas
  • 736129 View
  • 2214 Score
  • 16 Answer
  • Tags:   css padding margin

2 Answered Questions

[SOLVED] can't interpret PHP on RHEL + nginx

  • 2017-06-08 17:33:56
  • Night
  • 107 View
  • 1 Score
  • 2 Answer
  • Tags:   php linux nginx

Sponsored Content