加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

通过Nginx和Django服务206字节范围

发布时间:2020-12-13 20:53:08 所属栏目:Nginx 来源:网络整理
导读:我有Nginx服务在Gunicorn上运行的静态Django文件.我正在尝试提供MP3文件,并使它们的头部为206,以便Apple接受它们进行播客.目前,音频文件位于我的静态目录中,并直接通过Nginx提供.这是我得到的答复: HTTP/1.1 200 OK Server: nginx/1.2.1 Date: Wed,30 Jan 2

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

希望它能对您有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读