如何将Nginx+OBS生成的本地m3u8推至公网?求免费方案
Hey there! Let's work through your problem with free, practical solutions tailored for teaching purposes. First, let's break down the feasibility of your two ideas, then walk through step-by-step implementations.
方案可行性分析
你的方案1:Nginx写入/删除到MySQL或云存储
- MySQL存储视频文件:Not feasible. MySQL is designed for structured data, not large binary files like
.tssegments. Storing video here will kill performance, waste space, and is totally overkill for your use case. - 云存储替代MySQL:Absolutely feasible. Free cloud storage options (with limited free tiers) can work, but you'll need tools to sync Nginx's local files to the cloud automatically. This is better for long-term hosting but has more setup steps than your second idea.
你的方案2:开放存储文件夹的公网访问
- Totally feasible (and perfect for teaching):This is the simplest, cheapest way to get your HLS stream online for demo purposes. You just need a way to expose your local Nginx server to the public internet—no need to move files anywhere.
免费实现步骤
优先推荐:内网穿透 + 本地Nginx开放文件夹(最简单,适合教学演示)
This approach lets you share your local HLS stream directly without moving files. We'll use free tools to tunnel your local Nginx server to the public internet.
Step 1: Verify your local Nginx setup
First, make sure your Nginx is correctly serving the HLS files. Update your Nginx config (usually at /etc/nginx/sites-available/default) to enable directory listing and proper MIME types for HLS:
server { listen 80; server_name localhost; # Point this to your HLS file directory location /hls { root /var/www; autoindex on; # Lets you browse files (useful for testing) types { application/vnd.apple.mpegurl m3u8; video/MP2T ts; } add_header Access-Control-Allow-Origin *; # Fixes CORS issues for players } }
Test the config with sudo nginx -t, then reload Nginx: sudo systemctl reload nginx. Confirm you can access http://your-local-ip/hls/name.m3u8 locally.
Step 2: Use Ngrok (free, zero-config tunneling)
Ngrok is perfect for quick demos—no server required, just a free account.
- Sign up for a free Ngrok account, download the client for your OS.
- Unzip the client, then run this command (assuming your Nginx uses port 80):
./ngrok http 80 - You'll see a public URL like
https://abc123.ngrok-free.appin the output. Now anyone can access your stream athttps://abc123.ngrok-free.app/hls/name.m3u8.- Note: Free Ngrok URLs change every time you restart the client, but this is totally fine for short teaching demos.
Alternative: FRP (open-source, more stable if you have a free cloud server)
If you have access to a free cloud server (like Aliyun's student ECS free tier), FRP is a self-hosted tunneling tool that gives you a fixed public address.
- On your cloud server (FRP server):
- Download the FRP server binary, unzip it, and create a
frps.inifile:[common] bind_port = 7000 # Port for FRP client to connect vhost_http_port = 8080 # Port for public HTTP access - Start the server:
./frps -c ./frps.ini
- Download the FRP server binary, unzip it, and create a
- On your local machine (FRP client):
- Download the FRP client binary, unzip it, and create a
frpc.inifile:[common] server_addr = your-cloud-server-public-ip server_port = 7000 [hls-stream] type = http local_port = 80 # Your local Nginx port custom_domains = your-cloud-server-ip # Or a free domain if you have one - Start the client:
./frpc -c ./frpc.ini
- Download the FRP client binary, unzip it, and create a
- Now the stream is accessible at
http://your-cloud-server-ip:8080/hls/name.m3u8.
备选方案:挂载免费云存储到本地(适合 longer-term hosting)
If you need a persistent public stream (not just a demo), you can sync your local HLS files to a free cloud storage service and serve them directly from the cloud.
Step 1: Choose a free cloud storage provider
- Backblaze B2: 10GB free forever, supports S3-compatible APIs.
- Aliyun OSS: 10GB free for new users (12 months).
- Tencent Cloud COS: 5GB free forever.
Step 2: Mount the cloud storage to your local Nginx directory
We'll use s3fs to mount the cloud bucket as a local folder—so Nginx writes files directly to the cloud automatically.
- Install s3fs (Ubuntu/Debian example):
sudo apt update && sudo apt install s3fs - Create a credentials file for your cloud storage:
echo "YOUR_ACCESS_KEY:YOUR_SECRET_KEY" > ~/.passwd-s3fs chmod 600 ~/.passwd-s3fs - Mount the bucket to your HLS directory (replace with your provider's endpoint):
s3fs your-bucket-name /var/www/hls -o passwd_file=~/.passwd-s3fs -o url=https://s3.us-west-004.backblazeb2.com - Add the mount to
/etc/fstabto make it permanent:s3fs#your-bucket-name /var/www/hls fuse _netdev,passwd_file=/home/your-username/.passwd-s3fs,url=https://s3.us-west-004.backblazeb2.com 0 0
Step 3: Enable static website hosting on the cloud storage
Most free cloud storage services let you enable static website hosting. For example:
- Backblaze B2: Enable "Static Website" for your bucket, use the provided default domain (or bind a free domain if you have one).
- Aliyun OSS: Enable "Static Website Hosting" and ensure files are set to public access.
Now your stream is accessible at http://your-bucket-domain/name.m3u8.
总结推荐
- For teaching demos:Go with the 内网穿透 + 本地Nginx approach (Ngrok is easiest). It's free, takes 5 minutes to set up, and lets you show the entire workflow (OBS → Nginx → public stream) without moving files.
- For longer-term hosting:Use the cloud storage mount approach, leveraging free tiers from providers like Backblaze B2.
内容的提问来源于stack exchange,提问作者Samoussa Binou Mymusicisinme

