代码之家  ›  专栏  ›  技术社区  ›  teewuane

如何配置nginx为Safari提供mp4视频?

  •  2
  • teewuane  · 技术社区  · 8 年前

    以下是我的html片段:

    <video autoplay="" muted="" loop="" preload="auto" poster="http://my.ip.add.ress/static/my_video_image.jpg">
        <source src="http://my.ip.add.ress/static/my_video.mp4" type="video/mp4" />
        <source src="http://my.ip.add.ress/static/my_video.webm" type="video/webm" />
    </video>
    

    我遇到的问题是,当safari请求视频时,nginx应该返回一个 206 partial content 回应。但是,它返回一个 200 OK 与整个视频(我认为整个文件返回)。但safari并没有要求完整的视频,而是要求使用 range

    所以这会导致视频无法在Safari中播放。现在,我的当前设置可以在Chrome和Firefox中运行。

    我用nginx来提供视频内容。我想避免使用第三方服务器,因为这是一个小项目:)。

    请求中的标头。有没有办法告诉nginx注意那个标题?

    这是我的nginx配置 /etc/nginx/sites-available/myproject :

    server {
        listen 80;
        server_name my.ip.add.ress;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            alias /home/website/my_python_virtual_env/my_project/static_folder_containing_mp4_videos/;
        }
    
        location / {
            # gunicorn to django
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }
    

    Request
    Range: bytes=0-1
    Accept: */*
    Connection: Keep-Alive
    Accept-Encoding: identity
    DNT: 1
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
    Referer: http://my.ip.add.ress/
    X-Playback-Session-Id: 97A1EC54-85A3-42A1-8EA2-8657D03058B6
    

    答案如下:

    Response
    Content-Type: video/mp4
    Date: Thu, 13 Sep 2018 17:48:40 GMT
    Last-Modified: Wed, 12 Sep 2018 22:20:39 GMT
    Server: nginx/1.14.0 (Ubuntu)
    Content-Length: 10732143
    Connection: keep-alive
    X-Frame-Options: SAMEORIGIN
    

    在有视频工作的站点上,请求/响应如下所示:

    Request
    GET /big_buck_bunny.mp4 HTTP/1.1
    Range: bytes=0-1
    Host: clips.vorwaerts-gmbh.de
    Accept: */*
    Connection: keep-alive
    Accept-Encoding: identity
    Accept-Language: en-us
    DNT: 1
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
    X-Playback-Session-Id: C2EAAF63-1230-44A9-9A16-6332C1EDEBF0
    
    
    Response
    HTTP/1.1 206 Partial Content
    ETag: "5416d8-47f21fa7d3300"
    Content-Type: video/mp4
    Date: Thu, 13 Sep 2018 17:28:47 GMT
    Last-Modified: Tue, 09 Feb 2010 02:50:20 GMT
    Server: cloudflare
    Content-Length: 2
    Expires: Fri, 13 Sep 2019 17:28:47 GMT
    Connection: keep-alive
    Content-Range: bytes 0-1/5510872
    Set-Cookie: __cfduid=d2776dbf7a6baaa1b2f2572d600deda141536859727; expires=Fri, 13-Sep-19 17:28:47 GMT; path=/; domain=.vorwaerts-gmbh.de; HttpOnly
    Vary: Accept-Encoding
    Cache-Control: public, max-age=31536000
    CF-RAY: 459c5511b243a064-SLC
    CF-Cache-Status: HIT
    
    1 回复  |  直到 8 年前
        1
  •  7
  •   teewuane    7 年前

    /media/ Range 请求。它将为他们提供适当的铬足以工作,虽然!;)

    解决办法很简单。为添加位置条目 /媒体/ 给我的nginx.conf 网站的文件。

    server {
        listen 80;
        server_name my.ip.add.ress;
    
        location = /favicon.ico { access_log off; log_not_found off; }
    
        # still have to have this location entry to serve the static files...
        location /static/ {
            alias /home/website/my_python_virtual_env/my_project/static_folder_containing_static_files/;
        }
    
        # Added this location entry to nginx, my videos were NOT in the static folders, they were in the media folders. I feel dumb but hopefully this will help someone else out there!
        location /media/ {
            alias /home/website/my_python_virtual_env/my_project/media_folder_containing_mp4_videos/;
        }
    
    
        location / {
            # gunicorn to django
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }