【问题标题】:Django project deployment: cannot load static filesDjango项目部署:无法加载静态文件
【发布时间】:2017-02-01 10:14:05
【问题描述】:

我正在部署一个 Django 项目,使用:

  • 虚拟环境
  • nginx
  • 独角兽

按照本教程进行操作:https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps


我的配置

django 设置

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATIC_URL = '/static/'

/etc/nginx/sites-available/esmart2

GNU nano 2.0.9                 File: /etc/nginx/sites-available/esmart2

server {
    server_name 192.168.30.17;
    access_log off;

    location /static/ {
        alias /new_esmart/esmart2/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

跑步

(esmart_env) [root@eprssrv09 esmart2]# /new_esmart/esmart_env/bin/gunicorn --bind 192.168.30.17:8001 esmart2.wsgi:application

我的 Django 项目正在运行,但是:

Not Found: /static/admin/css/login.css

有什么建议吗?


更新 1 我认为避免了nginx设置:

删除 'sites-enabled' 文件夹中的任何引用,django 项目运行,但有关静态文件的问题仍然存在。

更新 2 如果在 Django 设置中 debug=True 我得到了

Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/login.css
Not Found: /static/admin/css/base.css

【问题讨论】:

  • 您阅读过静态文档吗?您的 STATIC* 设置是什么?你跑collectstatic了吗?
  • @DanielRoseman 我已经用 django 静态设置编辑了我的问题,我已经运行了 collectstatic 并且所有静态文件都在 /new_esmart/esmart2/static/

标签: django nginx static gunicorn


【解决方案1】:

在我看来问题有两个方面:

  1. 看起来您的 nginx 配置可能有错误。 new_esmart/ 真的在你的根目录中吗/ 还是像 /home/sam/new_esmart/ 这样的地方?如果它不在您的根目录中,请确保提供完整路径。

  2. 看起来您的STATIC_ROOT 将指向path/to/new_esmart/static/ 而不是/new_esmart/esmart2/static/。请记住,STATIC_ROOT 是您在运行 collectstatic 时将静态文件复制到的位置而不是您在最初创建静态文件时个人放置静态文件的位置。

    李>

所以您可能需要做的是确认您确实希望将静态文件收集到/actual/path/to/new_esmart/static/,然后在您的 nginx 配置中使用相同的路径。

此外,正如 cmets 中所讨论的,您可以尝试将 listen 8001; 添加到您的 nginx 配置中:

server {
    listen      8001;
    server_name 192.168.30.17;
    access_log off;
    ...

【讨论】:

  • new_esmart/ 在我的根目录中,如果我打印 STATIC_ROOT 我得到 /new_esmart/esmart2/static/ ...所以我想问题不在这里
  • 抱歉,我假设new_esmart 是项目,esmrt2 是应用程序,但emart2 是项目?另外,我假设您的INSTALLED_APPS 中有'django.contrib.admin'?
  • @Sam 也可以尝试将listen 8001 添加到您的 nginx 配置中 server_name 192.168.30.17; 之前的一行
  • @Sam 你尝试添加listen 行吗?
  • @Sam 好的,我不确定,但我认为这些设置可能会被避免因为这条线丢失了。至少值得一试。
【解决方案2】:

似乎我解决了,但我认为这不是更好的解决方案。我的意思是,根据项目要求,这是一个很好的解决方案:我的允许使用此解决方案。


安装包

pip install whitenoise

我在 wsgi.py 上添加了:

from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

这样 nginx 站点 conf 就被忽略了(但我认为他们已经忽略了!)

Django WhiteNoise documentation

通过几行配置,WhiteNoise 允许您的网络应用程序 提供自己的静态文件,使其成为一个独立的单元,可以 无需依赖 nginx、Amazon S3 或任何其他工具即可部署在任何地方 对外服务。 (特别适用于 Heroku、OpenShift 和其他 PaaS 提供商。)

它旨在与高流量网站的 CDN 完美配合,因此您 不必为了从简单中受益而牺牲性能。

WhiteNoise 适用于任何 WSGI 兼容的应用程序,但有一些特殊的 Django 的自动配置功能。

WhiteNoise 会为您提供最佳实践,例如:

提供压缩内容(gzip 和 Brotli 格式,处理 Accept-Encoding 和 Vary headers 正确)设置远期缓存 内容的标题不会改变担心服务静态 使用 Python 的文件效率极低?仍然认为你应该是 使用亚马逊 S3?看看不常见的问题。

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 2015-08-22
    • 2014-06-18
    • 2014-07-12
    • 2018-03-19
    • 2020-12-17
    • 2022-01-12
    • 2017-12-06
    • 2011-11-26
    相关资源
    最近更新 更多