【发布时间】: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