Bryan
Bryan

Reputation: 55

Django static files 404 using nginx

I'm aware this has been asked a few others times, but none of those solutions seem to be working for me. This was my first time deploying a Django project, so for reference, I followed this tutorial: https://linuxhint.com/create_django_app_ubuntu/

For that reason, it is entirely possible that something flew over my head completely.

nginx config (I've tried alias instead of root, with and without trailing slash, made sure the static file aren't owned by root, etc.):

upstream django {
    server 127.0.0.1:8000;
}

server {
    listen 80;

    location /static/ {
        root /home/bryan/Projects/portfolio;
        try_files $uri =404;
    }

    location / {
        try_files $uri @send_to_django;
    }

    location @send_to_django {
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://django;
    }
}

settings.py:

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

Note:

{% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'home/css/index.css' %}">

My file structure:

Other than the static files, the site works flawlessly with this setup.

Upvotes: 0

Views: 402

Answers (1)

Alexey Popov
Alexey Popov

Reputation: 756

  1. Make sure your static files are in /home/bryan/Projects/portfolio/static
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )

Settings above may cause static files being collected into /home/bryan/Projects/portfolio/static/static dir

  1. Check static dir permissions

  2. Use "alias" instead of "root" in your nginx conf

Upvotes: 1

Related Questions