5

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/;
}
  • 2
    You forgot to include the relevant portions of your nginx configuration. – sarnold Jun 27 '12 at 23:27
  • This is a clean ubuntu 11.10 installation (it was working before in the same clean version) and I haven't touch the conf file but it seems you are right. There is something that seems to be the problem. I am editing my question... – toto_tico Jun 28 '12 at 0:03
  • @Carsten, maybe your link is wrong. [link](serveroverflow.com) does not exist – toto_tico Jun 28 '12 at 0:13
  • Is there a reason you need a specific location entry just for /static/admin? Wouldn't the standard '/static' cover it all? I just use a single: location /static/ { alias /path/to/static/; } – jdi Jun 28 '12 at 0:18
  • ok, i did the obvious here. I changed the path to the exact directory I have the files. It worked but I am worried, I don't think that is a line I should arbitrary change. This is a default configuration. I'll let you know exactly what is happening. Thanks! – toto_tico Jun 28 '12 at 0:25
17

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 am using django 1.4. I have the three of them: ADMIN_MEDIA_PREFIX, STATIC_URL, STATIC_ROOT. The collectstatic is working well as I explained. The only thing I am still curious about is where that ...python2.7/site-packages/...static/ came from... – toto_tico Jun 28 '12 at 0:36
  • 1
    Well you can get rid of ADMIN_MEDIA_PREFIX. Its no longer in use. That and the reference to the site-packages is all OLD django stuff. Its not used in 1.3+. admin will end up in the static root. – jdi Jun 28 '12 at 0:37
  • perfect! As I said this was a clean ubuntu 11.10 / nginx installation. Do you mind to do a small edition in your answer adding another step: delete the location /static/admin/ { ... } section – toto_tico Jun 28 '12 at 0:41
  • Why? Did your default nginx conf somehow come with that entry in the first place? I was listing sort of a ground up set of items that would apply to anyone. If it isn't there then you probably shouldn't have it. – jdi Jun 28 '12 at 0:51
  • well, that's exactly what i mean. This is a clean installation. I never touched that file before. I solved the problem deleting that section in the file. – toto_tico Jun 28 '12 at 1:10
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.