bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
使用Homebrew安装带RTMP模块的Nginx
brew tap denji/nginx
brew install nginx-full --with-rtmp-module
安装完成后,查看Nginx信息
brew info nginx-full
编辑Nginx配置文件
sudo nano /usr/local/etc/nginx/nginx.conf
在配置文件中添加RTMP配置(在http块之外添加):
rtmp {
server {
listen 1935;
chunk_size 4096;
allow publish 127.0.0.1;
deny publish all;
application live {
live on;
record off;
# 推流鉴权(可选)
# on_publish http://localhost/auth;
}
}
}
保存后检查配置是否正确
sudo nginx -t
启动Nginx
sudo nginx
检查Nginx是否运行
ps aux | grep nginx
如果需要停止或重启Nginx:
sudo nginx -s stop # 停止
sudo nginx -s reload # 重新加载配置
使用OBS或其他推流软件进行测试
rtmp://localhost/live
使用VLC或其他播放器观看
rtmp://localhost/live/test
在RTMP配置中添加:
application live {
live on;
record off;
hls on;
hls_path /tmp/hls;
hls_fragment 3;
hls_playlist_length 60;
}
然后在http块中添加:
server {
listen 8080;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
sudo nano /usr/local/etc/nginx/nginx.conf
找到http
部分的server
块,确保有以下内容:
server {
listen 8080;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/html;
}
location / {
root html;
index index.html index.htm;
}
}
端口被占用:
sudo lsof -i :1935
sudo kill -9 [PID]
权限问题:
sudo chown -R $(whoami) /usr/local/etc/nginx
查看日志:
tail -f /usr/local/var/log/nginx/error.log
更新配置后不生效:
sudo nginx -s reload
这样你就成功在Mac上搭建了一个RTMP直播服务器,可以进行直播推流和观看了!