Nginx无法加载css文件

问题描述 投票:58回答:10

我最近决定从Apache2切换到Nginx。我在CentOS服务器上安装了Nginx并设置了基本配置。当我尝试在浏览器(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配置,一切似乎都很好:

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

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

text/css css;

一切似乎配置得很好但我的css文件仍然没有加载。我没有解释。

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

我感谢任何帮助。

配置文件:

/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.的/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
    ..........................................
}
css nginx mime
10个回答
72
投票

把qazxsw poi放在qazxsw poi而不是qazxsw poi下解决了这个问题。


-3
投票

将此添加到您的nginx conf文件中

chown root /usr/share/nginx/html/mywebprojectdir/*

29
投票

我在网上找到了一个解决方法。我在/etc/nginx/conf.d/default.conf中添加了以下内容:

include  /etc/nginx/mime.types;

现在的问题是对我的css文件的请求没有很好地重定向,就好像没有正确设置root。在error.log中我看到了

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

因此,作为第二种解决方法,我将根添加到每个定义的位置。现在它有效,但似乎有点多余。是不是继承自/ location的root?


19
投票

由于你的“位置/”指令,location / {实际上正在通过fastcgi进行处理。因此,fastcgi正在提供文件(http {),而不是直接提供文件系统(location ~ \.css { add_header Content-Type text/css; } location ~ \.js { add_header Content-Type application/x-javascript; } )。

由于某个原因我还没弄明白(我确定某处有指令),NGINX将mime类型style.css应用于fastcgi提供的任何内容,除非后端应用程序明确说明。

罪魁祸首是这个配置块具体:

nginx > fastcgi > filesystem

它应该是:

nginx > filesystem

此更改确保只从fastcgi请求text/html文件。此时,NGINX将应用正确的MIME类型。如果您正在进行任何URL重写,则必须在location指令(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; } )之前处理此问题,以便派生正确的扩展并正确路由到fastcgi。

一定要看看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; } 。鉴于安全隐患,我认为这是一个功能,而不是一个错误。


8
投票

我也遇到过这个问题。在我意识到错误之前,它让我很困惑:

你有这个:

*.php

你要这个:

location ~\.php$

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


1
投票

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

1)我在服务器块中添加了以下内容:

this article regarding additional security considerations using try_files

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

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

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

我的案例是关于VirtualBox的清洁xubuntu 14.04,nginx / 1.9.2,/ etc / hosts中的行include /etc/nginx/mime.types; default_type application/octet-stream; 以及带有服务器块的非常简单的/etc/nginx/nginx.conf:

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

0
投票

我在Windows中遇到了同样的问题。我解决了它添加:include mime.types;在http {在我的nginx.conf文件中。然后它仍然无法工作..所以我查看了error.log文件,我注意到它试图从文件路径收取.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文件夹中更改了我的player-web文件夹,它工作正常;)


0
投票

我实际上花了我的时间在这个页面上完成了上述所有答案,但无济于事。我碰巧使用以下命令更改了目录和子目录的所有者和权限。我使用以下命令将location ~ \.css { add_header Content-Type text/css; } 中的web项目目录的所有者更改为127.51.1.1 mysite用户:

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

最后使用以下命令更改了该目录和子目录的权限:

/usr/share/nginx/html

注意:如果拒绝,您可以使用sudo


0
投票

我遇到了同样的问题,上面没有任何一个对我有什么不同,我的位置php在任何其他位置块之上的工作是什么。

root

0
投票

您应该在浏览器中完全刷新网站,例如使用ctrl + f5刷新以避免使用不正确的标头获取缓存文件。

© www.soinside.com 2019 - 2024. All rights reserved.