nginx部署Django项目时css文件中的背景图片路径找不到
有关nginx静态文件路径配置参考:
https://editor.csdn.net/md/?articleId=119815675
例如我的静态文件结构为:
项目名
- collect_static # 生产部署时通过 python3 manage.py collectstatic 命令集成所有静态文件的目录,与STATIC_ROOT路径相同
- css
- images
- a.jpg
- js
- comment_statc # 存放公共静态文件,与STATICFILES_DIRS路径相同
- css
- images
- a.jpg
- js
- app1
- static # 各个app单独的静态文件
- app2
- static
此时nginx.conf配置文件中静态路径为:
location /collect_static/ {
alias /xxx/xxx/xxx/collect_static/;
}
如果css文件中图片地址为
background: url(/collect_static/images/a.jpg);
由于此时加载的路径为http:127.0.0.1/collect_static/images/a.jpg,Django无法利用STATIC_URL来让浏览器直接访问静态文件,因此浏览器响应时会报404 NOT FOUND。
解决办法:
css文件中图片地址改为:
background: url(../images/a.jpg);