我有以下设置:
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/project.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
internal;
root /Users/halsdunes/Desktop/project/backend; # your Django project's media files - amend as required
}
location /static {
root /Users/halsdunes/Desktop/project/backend; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /Users/halsdunes/Desktop/project/backend/backend/uwsgi_params; # the uwsgi_params file you installed
}
}
from django.conf.urls import url
from directmessages.views import message_image_serve
urlpatterns = [
url(r'^message_images/user_(?P<user1_id>\d+)/user_(?P<user2_id>\d+)/(?P<uuid>[0-9a-f-]+).jpg$', message_image_serve, name='message_image_service'),
]
使用根url.py
,如下所示:
url(r'^media/', include('directmessages.urls')),
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def message_image_serve(request, user1_id, user2_id, uuid):
user_id = request.user.id
if not user_id in (int(user1_id), int(user2_id)):
raise Http404
path = 'message_images/user_%s/user_%s/%s.jpg' % (user1_id, user2_id, uuid)
response = serve(request, path, document_root=settings.MEDIA_ROOT)
response['X-Accel-Redirect'] = safe_join(settings.MEDIA_ROOT, path)
return response
我的假设是,由于配置,nginx完全绕过Django视图并提供图像。但是,这不是我想要的。实际上,我宁愿它:
我怎样才能实现这一目标?
答案 0 :(得分:3)
您需要为该视图提供一个不同的网址,即nginx的媒体服务不。然后Django将使用X-Accel-Redirect标头进行响应,这将导致nginx在响应中提供文件。