Nginx无法加载css文件

我最近决定从Apache2切换到Nginx。 我在CentOS服务器上安装了Nginx,并设置了一个基本configuration。 当我尝试在浏览器(FF / Chrome)中加载我的网站时,我注意到没有加载css文件。 我检查了错误控制台,看到这个消息:

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

我检查了Nginx的configuration,一切似乎都很好:

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

css文件的mimetypes在/etc/nginx/mime.types中正确设置。

text/css css;

一切似乎configuration良好,但我的CSS文件仍然没有加载。 我没有解释。

另一件值得一提的事情。 最初,我使用epel存储库安装了Nginx,并且我得到了一个旧版本:0.8 …在我看来,我的问题是该版本中的一个错误,所以我卸载了0.8版本,添加了nginx版本库到yum,然后安装了最新版本:1.0。 14。 我以为新版本可以解决我的问题,但是不幸的是,我没有想法。

我感谢任何帮助。

configuration文件:

/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 .......................................... } 

include /etc/nginx/mime.types;location / {而不是根据http {为我解决了这个问题。

我在网上find了一个解决方法。 我添加到/etc/nginx/conf.d/default.conf下面:

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

现在的问题是,我的CSS文件的请求没有很好的redirect,好像root没有正确设置。 在error.log我看到

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

所以作为第二个解决方法,我将根添加到每个定义的位置。 现在它可以工作,但似乎有点多余。 不是从/ locationinheritance的root?

由于你的“位置/”指令, style.css实际上是通过fastcgi处理的。 所以它是fastcgi提供文件( nginx > fastcgi > filesystem ),而不是文件系统直接( nginx > filesystem )。

由于我有一个原因(我确定有一个指令),NGINX将MIMEtypestext/html应用于从fastcgi提供的任何东西,除非后端应用程序明确说明。

罪魁祸首就是这个configuration块:

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

它应该是:

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

这个改变确保只有*.php文件是从fastcgi请求的。 此时,NGINX将应用正确的MIMEtypes。 如果您有任何URL重写发生,您必须在位置指令( location ~\.php$之前处理这个以便正确的扩展名派生并正确地路由到fastcgi。

请务必阅读本文,了解有关使用try_files进行其他安全性考虑事项 。 鉴于安全性的影响,我认为这是一个function,而不是一个错误。

我也遇到了这个问题。 它让我感到困惑,直到我意识到什么是错的:

你有这个:

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

你要这个:

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

似乎是nginx中的一个错误或文档中的缺陷(这可能是预期的行为,但很奇怪)

我从其余的回答中得到了一些提示,发现这些奇怪的行为有助于(至less在我的情况下)。

1)我添加到服务器块下面:

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

我重新加载nginx,并在error.log中得到这个:

2015/06/18 11:32:29 [error] 3430#3430:* 169 open()“/etc/nginx/html/css/mysite.css”失败(2:没有这样的文件或目录)

2)我删除了行,重新加载nginx和得到工作的CSS。 我无法解释发生了什么,因为我的conf文件变得像以前一样。

我的情况是干净的xubuntu 14.04在VirtualBox,nginx / 1.9.2,一行127.51.1.1 mysite在/ etc / hosts和非常简单的/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; server { listen 80; server_name mysite; location / { root /home/testuser/dev/mysite/; } } } 

把这个添加到你的ngnix conf文件中

 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'"; 

我在Windows中有同样的问题。 我解决了它添加: 包括mime.types;http {在我的nginx.conf文件中。 然后它仍然没有工作..所以我看着error.log文件,我注意到它试图从文件path充电.css和JavaScript文件,但与/ HTTP文件夹之间。 例如:我的.css位于:“C:\ Users \ pc \ Documents \ nginx-server / player-web / css / index.css”,它是从“C:\ Users \ pc \ Documents \ nginx -server / html /player-web/css/index.css“所以我改变了我的播放器的网页文件夹内的HTML文件夹,它的工作;)