Nginx无法加载css文件

use*_*620 63 css mime nginx

我最近决定从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;
    ..........
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

/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;
    #}
}
Run Code Online (Sandbox Code Playgroud)

/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
    ..........................................
}
Run Code Online (Sandbox Code Playgroud)

Amn*_*non 79

include /etc/nginx/mime.types;location / {而不是在http {解决这个问题对我来说.

  • 另请注意,您应该在浏览器中完全刷新网站,例如使用ctrl + f5进行刷新,以避免获取包含错误标头的缓存文件. (8认同)
  • 还要注意,如果你从头开始配置 - 除了mime类型可能 - `include mime.types;`做它的工作,因为(至少在windows上,nginx 1.5.2)它只是相对于其他配置文件. (3认同)

use*_*620 30

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

location ~ \.css {
    add_header  Content-Type    text/css;
}
location ~ \.js {
    add_header  Content-Type    application/x-javascript;
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 这是一个nginx错误吗?这是我能让它发挥作用的唯一方法.顺便说一句,我使用的是Arch Linux,nginx 1.4.1-3. (2认同)

zam*_*uts 19

style.css实际上是由于你的"位置/"指令而通过fastcgi进行处理.因此fastcgi正在提供文件(nginx > fastcgi > filesystem),而不是直接提供文件系统(nginx > filesystem).

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

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

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;
}
Run Code Online (Sandbox Code Playgroud)

它应该是:

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;
}
Run Code Online (Sandbox Code Playgroud)

此更改确保只*.php从fastcgi请求文件.此时,NGINX将应用正确的MIME类型.如果您正在进行任何URL重写,则必须在location directive()之前处理此问题,location ~\.php$以便派生正确的扩展并正确路由到fastcgi.

请务必查看有关其他安全注意事项的文章try_files.鉴于安全隐患,我认为这是一个功能,而不是一个错误.


Jon*_*sco 10

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

你有这个:

include       /etc/nginx/mime.types;
default_type  application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

你要这个:

default_type  application/octet-stream;
include       /etc/nginx/mime.types;
Run Code Online (Sandbox Code Playgroud)

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

  • 这并没有解决使用nginx/1.6.0的Windows上的问题 (4认同)

Gil*_*rtS 7

  1. 在 nginx.conf 文件中,将 mime.types 添加到正文中,http如下所示:

    http {
        include /etc/nginx/mime.types;
        include /etc/nginx/conf.d/*.conf;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在转到终端并运行以下命令来重新加载服务器:

    sudo nginx -s reload
    
    Run Code Online (Sandbox Code Playgroud)
  3. 打开网络浏览器并进行硬重新加载:右键单击重新加载按钮并选择硬重新加载。在 Chrome 上你可以这样做Ctrl+ Shift+R


小智 6

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

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

location ~ \.css {
 add_header Content-Type text/css;
}
Run Code Online (Sandbox Code Playgroud)

我重新加载了 nginx 并在 error.log 中得到了以下内容:

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

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

我的情况是 VirtualBox 上的干净 xubuntu 14.04、nginx/1.9.2、/etc/hosts 中的一行127.51.1.1 mysite以及非常简单的 /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/;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)