Django nginx 查看静态文件而不是静态文件

Django nginx looking at static not staticfiles

我无法解决这个问题,nginx 坚持要查看 "static" 文件夹,即使我认为我已经将 "staticfiles" 作为位置。然而,日志不断地在文件路径中显示工作 "static"。这是 nginx.conf:

server {
    listen 80;
    server_name 10.88.58.95;

    location = /favicon.ico { access_log off; log_not_found off; }
    root /srv/pcc_django/;
    location /staticfiles/ {
    }

location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, "static")

...

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

...

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

这是访问日志显示的内容:

10.184.52.12 - - [18/Oct/2019:12:58:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:12:58:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET / HTTP/1.1" 200 5916 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET / HTTP/1.1" 200 5916 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"

请问如何让 nginx 查看正确的文件夹,它应该查看:

/srv/pcc_django/staticfiles/

目前正在查看:

/srv/pcc_django/static/ 

在同一文件夹中。如果我交换名称,它就可以工作,所以我确定我的 settings.ph 文件已针对 DEBUG = False 正确配置,这是服务器上的当前设置。

更新:我刚刚将 nginx.conf 文件编辑为以下内容:

location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles {
    root /srv/pcc_django;
    }

并从错误日志中得到:

2019/10/21 13:22:00 [error] 5888#0: *1 open() "/srv/pcc_django/staticfiles/static/admin/css/responsive.css" failed (2: No such file or directory), client: 10.184.53.51, server: 10.88.58.95, request: "GET /static/admin/css/responsive.css HTTP/1.1", host: "10.88.58.95", referrer: "http://10.88.58.95/admin/"

我不明白 /static/ 是从哪里来的,nginx 还是 django,我该怎么做才能验证问题到底出在哪里?

Django 在设置 STATIC_ROOTSTATIC_URL 中提供的静态文件夹中查找。第一个应该是“/staticfiles/”。第二个应该匹配 nginx 中的 location 以映射到服务器目录。

location /static {
    expires 1y;
    add_header Cache-Control "public";
    alias /some/path/in/your/server/;
}

Django 设置设置正确,问题是 nginx.conf 添加绝对路径的别名到我的 css 解决了我的问题。

server {
    listen 80;
    server_name 10.88.58.95;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
             alias /srv/pcc_django/staticfiles/;
    }

    location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/run/gunicorn.sock;
    }
}