You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置Nginx以同时推送HLS/RTMP流媒体

Fixing Nginx RTMP to HLS Conversion for Your Custom Player

Hey there! I know how frustrating it can be when you're this close to getting HLS working but hit roadblocks. Let's take your existing RTMP setup and expand it to support HLS properly—no Flash required.

First, Let's Complete Your RTMP Config

It looks like your config cut off mid-application block. Here's a full, working example that keeps your existing multi-platform pushes while adding HLS output:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        # Your existing live stream application
        application live {
            live on;
            record off;

            # Keep your existing platform push configs here!
            push rtmp://facebook-live-api.facebook.com:443/rtmp/your-facebook-stream-key;
            push rtmp://a.rtmp.youtube.com/live2/your-youtube-stream-key;
            push rtmp://periscope.pscp.tv:80/x/your-periscope-stream-key;

            # Add HLS support right here
            hls on;
            hls_path /var/www/hls;  # Ensure this directory exists on your server
            hls_fragment 5s;        # Smaller fragments = faster stream startup
            hls_playlist_length 30s;# Retain the last 30 seconds of stream in the playlist
            hls_type live;           # Mark this as a live stream (not on-demand video)
        }
    }
}

# Critical: Add HTTP block to serve HLS files to your player
http {
    server {
        listen 80;

        location /hls {
            root /var/www;
            add_header Access-Control-Allow-Origin *; # Allow cross-origin playback for custom players
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
        }
    }
}

Key Checks to Avoid Headaches

  • Directory Permissions: The hls_path (/var/www/hls in the example) must be writable by your Nginx process. Run sudo chown -R www-data:www-data /var/www/hls (adjust the user/group if your Nginx uses a different one).
  • Nginx Modules: Confirm your Nginx was compiled with both --with-rtmp_module (which you already have) and --with-http_hls_module. Verify this with nginx -V—look for these flags in the output.
  • Config Validation: Always test your config before restarting: sudo nginx -t. If it says "test is successful", reload Nginx with sudo nginx -s reload.

Testing the Setup

  1. Push your stream to rtmp://your-server-ip/live/your-stream-key using OBS or your preferred encoder.
  2. Test the HLS playlist:
    • Use a native HTML5 video tag: <video src="http://your-server-ip/hls/your-stream-key.m3u8" controls autoplay></video>
    • Or use a robust player like Video.js (you can host its script locally to avoid external dependencies)

Troubleshooting Common Issues

  • Can't access the .m3u8 file: Check if port 80 is open in your firewall (e.g., sudo ufw allow 80 for UFW). Also, confirm the HTTP block correctly points to your HLS directory.
  • Player loads but no video: Check the Nginx error log (/var/log/nginx/error.log) for permission errors or missing files. If you see "permission denied", fix the directory ownership as mentioned earlier.
  • Stream is laggy: Adjust hls_fragment and hls_playlist_length—smaller fragments reduce lag but use more server resources. 3-5 seconds is a good balance for live streams.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:50:12