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

标签 django nginx mp3 gunicorn podcast

我有 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/

相关文章:

docker - 如何通过 Dockerfile 更改 nginx 的默认主页,然后通过运行容器启动它

JAVA,通过URL播放mp3

c# - 是否有一个 C# 库允许您查询 mp3 持续时间和比特率

django - 我如何在 Django 模板中将整数的 unix 时间戳转换为人类可读的格式?

python - 修改 django 管理模板

Traefik Docker Swarm 模式真实 ip 背后的 Nginx

node.js - 为以下应用程序结构编写 docker 的正确方法是什么?

java - 使用Java读取远程MP3文件的ID3标签

python - 用户使用 DRF token 身份验证登录后,如何在 HTTP 响应中返回用户 ID?

python - 如何从 Django 迁移中调用基于实例的函数并访问实例变量