搭建nginx服务器

2017-04-26  本文已影响42人  xiari1991

1.安装Homebrew

因为安装nginx+rtmp需要Homebrew。使用命令man brew查看是否装有Homebrew,若没有自行再百度安装即可,我的由于之前装过ReactNative的环境需要Homebrew,因此会提示一个帮助信息(证明已经安装有了),然后输入Q即可退出。

2.安装nginx

分别键入如下命令

brew tap homebrew/nginx
brew install nginx-full --with-rtmp-module
nginx
brew info nginx-full

在终端 Command+F 搜索nginx.conf

695203-45cec11a4ab0aa1f.png.jpeg

进入该路径,以文本编辑形式打开配置文件,直接在最后面插入以下代码

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }
    }
}
695203-e13e520e95b8edbe.png.jpeg

查看nginx版本号,在终端输入:

nginx -v

重启ngix,把版本号替换成你电脑的nginx的版本号

/usr/local/Cellar/nginx-full/1.10.1/bin/nginx -s reload
// 或者nginx -s reload

nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)错误

//解决办法

nginx -c /usr/local/etc/nginx/nginx.conf
增加对hls的支持

打开配置文件

/usr/local/etc/nginx/nginx.conf

在整个http之后添加 rtmp的配置内容,如下

193353-eaa3a56d8f92bd14.png
# HLS 直播流配置
    application hls {
        live on;
        hls on;
        hls_path /usr/local/var/www/hls;
        hls_fragment 5s;
    }

在http中添加 hls的配置,如下

193353-58b1b63d66ec5189.png
location /hls {
        #Serve HLS fragments
        types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
        }
        root /usr/local/var/www;
        add_header Cache-Control no-cache;
    }

重启nginx

$nginx -s reload

地址修改

推流地址:rtmp://192.168.1.58:1935/hls/test
拉流地址:http://192.168.1.58:8080/hls/test.m3u8

192.168.1.58 换成自己的nginx ip。
上一篇下一篇

猜你喜欢

热点阅读