You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过NGINX反向代理实现MP4视频流式传输?

Fixing Video Streaming Issues with Nginx Reverse Proxy for Your Django-Based Video Platform

Hey there, let’s walk through this scenario and get your video streaming working smoothly through your front-end Nginx reverse proxy. You’ve already nailed the backend setup: Ubuntu 16, Django + uWSGI + Nginx, and direct access to http://www.backend/ serves your 1.mp4 with proper 206 Partial Content responses (2MB chunks, incremental playback—perfect). The problem hits when routing through your front-end reverse proxy, so let’s fix that.

Key Configuration Fixes for Nginx Reverse Proxy

The main culprit here is usually missing header forwarding or buffering settings that break partial content streaming. Here’s what to adjust on your front-end Nginx:

  1. Forward Range Request Headers
    Your proxy needs to pass the client’s range request headers to the backend, and relay the backend’s partial content response back. Add these directives to your front-end server block:

    location / {
        proxy_pass http://www.backend/;
        # Standard proxy headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # Critical for partial content streaming
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;
        proxy_ignore_headers X-Accel-Redirect;
        # Disable buffering to stream chunks immediately
        proxy_buffering off;
    }
    

    The proxy_buffering off line is non-negotiable here—if buffering is enabled, Nginx will wait for the entire video to load from the backend before sending it to the client, killing the incremental playback.

  2. Verify Backend Nginx Media Serving
    Double-check your backend Nginx is configured to handle MP4 streaming correctly. Your media file location block should include the mp4 directive, which enables Nginx’s built-in MP4 streaming support:

    location /media/ {
        alias /path/to/your/django/media/;
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
        # Enable partial content for MP4s
        mp4;
    }
    

    This directive tells Nginx to parse the MP4’s metadata so it can respond to range requests properly.

  3. Test the Proxy with Curl
    To confirm the fix works, run this curl command from a client machine targeting your front-end server:

    curl -I -H "Range: bytes=0-2097151" http://your-frontend-server/media/1.mp4
    

    You should get a 206 Partial Content response, along with a Content-Range header—just like when accessing the backend directly.

Additional Checks

  • Firewall Access: Make sure your front-end server can reach the backend server over HTTP/HTTPS (no firewall rules blocking traffic between the two).
  • Avoid Serving Media via Django: In production, never serve media files directly through Django/uWSGI—your backend Nginx should handle this entirely, as uWSGI isn’t optimized for static file streaming.
  • Nginx Version: Ubuntu 16.04 ships with Nginx 1.10.x, which fully supports the mp4 directive and range requests—just confirm it’s installed with nginx -v.

If you’re seeing specific errors (like 404s, 200 OK instead of 206, or playback stalls), share snippets from your front-end and backend Nginx logs, and we can dig into the specifics.

内容的提问来源于stack exchange,提问作者alex_devPY

火山引擎 最新活动