通过 Nginx、Django 提供 206 字节范围服务

2024-02-13

我让 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然后在你的location负责提供静态 .mp3 文件的指令添加这些指令:

# 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 Nginx、Django 提供 206 字节范围服务 的相关文章

随机推荐