使用Django + nginx串流播放带有字节范围请求的音乐 [英] Stream music with byte range requests with Django + nginx

查看:334
本文介绍了使用Django + nginx串流播放带有字节范围请求的音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django + nginx构建一个音乐播放器应用程序,我需要一个支持字节范围请求的后端。



Django正确验证媒体文件,但django dev服务器不支持范围请求(206部分响应)。在使用此配置后,Nginx直接提供字节范围请求,我验证了响应头具有内容范围。然而,我无法将请求从django转发到nginx,以提供内容。



我尝试使用 X-Accel-Redirect 在django视图中,但是响应头仍然没有内容的范围,如果这个文件已经被nginx直接提供了。




  • Django dev服务器 - 验证完成但没有字节范围支持(响应200) li>
  • Nginx - 没有身份验证,字节范围请求支持(响应206)

  • Django视图+ X-Accel-Redirect + nginx - 验证完成,但没有字节范围支持(响应200)



所以我试图找到一种使用Django进行身份验证的方法,并为nginx或另一个静态文件服务器。

解决方案

  response = HttpResponse(content_type = mimetype,status = 6)
response ['Content-Disposition'] =attachment; filename =%s%\
(fileModel.FileName)
response ['Accept-Ranges'] ='bytes'
response ['X-Accel-Redirect'] =设置。 MEDIA_URL +'/'+ fileModel.FileData.MD5
response ['X-Accel-Buffering'] ='no'
返回响应

这是为我而设计的,现在用nginx与django + streaming进行身份验证。


I am building a music player application with Django + nginx for which I need a backend which supports byte range requests.

Django is authenticating the media file correctly but django dev server does not support range requests (206 partial response). Nginx directly serves byte range requests after using this configuration, I verified that the response header has content range. However I am unable to forward the request from django to nginx, to serve the content.

I tried using X-Accel-Redirect in a django view but still the response header doesn't have content range the way it would have been if the file had been directly served by nginx.

  • Django dev server - Authentication done but no byte range support (response 200)
  • Nginx - No authentication, byte range request support (response 206)
  • Django view + X-Accel-Redirect + nginx - Authentication done but no byte range support (response 200)

So I am trying to find a way to authenticate using Django and provide support for byte range requests with nginx or another static file server.

解决方案

response = HttpResponse(content_type = mimetype, status=206)
response['Content-Disposition'] = "attachment; filename=%s" % \
                                         (fileModel.FileName)
response['Accept-Ranges'] = 'bytes'
response['X-Accel-Redirect'] = settings.MEDIA_URL + '/' + fileModel.FileData.MD5
response['X-Accel-Buffering'] = 'no'
return response

This worked out for me. Now authentication with django + streaming with nginx is accomplished.

这篇关于使用Django + nginx串流播放带有字节范围请求的音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆