无法使用 AWS EC2 在 DJANGO 应用程序中加载静态文件
Not able to load static files in DJANGO app using AWS EC2
我使用 gunicorn 和 NGINX 服务器在 AWS EC2 上成功部署了 Django 应用程序。但是即使在 django.conf 中配置 /etc/nginx/sites-available/ 文件后,静态文件也不会加载到模板中。
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' %}
很简单的错误,静态文件地址错误:
# changed
# alias /home/ubuntu/learning-aws/myprofile/core/static/;
# to
alias /home/ubuntu/learning-aws/core/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/;
}
}
我使用 gunicorn 和 NGINX 服务器在 AWS EC2 上成功部署了 Django 应用程序。但是即使在 django.conf 中配置 /etc/nginx/sites-available/ 文件后,静态文件也不会加载到模板中。
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' %}
很简单的错误,静态文件地址错误:
# changed
# alias /home/ubuntu/learning-aws/myprofile/core/static/;
# to
alias /home/ubuntu/learning-aws/core/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/;
}
}