'Django unable to serve static files from s3 bucket
I created a few models and pushed my project to a development server on an AWS EC2 instance. I used Django storages and followed the docs to configure my settings.py
. when I run manage.py collectstatic
the static files are pushed to the bucket successfully, but when I access the Django admin the CSS is missing.
Can anyone help me fix this?
My bucket permissions
{
"Version": "2012-10-17",
"Id": "Policy1650117254896",
"Statement": [
{
"Sid": "Stmt1650117250899",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::agristore.001/*"
}
] }
My settings file
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media/'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_ACCESS_KEY_ID = '***'
AWS_SECRET_ACCESS_KEY = '***'
AWS_STORAGE_BUCKET_NAME = '****'
AWS_S3_FILE_OVERWRITE = False
AWS_QUERYSTRING_AUTH = False
My Nginx settings
server {
server_name <my ip>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/venv/src;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Solution 1:[1]
You need to set STATIC_URL
to the full URL (including domain) of your S3 bucket's root.
What you're doing here causes templates to generate relative static asset URLS which likely end up going to your EC2 instance where Nginx receives them and tries finding the referenced files in /home/ubuntu/venv/src
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | esmail |