Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
            fastcgi_pass    127.0.0.1:7777;
        }
    }

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)

share|improve this question

migrated from stackoverflow.com Jun 16 '12 at 13:01

This question came from our site for professional and enthusiast programmers.

    
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

2 Answers 2

You could try to add

<?php
  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.

share|improve this answer
    
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 Wielicki 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;
share|improve this answer
    
Sure that text/html is correct? Shouldn't it be text/css? –  Jonas Wielicki 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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