我有 Nginx 为我的静态 Django 文件提供服务,该文件在 Gunicorn 上运行。我正在尝试提供 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;
关于django - 通过 Nginx、Django 提供 206 字节范围服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598565/