【问题标题】:Nginx finds css but doesn't load it into index.htmlNginx 找到 css 但没有将其加载到 index.html
【发布时间】:2015-04-03 12:20:08
【问题描述】:

我的 nginx 服务器提供一个 index.html 文件,该文件加载一个 css 文件和一些 js 文件。 css 文件已加载(NO 404),但样式未出现。

文件夹:

/src
    /assets
        /images
            ...
        /sounds
            ...
    /css
        /style.css
    /js
        ...
    index.html

js 文件加载的声音和图像完美运行。

/etc/nginx/sites-enabled/default/:

server {
  listen 8080;
  server_name jumpnrun jumpnrun.vm;
  access_log /var/log/nginx/jumpnrun.access.log;
  error_log /var/log/nginx/jumpnrun.error.log;

  location / {
    try_files $uri $uri/index.html;
  }

  location ~ ^/(assets/|css/|js/|index.html) {
    root /src;
    index index.html;
    access_log off;
  }

  location /socket.io {
    add_header Access-Control-Allow-Origin *;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://node-server;
  }

}

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Jump n' Run</title>
        <link href="css/style.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <canvas id="game" width='1000' height='500'></canvas>
        <button class="sound"></button>
    </body>
    <!-- JS -->
</html>

如果我转到 localhost:8080,它会为我提供索引页面,但没有任何样式,并且开发工具不会显示任何错误。

感谢您的帮助!

编辑

整个事情都在一个 docker 容器中。现在每次我浏览到 localhost:8080 nginx 都会死掉。错误日志不知何故无法访问..

【问题讨论】:

    标签: html css nginx stylesheet


    【解决方案1】:

    我遇到了类似的问题,并通过确保我的 nginx.conf 中的 http 块正在加载正确的 mime.types 映射来解决它:

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

    【讨论】:

    • 我遇到了同样的问题,同样的解决方案。请注意,如果您想保持类型明确,或者不想依赖 mime.types 文件,您可以从该文件中获取类型到 .conf 中的“types {}”块中。
    • 如果在 http 块中不起作用,请尝试在 server 块中加载 mime 类型。
    • 只需放入 http 块,它就对我有用。我还必须记得执行 Ctrl+Reload 才能使其生效。谢谢。
    • 这对我有用!谢谢。很惊讶这不是默认设置。
    【解决方案2】:

    尝试添加@charset "UTF-8";

    在css文件的顶部 它对我有用。希望对你有用

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 2021-06-27
      • 2021-04-04
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多