使用nginx+django部署项目的时候,为了提高加载速度,我将css,js,image等静态资源在nginx中配置了路径
本地测试没有问题,代码提交线上就出问题了,出现了这么个情况:
css文件能加载但是没有生效
经过查找资料,大概从以下两个方面入手检查:
1、考虑css文件引用是否正确
2、考虑服务器nginx配置静态文件是否正确
打开浏览器console,出现这么一句话
Resource interpreted as Stylesheet but transferred with MIME type text/plain
先尝试修改css文件引用,检查必要的参数是否都有,路径是否正确
<link rel="stylesheet" type='text/css' href="/static/css/toutiao.css">
依然没有用,后来发现是nginx配置的问题,我的配置文件中没有以下两句,加入之后就ok了
include mime.types; default_type application/octet-stream;
最后修改nginx配置如下:
http { # css文件正常加载 include mime.types; default_type application/octet-stream; server { listen 8000; server_name localhost; location ^~ /static/ { # 静态文件路径,不包含static root staticPath; } location / { proxy_pass http://127.0.0.1:8090; # 转发路径 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
参考