nginx not serving admin static files?

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/;
}

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


I have the same problem. My nginx server on Centos 7.6 can't access to static folder in path /home/user/app/mysyte/static/. In /var/log/nginx/error.log same error

open() "/home/user/app/mysyte/static/*.css" failed (13: Permission denied)

For solving and understanding this problem :=*

  1. run command getenforce
  2. if enforcing - cat /var/log/audit/audit.log | grep nginx

for me string with errrors looks like

type=AVC msg=audit(1558033633.723:201): avc:  denied  { read } for  pid=7758 comm="nginx" name="responsive.css" dev="dm-0" ino=17312394 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0
type=SYSCALL msg=audit(1558033633.723:201): arch=c000003e syscall=2 success=no exit=-13 a0=564f710dd55d a1=800 a2=0 a3=68632f656d6f682f items=0 ppid=7757 pid=7758 auid=4294967295 uid=998 gid=996 euid=998 suid=998 fsuid=998 egid=996 sgid=996 fsgid=996 tty=(none) ses=4294967295 comm="nginx" exe="/usr/sbin/nginx" subj=system_u:system_r:httpd_t:s0 key=(null)

copy id of audit msg 1558033633.723:201

  1. run command grep yours_audit_id /var/log/audit/audit.log | audit2why

output for me

[root@uwsgi ~]# grep 1558034479.384:221 /var/log/audit/audit.log | audit2why
type=AVC msg=audit(1558034479.384:221): avc:  denied  { read } for  pid=7758 comm="nginx" name="responsive.css" dev="dm-0" ino=17312394 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0

        Was caused by:
        The boolean httpd_read_user_content was set incorrectly.
        Description:
        Allow httpd to read user content

        Allow access by executing:
        # setsebool -P httpd_read_user_content 1

So as you can see answer here setsebool -P httpd_read_user_content 1 when you run this command you see your static content