Nginx not serving admin static files

djangonginx

Clarification: The following error is onlyfor the admin static files, i.e. it is specific to the static files corresponding to the Django admin. The rest of the static files are working perfectly.

Problem

Basically, I cannot access the admin static files using the ngix server.

It does work with the micro server of Django, and the collectstatic is doing its job, meaning it is putting the files on the expected place in the static folder.

The urls are correct but I cannot access the admin static files directly, but the others I can. So, for example:

  1. I am able to access this url (copying it in the browser):
    myserver.com:8080/static/css/base/base.css

  2. but I am not able to access this other url (copying it in the browser):
    myserver.com:8080/static/admin/css/admin.css


What have I tried?

It does work if I copy the admin/ directory structure into __other_admin_directory_name/__, and then I access
myserver.com:8080/static/__other_admin_directory_name__/css/admin.css

Moreover,

  1. I checked permissions and everything is fine.
  2. I tried to change ADMIN_MEDIA_PREFIX = '/static/admin/' to ADMIN_MEDIA_PREFIX = '/static/other_admin_directory_name/', it doesn't work.

Finally, and it seems to be an important clue:

I tried to copy the admin/ directory structure into __admin_and_then_any_suffix/__. Then I cannot access
myserver.com:8080/static/__admin_and_then_any_suffix/__/css/admin.css. So, if the name of the directory starts with admin (for example administration or admin2) then it doesn't work.


EDIT – added thanks to @sarnold observation:

The problem seems to be in the nginx configuration file /etc/nginx/sites-available/mysite

location /static/admin {
   alias /home/vl3/.virtualenvs/vl3/lib/python2.7/site-packages/django/contrib/admin/media/;
}

Best Answer

My suggestions:

  1. Use django 1.3+ (and ADMIN_MEDIA_PREFIX is deprecated now)

  2. Set both STATIC_URL and STATIC_ROOT in your settings.py

  3. Define just a single static entry in your nginx conf (with trailing slashes). No need for a second one that addresses static/admin/:

    location /static/  {
            alias /path/to/static/;
    }
    
  4. Use collectstatic which should collect admin -> static/admin. It will live under the same location as all the rest of your collected static media.

    python manage.py collectstatic