Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream music with byte range requests with Django + nginx

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.

like image 777
pratsJ Avatar asked Jan 10 '23 17:01

pratsJ


1 Answers

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.

like image 99
pratsJ Avatar answered Jan 18 '23 23:01

pratsJ