Nginx finds css but doesn't load it into index

2019-06-27 09:29发布

my nginx server serves an index.html file, which loads a css file and some js files. the css file is loaded (NO 404), but the style doesn't appear.

folders :

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

sound and images loaded by the js files work perfectly.

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

if i go to localhost:8080 it gives me the index page but without any styling and the dev tools doesn't show me any errors.

thanks for your help!

EDIT:

The whole thing is in a docker container.. Now every time i browse to localhost:8080 nginx dies. error log is somehow not accessible..

2条回答
做个烂人
2楼-- · 2019-06-27 10:01

try adding @charset "UTF-8";

at the top of css file it worked for me. hope it works for you

查看更多
叼着烟拽天下
3楼-- · 2019-06-27 10:05

I had a similar problem and resolved it by making sure the http block in my nginx.conf was loading the proper mime.types mappings:

http { 
    include /etc/nginx/mime.types;
}
查看更多
登录 后发表回答