【问题标题】:Not able to load static files in DJANGO app using AWS EC2无法使用 AWS EC2 在 DJANGO 应用程序中加载静态文件
【发布时间】:2020-10-10 10:40:43
【问题描述】:

我使用 gunicorn 和 NGINX 服务器在 AWS EC2 上成功部署了 Django 应用程序。但是,即使在 /etc/nginx/sites-available/ 中配置了 django.conf 文件后,静态文件也没有被加载到模板中。

django.conf 文件:

server{
        listen 80;
        server_name my_server_name;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/learning-aws/app.sock;
        }

        location /static/ {
                autoindex on;
                alias /home/ubuntu/learning-aws/myprofile/core/static/;
        }
}

gunicorn.conf 文件:

[program:gunicorn]
directory=/home/ubuntu/learning-aws/myprofile
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/learning-aws/app.sock myprofile.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log

[group:guni]
programs:gunicorn

settings.py 文件:

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

使用的模板标签(例如base.html):

{% static 'plugins/bootstrap/bootstrap.min.css' %}

【问题讨论】:

  • 你跑collectstatic了吗?
  • 它在命令行中不起作用:No Django settings specified. Unknown command: 'collectstatic' Type 'django-admin help' for usage.
  • @NamanMonga 它是$ py manage.py collectstatic
  • 130 static files copied to '/home/ubuntu/learning-aws/core/static', 88 unmodified. 重新加载supervisor和nginx后,问题没有解决
  • 您的 nginx 配置中有 myprofile,但 collectstatic 报告的路径不包含该目录。您的 STATIC_ROOT 或您的 nginx 配置不正确

标签: django amazon-web-services nginx gunicorn django-staticfiles


【解决方案1】:

这是一个很简单的错误,静态文件的地址错误:

    # changed
    # alias /home/ubuntu/learning-aws/myprofile/core/static/;
    # to
    alias /home/ubuntu/learning-aws/core/static/;

并且因为系统的优先级改变了root('/')和static('/static')的位置顺序。 到:

    server{
        listen 80;
        server_name my_server_name;

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/learning-aws/app.sock;
        }

        location /static/ {
                autoindex on;
                alias /home/ubuntu/learning-aws/myprofile/core/static/;
        }
    }

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2021-09-08
    • 2020-04-16
    • 2020-12-17
    • 2020-11-12
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多