通过Nginx和Django服务206字节范围
作者:互联网
我有Nginx服务在Gunicorn上运行的静态Django文件.我正在尝试提供MP3文件,并使它们的头部为206,以便Apple接受它们进行播客.目前,音频文件位于我的静态目录中,并直接通过Nginx提供.这是我得到的答复:
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Wed, 30 Jan 2013 07:12:36 GMT
Content-Type: audio/mpeg
Content-Length: 22094968
Connection: keep-alive
Last-Modified: Wed, 30 Jan 2013 05:43:57 GMT
有人可以提供正确的方法来提供mp3文件,以便可以接受字节范围吗?
更新:这是我认为通过Django提供文件的代码
response = HttpResponse(file.read(), mimetype=mimetype)
response["Content-Disposition"]= "filename=%s" % os.path.split(s)[1]
response["Accept-Ranges"]="bytes"
response.status_code = 206
return response
解决方法:
如果只想在nginx中执行此操作,则在负责提供静态.mp3文件的location指令中添加以下指令:
# here you add response header "Content-Disposition"
# with value of "filename=" + name of file (in variable $request_uri),
# so for url example.com/static/audio/blahblah.mp3
# it will be /static/audio/blahblah.mp3
# ----
set $sent_http_content_disposition filename=$request_uri;
# or
add_header content_disposition filename=$request_uri;
# here you add header "Accept-Ranges"
set $sent_http_accept_ranges bytes;
# or
add_header accept_ranges bytes;
# tell nginx that final HTTP Status Code should be 206 not 200
return 206;
希望它能对您有所帮助.
标签:nginx,gunicorn,podcast,django,mp3 来源: https://codeday.me/bug/20191127/2070559.html