Nginx 不提供 Django 管理静态文件

Nginx not serving up Django admin static files

一个例子404 URL:

http://ip.address/static/admin/css/base.css

我不确定我做错了什么。这是相关文件:

settings.py

STATICFILES_DIRS = [
  '/home/username/sitename/sitenameenv/lib/python3.5/site-packages/django/contrib/admin/static',
]
STATIC_URL = '/static/'
STATIC_ROOT = '/home/username/sitename/website/staticroot/'

Nginx 配置:

server {
    listen 80;
    server_name http://ip.address/;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/sitename/website/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/sitename/sitename.sock;
    }
}

常规静态文件已正确提供,并且在我添加 STATIC_ROOT 和使用 collectstatic 之前它们也是这样做的,我认为这不是必需的。不过,我的管理员的静态文件收到 404s。

更新:

运行 collectstatic 会将您的所有文件收集到 STATIC_ROOT 设置指定的目录中。

因此,您需要配置您的 Nginx 服务器以在 /satic/ url:

为您的 STATIC_ROOT 文件夹提供服务
location /static/ {
    # using `alias` instead of `root`
    # vvv
    alias   /home/username/sitename/website/staticroot/;
            #                                  ^^^
            # the full path to the STATIC_ROOT directory
}

旧版本(不是很有用):

为什么会这样?

这里有一件重要的事情需要注意:Django 不提供静态文件。我相信你知道。

因此,当您的浏览器从 - http://ip.address/static/admin/css/base.css 请求管理 css 文件时,此请求会被您的 Nginx 服务器拦截,因为您已将 /static/ 路径映射到此目录- /home/username/sitename/website/static/.

现在,接下来发生的是,Nginx 将尝试在 /home/username/sitename/website/static/ 中找到 /admin/css/base.css 文件。但是那里没有任何 admin 目录,因此这个请求最终成为 404.


如何解决这个问题?

您可以将 /static/admin/ url 映射到 admin 目录实际所在的目录。示例:

# put this before `location /static/` conf

location /static/admin/ {
    root   /home/username/sitename/sitenameenv/lib/python3.5/site-packages/django/contrib/admin/;
}

我也遇到了同样的问题。我在 Centos 7.6 上的 nginx 服务器无法访问路径 /home/user/app/mysyte/static/ 中的静态文件夹。 /var/log/nginx/error.log 同样的错误

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

为了解决和理解这个问题:=*

  1. 运行命令getenforce
  2. 如果强制执行 - cat /var/log/audit/audit.log | grep nginx

对我来说,有错误的字符串看起来像

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)

审核消息的副本 ID 1558033633.723:201

  1. 运行命令grep yours_audit_id /var/log/audit/audit.log | audit2why

为我输出

[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

所以你可以在这里看到答案setsebool -P httpd_read_user_content 1当你运行这个命令你看到你的静态内容