我有一个非常标准的Django应用程序,有一个Vuejs前端.

我有不同的环境(preprod/dev),在这些环境中我具有文件上传/下载功能.

对于文件,一切正常,因为它们是通过附件(Content-Disposition: attachment)中的标准API视图返回的.然而,当涉及到图片时,比如头像,就有一个问题.

在发展(DEBUG=True)中,我有这样的 idea :

from django.conf import settings
from django.conf.urls.static import static
from django.urls import include, path

from backend.applications.base.views.authentication_views import LoginAPIView, LogoutAPIView

urlpatterns = [
    path("api/login", LoginAPIView.as_view()),
    path("api/logout", LogoutAPIView.as_view()),
    path("api/base/", include("backend.applications.base.urls")),
    path("api/contact/", include("backend.applications.contact.urls")),
    path("api/helpdesk/", include("backend.applications.helpdesk.urls")),
    path("api/inventory/", include("backend.applications.inventory.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  # For serving media files when DEBUG=True

并且图像被正确地提供(在开发模式中没有ngix,只有前端和后端开发服务器django's runserver).

然而,我的preprop是由一个为我构建的vuejs前端提供服务的nginx容器和一个包含我的Django(DEBUG=False)应用程序的后端容器组成的(这次它使用gunicorn运行,如下所示:gunicorn backend.wsgi:application --bind 0.0.0.0:8000 --access-logfile="-").

在try 提供镜像之前,我有这样的nginx配置:

http {
    client_max_body_size 5M;

    upstream backend_api {
        server backend:8000;
        # 'backend' is the name of the backend service in my docker-compose config
    }

    server {
        listen 80;

        include /etc/nginx/mime.types;

        root /usr/share/nginx/html;
        index index.html;

        location = /favicon.ico {
            access_log off;
            log_not_found off;
        }

        location /api {
            proxy_pass http://backend_api;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}

然后我认为/media个请求也应该传递到后端,于是我改变了

location /api

vt.进入,进入

location ~ ^/(api|media)/ {

我的/api个URL仍被正确处理,但/media个URL由404:

trying to load profile pictures of my user(s) in a kanban view (trying to load profile pictures of my user(s) in a kanban view).

此外,在我的浏览器中直接try http://localhost/media/base/users/8/picture.jpg也不起作用:

enter image description here enter image description here

从现在起,我不知道该怎么办才能解决这个问题.如果遗漏了什么,请提出来,我会更新帖子的.

推荐答案

Django不使用runserver提供静态和媒体文件,因此您需要使用Whitenoise.见http://whitenoise.evans.io/en/stable/ 然而,Whitenoise不适合提供用户上传的媒体文件.见http://whitenoise.evans.io/en/stable/django.html#serving-media-files

(可选)跳过白噪声,通过Nginx托管静态/媒体文件.)

您真的不应该使用py manage.py runserver托管您的服务器.这是不安全的.见Why not use "runserver" for production at Django?https://docs.djangoproject.com/en/dev/ref/django-admin/#runserver

Use something like Gunicorn instead.

https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/gunicorn/

(或者女服务员,windows 的替代品)

https://pypi.org/project/django-waitress/

要使用nginx托管静态/媒体文件,请将以下内容粘贴到您的nginx conf中:

    location /media  {
        alias /PATH/TO/DIRECTORY; #Absolute path.
    }

在settings.py中,将媒体根目录设置为相同的目录.

Django相关问答推荐

获取PyCharm中继承方法的未解析属性引用

Django Prefetch上的多重过滤

Django为不同应用程序发出信号

Django和静态文件

Django REST序列化程序TO_REATION失败

Django-admin 显示decorator 按多列排序

Django通用列表视图与多查询搜索

如何根据状态计算表中数据的数量?

Docker 中的 Django检测到更改...,正在重新加载错误

在Django Rest Framework中按模型属性排序时如何避免重新计算?

将 **kwargs 传递给 Django 表单

用于测试文件下载的 Django 单元测试

ManyRelatedManager 不可迭代

在 Django 的 ORM 中访问存储过程的最佳方法是什么

可以在 github 页面上托管 django 站点吗?

has_object_permission 和 has_permission 有什么区别?

我应该如何在我的模型中使用 DurationField?

Django 发送邮箱

django 我们可以在预取相关模型上 Select 一个字段吗?

AttributeError:ManyRelatedManager对象没有add属性?