Stream music with byte range requests with Django + nginx

感情迁移 提交于 2019-12-03 13:54:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23301639/stream-music-with-byte-range-requests-with-django-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!