我有一个非常标准的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).
此外,在我的浏览器中直接try http://localhost/media/base/users/8/picture.jpg
也不起作用:
从现在起,我不知道该怎么办才能解决这个问题.如果遗漏了什么,请提出来,我会更新帖子的.