mac nginx 服务器搭建网站,MAC上使用nginx搭建直播服务器-Go语言中文社区

第一步:安装nginx-full

安装

brew tap homebrew/nginx

Error: homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.

报错原因是homebrew/nginx被弃用了,git路径变了。改用

brew tap denji/nginx

brew install nginx-full --with-rtmp-module

如果之前安装过nginx,那执行install的时候会报错,提示需要执行

brew unlink nginx

在此执行install就会成功。

参考:https://blog.csdn.net/fengsh998/article/details/79942775

查看安装信息

执行 brew info nginx-full,可以看到

版本:denji/nginx/nginx-full: stable 1.15.8, HEAD

安装所在位置:/usr/local/Cellar/nginx-full/1.15.8配置文件所在位置:/usr/local/etc/nginx/nginx.conf

服务器根目录所在位置:/usr/local/var/www

服务启动方式:brew services start denji/nginx/nginx-full or nginx

启动nginx

/usr/local/Cellar/nginx-full/1.15.8/bin/nginx

扩展:

brew tap [options] user/repo [URL]

tap是采用,利用的意思,即采用规定的仓库。当未指定URL时,使用HTTPS采用来自GitHub的规定的仓库。由于有太多的taps被托管在GitHub上,这个命令是 brew tap user/repo https://github.com/user/homebrew-repo 简写。

第二部:配置rtmp

编辑配置文件

编辑/usr/local/etc/nginx/nginx.conf,添加rtmp配置

...

http {

...

}

rtmp {

server {

listen1935;

application myapp {

live on;

record off;

}}

}

重新加载nginx

/usr/local/Cellar/nginx-full/1.15.8/bin/nginx -s reload

检查1935端口是否被监听

lsof -i tcp:1935

第三步:rtmp推流直播

安装ffmpeg

brew install ffmpeg

使用ffmpeg进行推流

ffmpeg -re -i /Users/lipengfei/Downloads/1549850041799339.mp4 -vcodec copy -f flv rtmp://localhost:1935/myapp/room

URL中的myapp对应nginx.conf中配置application,后面的room是随意加的。

未报错说明推流成功。

如果报错说连接失败

5bd9f6d0369c79545ad0f5d2cf7f29ff.png

则需要检查1935端口是否被监听,nginx.conf配置文件是否正常,nginx是否已reload。

验证rtmp直播

下载安装VLC

https://www.videolan.org/vlc/

点击菜单【File】->【Open Network】,在弹出框中Network下的URL中输入rtmp://localhost:1935/myapp/room,点击Open即可。

ef33295775a248bfc27b6121fcf4ca46.png

点击播放按钮,查是否能够播放。

a7baa4315e0be8f34d065022cf0e8098.png

第四步:HLS直播

编辑 /usr/local/etc/nginx/nginx.conf ,在http服务中配置中添加hls配置

...

http {

...

location/{

root html;

index index.html index.htm;

}

# HLS配置开始,这个配置为了‘客户端’能够以http协议获取HLS的拉流

location/hls {

# Serve HLS fragments

types {

application/vnd.apple.mpegurl m3u8;

video/mp2t 5s;

}

root html;

add_header Cache-Control no-cache;

}

# HLS配置结束

}

在rtmp服务中添加HLS支持

rtmp {

server {

listen1935;

application myapp {

live on;

record off;

}

# 增加对HLS支持

application hls {

live on;

hls on;

hls_path/usr/local/var/www/hls;

hls_fragment 5s;

}

}

}

保存配置后,重新加载nginx

/usr/local/Cellar/nginx-full/1.15.8/bin/nginx -s reload

进行推流

ffmpeg -re -i /Users/lipengfei/Downloads/1549850041799339.mp4 -vcode copy -f flv rtmp://localhost:1935/hls/movie

url中当myapp需要换成hls,movie可以随意替换。

推流后在/usr/local/var/www/hls/目录下可以看到生成的m3u8和ts文件

测试拉流

使用rtmp播放

使用hls播放

参考:

https://blog.csdn.net/fengsh998/article/details/79942775

https://www.cnblogs.com/jys509/p/5649066.html

http://www.cnblogs.com/jys509/p/5653720.html