即使在collectstatic之后,Django Static也无法在生产环境中工作

标签 django docker nginx

我遇到了一个问题,但无法解决。寻求一些建议!

好吧,本地一切正常,我已经运行了收集静态 我在 django 应用程序文件夹中名为“static”的文件夹下收集了静态文件:

app
|
|- app
|- app n°2
|- static

所以在本地运行,没有一个错误,并且管理员有CSS 当我插入生产时,我的应用程序运行,代理(nginx)运行......

但是,当 connectinf 到 example.com/admin/时,css 未运行

我的代理日志给了我这个......

"GET /app/static/admin/css/responsive.css HTTP/1.1" 404 179 "https://example.com/admin/login/?next=/admin/"

出现404错误?

这是我的setting.py 的样子

STATIC_URL = '/app/static/'
STATIC_ROOT = '/app/static'

欢迎任何帮助,我想了解错误! 非常感谢

干杯


更新@ivan

据我了解,正在发生的事情:

collectstatic -> generate a folder « staticfiles » in django’s app folder -> Dockerfile copy all django’s app -> docker-compose take folder /app/staticfiles and copy in volume static_volume -> docker-compose nginx copy folder /app/staticfiles in her container -> web request : Django's setting try to ask static file on path /static/ -> nginx with alias redirect the /static/ to /app/staticfiles/

这是我当前的配置:

Docker-compose(我删除了无用的信息,例如certbot和数据库)

version: "3"

services:
  app:
    build:
      context: .
    volumes:
      - ./app:/app
      - static_volume:/app/staticfiles
    environment:
      - DJANGO_SECRET_KEY=${DJANGO_SECRET_KEY}
      - DJANGO_ALLOWED_HOSTS=${DOMAIN}

  proxy:
    build:
      context: ./docker/proxy
    restart: always
    depends_on:
      - app
    ports:
      - 80:80
      - 443:443
    volumes:
      - proxy-dhparams:/vol/proxy
      - static_volume:/app/staticfiles
    environment:
      - DOMAIN=${DOMAIN}


volumes:
  proxy-dhparams:
  static_volume:

ssl.conf (nginx)

server {
    listen      443 ssl;
    ...

    location /static/ {
        autoindex on;
        alias /app/staticfiles/;
    }

设置.py


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

目录

project
|
|app
|-app
|-app n°2
|-staticfiles
|
|Dockerfile
|
|docker-compose.deploy.yml
|
|docker
|-proxy
|--nginx
|---ssl.conf
|-Dockerfile

Django 的 Dockerfile

FROM python:3.10-alpine

ENV PYTHONBUFFERED 1

COPY ./requirements.txt /requirements.txt
COPY ./app /app
WORKDIR /app

ENV PATH="/py/bin:$PATH"

我的代理日志中的两种错误:

"GET /static/css/style.css HTTP/1.1" 404

"/staticfiles/admin/js/nav_sidebar.js" failed (2: No such file or directory)

我在运行时验证了 staticfiles 文件夹是否位于 nginx 容器中(是的,它在那里) nginx container

最佳答案

您的设置中必须有这些设置 settings.py

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

另外,将以下配置放入应用程序的URL文件中 urls.py

urlpatterns = [
    # admin page url
    path('admin/', admin.site.urls),
    .
    .
    .
    .
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

如果你想更专业地服务你的文件,最好使用白噪声 http://whitenoise.evans.io/en/latest/

关于即使在collectstatic之后,Django Static也无法在生产环境中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75144975/

相关文章:

python - centos无法安装mysqlclient

node.js - 使用docker时Winston日志文件在哪里

python - 将 Python 应用程序连接到 Kubernetes 集群上的 Redis

php - NGINX/PHP - 无法通过 php 脚本提供 jpeg

Python 无法识别 Django 安装。 (导入错误 : No module named django)

python - 检查函数是否有装饰器

django 管理员登录突然要求 csrf token

docker - 在Docker镜像的kubernetes安装卷中找不到现有的二进制可执行文件

nginx - [NGINX]无法使 proxy_pass 和 robots.txt 工作

nginx - 为什么是proxy_set_header Host $host;不再在 nginx 1.8.0 中工作?