skip to Main Content

My website serves the static files from the Django app instead of the Nginx staticfiles folder but I can’t figure out why.

My Django app static is on the (Linode) server at:

/var/www/DjangoApp/myapp/static/myapp

The server static folder is at:

/var/www/html/staticfiles/myapp

Here’s my settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/html/staticfiles/'

Here’s my urls.py:

from django.contrib import admin
from django.urls import path,include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('myapp.urls')),
]

urlpatterns += staticfiles_urlpatterns()

Here’s my config file at /etc/nginx/sites-enabled/default:

server {
    server_name helpmesaythis.com;

    location / {
        proxy_pass http://172.105.76.53:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /static/ {
        root /var/www/html/staticfiles;
    }

    listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/helpmesaythis.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/helpmesaythis.com/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 = helpmesaythis.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name helpmesaythis.com;
    return 404; # managed by Certbot
}

I have restarted/reloaded Nginx and I have been using collectstatic without success.

I have also tried the following changes to the default file:

location /static {
        root /var/www/html/staticfiles;
    }

location /static/ {
        alias /var/www/html/staticfiles;
    }

location /static {
        alias /var/www/html/staticfiles;
    }

I have also tried putting the location /static/ section above the location / section, just in case.

When I go to the staticfiles folder at /var/www/html/staticfiles/myapp there are a few files in their from when I first put the project on the server a few months ago, however none of the static files since then have gone in.

I only realised that this was happening recently when I saw that the audio files on my site were served as HTTP/1.1 instead of HTTP/2.

EDIT: I tried to open the following static file:

https://helpmesaythis.com/static/myapp/flag-spain.webp

And I got a 404 error. The Nginx error log states the following:

2023/03/27 14:59:10 [error] 18795#18795: *297 open() "/var/www/html/staticfilesmyapp/flag-spain.webp" failed (2: No such file or directory), client: 181.237.132.46, server: helpmesaythis.com, request: "GET /static/myapp/flag-spain.webp HTTP/2.0", host: "helpmesaythis.com"

It makes sense as there is nothing in the server static folder as python3 manage.py collectstatic only collects the static files in the Django app directory.

2

Answers


  1. Chosen as BEST ANSWER

    The static files problems were due to this in settings.py:

    STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    

    Which I changed to this:

    STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
    

    This was a pretty silly oversight on my part. I think the problem was that when I made the change originally, and then did collectstatic on my local machine, it threw up an error because the path in STATIC_ROOT = '/var/www/html/staticfiles/' obviously isn't on my local machine. However, after pushing through the change and doing collectstatic on my server, everything worked as it should.


  2. When I go to the staticfiles folder at /var/www/html/staticfiles/myapp
    there are a few files in their from when I first put the project on
    the server a few months ago, however none of the static files since
    then have gone in.

    Given this, my guess is that collectstatic fails to find the new files you added to the app. Why and how it got them the first time I don’t know, as I wasn’t there (maybe you copied the directory manually?), but it obviously isn’t catching the new ones.

    Fastest solution is to copy them manually into the /var/www/html/staticfiles/ directory. This should fix it without further intervention.

    However, if that’s tedious and you want collectstatic to do it for you, then read ahead.


    Set up the STATICFILES_DIRS in settings.py, then re-run collectstatic.

    STATICFILES_DIRS = [
        "/var/www/DjangoApp/myapp/static/myapp",
        # more directories ...
    ]
    

    then:

    python manage.py collectstatic
    

    Directories named static under apps that are correctly installed, should automatically be picked up by collectstatic, without having to be added to STATICFILES_DIRS. My guess as to why this isn’t happening is possibly the weird reduntant structure of the folder /myapp/static/myapp (myapp twice). If the above STATICFILES_DIRS fix works, you might want to delete it, and refractor your tree correctly, like:

    /var/www/DjangoApp/myapp/static/css
    /var/www/DjangoApp/myapp/static/js
    /var/www/DjangoApp/myapp/static/* # etc...
    

    Here are a few other things you can check:

    • Make sure your new static files are located in a directory that’s included in STATICFILES_DIRS. By default, this setting includes the static subdirectory in each app, but you can add additional directories if needed.

    • Make sure your new static files have the correct naming and directory structure. By default, Django’s collectstatic command expects static files to be located in a static subdirectory of each app, and to be named in a certain way (e.g. app_name/css/style.css). If your files are not named or structured correctly, collectstatic may not find them.

    • Make sure your app is included in INSTALLED_APPS. If your app is not included in this setting, Django will not search its static files when running collectstatic.

    If you’ve checked all of these settings and your static files are still not being collected, try running collectstatic with the --traceback option to see if there are any error messages that might explain the problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search