my Django deployment with ec2, nginx and gunicorn went well beside that my staticfiles are not loading, browser show a 404 error. And this is because nginx looks in a completely different place than statics. At this point I have tried a lot of configurations and nothing does the trick. I am wondering if a pair of fresh eyes can spot a mistake here that I do not.
/sites-enabled/django.conf:
server {
server_name site.net www.site.net;
location /static/ {
autoindex on;
alias /home/ubuntu/saas/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/saas/app.sock;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/site.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/site.net/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site.net www.site.net;
return 404; # managed by Certbot
}
and my django settings.py look like this and collectstatics works fine.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I have deleted the default file but it stills seems that nginx looks for the staticfiles in the wrong place according to this error.log:
2020/05/01 19:57:45 [error] 3502#3502: *6 open() "/usr/share/nginx/html/static/css/custom.css" failed (2: No such file or directory), client: 86.221.78.105, server: site.net, request: "GET /static/cs$
I have rebooted the server, reload nginx, nothing seems to work out. Would someone has an idea of what I could try at that point?
There's usually a default.conf file that gets installed with nginx when you install with yum or apt. It will either be in /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/default.conf. Delete that file and reload nginx with sudo systemctl reload nginx, and you should be good to go absent there being another configuration file on your server that you don't know about.
Here is what I did to resolve the problem. Getting rid of the default index.html in /usr/share/nginx/html/ and running the conf script as:
server {
server_name site.net www.site.net;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/site.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/site.net/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location /static/ {
autoindex on;
alias /home/ubuntu/saas/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/saas/app.sock;
}
}
server {
if ($host = www.site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = site.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name site.net www.site.net;
return 404; # managed by Certbot
}
Although this seems to be a workaround it makes the trick