2015-04-03 27 views
4

我的nginx服務器提供了一個index.html文件,它加載了一個css文件和一些js文件。加載CSS文件(NO 404),但樣式不出現。Nginx發現css,但沒有加載到index.html

文件夾:

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

聲音和圖像通過js文件加載完美地工作。

的/ etc/nginx的/啓用的站點 - /默認/:

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它給我的索引頁,但沒有任何造型和開發工具不會顯示任何錯誤。

感謝您的幫助!

編輯

整個事情是一個泊塢窗容器..現在我每次瀏覽到本地主機:8080 nginx的死亡。錯誤日誌是某種不可訪問..

回答

4

我有一個類似的問題,並通過確保在我的nginx.conf的http塊加載正確mime.types映射解決它:

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

我有同樣的問題,相同的解決方案。請注意,如果要保持類型明確,或者不想依賴mime.types文件,只需將該文件中的類型收集到.conf中的「types {}」塊中即可。 – stolli 2015-08-14 22:31:52

+0

如果它在'http'塊中不起作用,請嘗試在'server'塊中加載MIME類型。 – 2017-03-22 23:54:32

+0

它只是通過放入http塊來爲我工作。我還必須記住做一個Ctrl +重新加載才能生效。謝謝。 – yngwietiger 2017-08-08 12:03:36

0

嘗試添加@charset「 UTF-8" ;

在css文件的頂部 它爲我工作。希望它適合你