djangonginx

Django static files are saved recursively creating bloat via collectstatic


When running collectstatic on my production system, it seems like a bunch of files is stored recursively e.g. (real example):

'production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/production/css/lib/nestable/nestable.css'

This causes bloat every time I update my static files (which is necessary when I update any javascript for the front-end).

My settings.py file has the following:

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

STATICFILES_DIRS = ([
    os.path.join(BASE_DIR, "static"),

])

I also have + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in my main urls.py

In my Nginx available-sites config file I have the following lines:

location /static/ {
        alias /home/ubuntu/myapp/static/production/;
        expires modified +1w;
    }

To circumvent this issue, I now have to run collectstatic -c and delete all my static files with every update which isn't ideal.

Where am I going wrong?


Solution

  • This is caused, because you're storing your collected static files inside your static sources directory.

    Static files in Django can come from any Django application, as applications can define their own static files. To simplify your server configuration, all static files has to be collected from your project and from all used applications to one directory from which your web server can serve them.

    STATIC_ROOT is the location of this central directory into which all static files will be collected.

    STATIC_URL is reflecting your STATIC_ROOT from the web server endpoint. In other words, on this URL all files from STATIC_URL will be served by your server.

    STATICFILES_DIRS together with static directories from each application are the directories from which all files should be collected (copied over) to the STATIC_ROOT directory.

    In your example, you are storing your STATIC_ROOT inside a path defined in STATICFILES_DIRS, so after each run of the collectstatic django will copy over everything from STATICFILES_DIRS into your STATIC_ROOT, including the contents of the STATIC_ROOT it copied over previously.

    To fix your issue, isolate those 2 locations in your file system and adjust your Django settings accordingly.