8

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/;
}
toto_tico
  • 181
  • 1
  • 1
  • 6
  • 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... –  Jun 28 '12 at 00:03
  • @Carsten, maybe your link is wrong. [link](serveroverflow.com) does not exist –  Jun 28 '12 at 00: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 00: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! –  Jun 28 '12 at 00:25
  • ok, the directory that is created automatically is just wrong, instead of ...python2.7/site-packages/...static/, it should be ...python2.7/site-packages/...static/media/ –  Jun 28 '12 at 00:31
  • You shouldn't be needing to reference into your python site-packages location. Are you collecting your static files there? What version of django are you using? – jdi Jun 28 '12 at 00:32

2 Answers2

20

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

jdi
  • 361
  • 2
  • 5
  • 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... –  Jun 28 '12 at 00: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 00: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 –  Jun 28 '12 at 00: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 00: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. –  Jun 28 '12 at 01:10
  • I did all of these and its still not working. However for the nginx config I have to use `root /usr/local/bin/apps/my-app-dir/;` since the `alias` config you list does not work. Using nginx 1.15.1, django 2.2.1. I can actually navigate to the CSS page too (view-source:http://myserver.com:1234/static/admin/css/base.css) and see the file contents, but it does not work on the actual admin site, the CSS does not display. – user5359531 Mar 21 '19 at 21:51
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

N. Turshiev
  • 131
  • 1