再次 – NGINX今天给我很多问题:)
像往常一样,我已经尝试了一段时间,但似乎无法解决这个问题:
我只是configurationNGINX处理我的.css文件等于我的.php文件(parsingCSS文件中的PHP标签)。 这工作完美,文件被发现和交付。
我可以使用FIrebug进行debugging,并且一切正常(在打开的<link>标签内显示.css的内容)。 所以,一切正常,对不对? 错误。
它有CSS,但它不解释! 我的意思是:显然,CSS的文件types(或应用程序types,不pipe)是错误的。 页面可以访问CSS,但并不打扰到实际使用它。
我查了/试过了:
body {background-color: #000; } 这里的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“networking”响应头:
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
我想我只是回答了我自己的问题。 Content-Type是text / html的问题吗? 我怎样才能删除?
我个人的猜测是,我必须以某种方式使用它
include /etc/nginx/mime.types; default_type application/octet-stream;
但我不确定…任何人都知道如何解决这个问题?
TLDR; CSS文件被正确传递,但它似乎不被用作浏览器的CSS。 (经过testing,在Apache上工作)
你可以尝试添加
<?php header('Content-Type: text/css'); ?>
(可选的编码)到您的phpparsing的CSS文件。 这必须作为第一条指令发生(更精确:在任何输出发生之前;如果在调用header()之前发生任何输出,php将会出错),并且在它之前不能有任何空格。
这将发送HTTP头与内容应该完全是你想要的。
您可以通过PHP的header()函数或通过nginx的add_headerconfiguration选项来设置正确的头文件。
正如Jonas Wielicki所提到的,使用PHP,您可以通过添加来设置正确的标题
<?php header("Content-Type: text/css"); ?>
在您发送任何输出之前,
另一种方法是添加一个add_headerconfiguration选项到你的nginxconfiguration的相应部分,在你的情况下:
add_header Content-Type text/css;