And again - NGINX is giving me many Questions today :)

Like always, I already tried around for a while, but cant seem to fix this issue:

I just configured NGINX to handle my .css files equal to my .php files (to parse PHP tags inside the CSS file). This works perfectly, and the file is found and delivered.

I could debug it with FIrebug, and everything is OK (it displays the contents of the .css inside the opened <link> tag). So, everything working, right? Wrong.

It has the CSS, but it does not interpret it! What I mean by this: apparently, the file-type of the CSS (or aplication-type, whatever) is wrong. The Page can access the CSS, but doesnt bother at all to actually use it.

What I checked / tried:

  • There are no PHP errors inside of the .css, so that one is out
  • The .css is accessible. I can call the URI manually, or check if the included URL finds it: both works
  • The .css has no syntax errors (i switched to a css that just has body {background-color: #000; }
  • It works whitout NGINX
  • I deleted the browser cache / restarted NGINX after config rewrites

Here the configuration:

server {
        listen      80;
        server_name localhost;

        access_log  /var/log/nginx/board.access_log;
        error_log   /var/log/nginx/board.error_log warn;

        root        /var/www/board/public;

        index       index.php;
        fastcgi_index   index.php;

        location / {
            try_files $uri $uri /index.php;

        location ~ (\.php|\.css)$ {
            try_files $uri =404;

            include /etc/nginx/fastcgi_params;
            #keepalive_timeout 0;           
            fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;

Firebug 'Network' Response Header:

Connection  keep-alive
Content-Encoding    gzip
Content-Type    text/html
Date    Sat, 16 Jun 2012 10:08:40 GMT
Server  nginx/1.0.5
Transfer-Encoding   chunked
X-Powered-By    PHP/5.3.6-13ubuntu3.7

I think I just answered my own question. Is the Content-Type text/html the problem? How can I remove that?

My personal guess is that I have to use this in some way

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

But I'm not sure... anyone an idea how to solve this?

TLDR; CSS file is delivered correctly, but it doesnt seem to be 'used' as CSS from the browser. (Tested, works on apache)

  • I think I just answered my own question. Is the Content-Type text/html the problem? How can I remove that? – Katai Jun 16 '12 at 10:13

You could try to add

  header('Content-Type: text/css');

(optionally with encoding) to your php-parsed css file. This must happen as first instruction (more accurate: before any output happens; php will error on you if any output happened before a call to header()) and there must not be any spaces before it.

This will send the HTTP header given together with the content which should do exactly that what you want.

  • This actually works! Great - but would there be a NGINX way too? Over the config? If not, I'll accept your answer, but I would prefer to keep this things in the NGINX config – Katai Jun 16 '12 at 10:23
  • See Carstens reply for that, I don't know NGINX that well :). – Jonas Schäfer Jun 16 '12 at 11:27

You can set the correct header either via PHP's header() function or via nginx's add_header configuration option.

As Jonas Wielicki already mentioned, using PHP, you set the correct header by adding

<?php header("Content-Type: text/css"); ?>

to your file before you send any output.

The other way is to add an add_header configuration option to the appropriate section of your nginx configuration, in your case:

add_header Content-Type text/css;
  • Sure that text/html is correct? Shouldn't it be text/css? – Jonas Schäfer Jun 16 '12 at 11:27
  • Well of course it should be text/css. Thank you, I've corrected it. – Carsten Jun 16 '12 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.