nginx搭建rtmp服务器(原有nginx上添加模块方式)及让nginx支持HLS

 nginx  nginx搭建rtmp服务器(原有nginx上添加模块方式)及让nginx支持HLS已关闭评论
1月 122021
 

转自:https://www.cnblogs.com/HintLee/p/9499429.html

参考:   https://www.jianshu.com/p/089b70f57bca

前言

最近接手了一个跟视频监控相关的项目,用了近年来越来越流行的 Web 服务器 nginx 加上 nginx-rtmp-module 搭建 rtmp 服务器。使用了阿里云的服务器,系统 Ubuntu 16.04 。

步骤

更新源并安装 nginx 。

sudo apt-get update
sudo apt-get install nginx

一、 支持RTMP

输入 nginx -V 查看 nginx 版本,可以看到当前版本号是 1.10.3,且可以看到编译选项。所以下一步要做的是下载相同版本的 nginx 源码,使用相同的编译选项并添加 nginx-rtmp-module,替换原来的 nginx 。
下载 nginx 1.10.3 的源码和 nginx-rtmp-module 的源码。

wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar zxvf nginx-1.10.3.tar.gz
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
cp -r nginx-rtmp-module nginx-1.10.3

在第 3 步中可以得知安装的 nginx 的编译选项,所以套用这些编译选项,在上一步已经把 nginx-rtmp-module 复制到 nginx 源码目录,所以在结尾添加 –add-module=./nginx-rtmp-module 。在 nginx-1.10.3 目录执行以下命令:

./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=./nginx-rtmp-module

上一步执行后可能会提示以下几个错误,需安装相关软件包,然后再次执行步骤 5 的命令。

./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: SSL modules require the OpenSSL library.
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
./configure: error: the HTTP image filter module requires the GD library.
./configure: error: the GeoIP module requires the GeoIP library.
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
sudo apt-get install libxml2 libxml2-dev libxslt-dev
sudo apt-get install libgd2-xpm-dev
sudo apt-get install libgeoip-dev

上一步执行完成后 make,等待 nginx 编译完成。编译过程可能会出现 error: macro “DATE” might prevent reproducible builds 错误,在 CFLAGS 中添加 -Wno-error=date-time 参数即可,也就是步骤5的命令改成

./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -Wno-error=date-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=./nginx-rtmp-module

编译完成后,在 objs 目录下会有 nginx 的可执行文件。首先停止 nginx 服务,替换掉 nginx 。

sudo service nginx stop
cd /usr/sbin
sudo mv nginx nginx.bak
sudo cp ~/nginx-1.10.3/objs/nginx ./

修改 /etc/nginx/nginx.conf,在结尾添加使其开启 nginx-rtmp-module 相关的功能。

rtmp {
        server {
                listen 1935;
                chunk_size 4000;
                application live {
                        live on;
                }
        }
}

执行 sudo service nginx restart 重启 nginx 服务,然后执行 netstat -a|grep 1935,可以看到 1935 端口处于 LISTEN 状态,即可向 nginx 推流。更多强大的功能可以查看 nginx-rtmp-module

 

二、 支持HLS

找到rtmp 修改下面这个地方

rtmp {
    server {
        listen 1935;
        application live {
            live on;
            record off;
        }
 
        # HLS
 
        # For HLS to work please create a directory in tmpfs (/tmp/hls here)
        # for the fragments. The directory contents is served via HTTP (see
        # http{} section in config)
        #
        # Incoming stream must be in H264/AAC. For iPhones use baseline H264
        # profile (see ffmpeg example).
        # This example creates RTMP stream from movie ready for HLS:
        #
        # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
        #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
        #    -f flv rtmp://localhost:1935/hls/movie
        #
        # If you need to transcode live stream use 'exec' feature.
        #
        application hls {
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
        }
 
        # MPEG-DASH is similar to HLS
 
        application dash {
            live on;
            dash on;
            dash_path /tmp/dash;
        }
    }
 } 
保存配置文件,重新加载nginx配置

nginx -s reload

2.进行推流测试(使用ffmpeg,安装见最后)

ffmpeg -loglevel verbose -re -i /Data/Movies/Demo.mov  -vcodec libx264 -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/hls/movie

然后你就可以在这个目录
/usr/local/var/www/hls
看到生成一个个ts的文件,还会生成一个movie.m3u8的文件

在Safari里输入地址查看视频(需要等movie.m3u8文件生成后),也可以用iPad或者iPhone上的Safari来访问(其他设备记得需要把localhost改为nginx的ip地址)
http://localhost:8080/hls/movie.m3u8

附:
ffmpeg安装:

Ubuntu

Ubuntu 中安装比较简单,直接使用 apt 安装即可

1
$ sudo apt install -y ffmpeg

CentOS

CentOS 中则比较麻烦,需要使用源码安装

下载并解压

1
2
3
$ wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2
$ tar -xjvf ffmpeg-4.1.tar.bz2
$ cd ffmpeg-4.1

编译

1
2
$ ./configure
nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.

可以使用 --disable-x86asm 参数略过该配置或者直接安装

1
$ sudo yum install -y yasm

也可以从官网下载源码安装

随后再次编译安装即可

1
2
$ ./configure
$ sudo make && sudo make install

debian/ubuntu/centos一键搭建openvpn服务器

 openvpn  debian/ubuntu/centos一键搭建openvpn服务器已关闭评论
12月 312019
 

想搭个个人用的openvpn服务器,网上看到大大分享了个一键搭建openvpn的脚本,适用于 Debian, Ubuntu 和 CentOS,测试了Debian和centos系统,果然好用!!!!(源码项目github地址)

安装步骤:

命令行运行: wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh

按照提示一步步往下走, 设置ip 、 tcp/udp选择、dns、端口、新增客户端ovpn文件名等。

如果后期需要删除原来的用户或新增新用户或删除openvpn等操作可以再次运行openvpn-install.sh脚本,根据提示进行对应操作即可,实在太方便了,给个大大的👍!!

ssh如何通过跳板机直接访问到后端服务器(Mac&Linux&Windows解决方案)

 linux, ssh  ssh如何通过跳板机直接访问到后端服务器(Mac&Linux&Windows解决方案)已关闭评论
9月 212018
 

记录下,转自:https://my.oschina.net/foreverich/blog/657075

前言

如果公司的服务器在外网,一般会设置一个跳板机,访问公司其他服务器都需要从跳板机做一个ssh跳转,外网的服务器基本都要通过证书登录的。于是我们面临一个情况,本机ssh->跳板机->目标机器。如果直接在跳板机上放置公私钥对,并将跳板机上的公钥放到目标机器上,这样可以直接登录。但这样会有一个问题,跳板机上的root权限的用户可以获取普通用户的公私钥对,就算对私钥设置了密码,但是从安全角度来看,这样还是失去了保障,失去了服务器的一部分安全性。

如何来解决这个问题呢,其实ssh协议本身是支持秘钥转发的,不需要在跳板机上放置公私钥。

Linux(Mac)下有如下两种方式:

方式一:

从linux客户端的ssh跳转时,执行命令

ssh [email protected]跳板机ip

然后在跳板机上跳转到目标机器

ssh [email protected]目标机器ip

跳板机ip和目标机器ip,username账户下已经在相应的 .ssh/authorized_keys 加入了公钥,配置是没有问题了,但是我们会遇到一个Pubkey Unauthorization的错误,因跳板机没有username的私钥。问题总是会有,解决方法也总是有,ssh是有转发密钥的功能,从本机跳转到跳板机时可以把私钥转发过去。

正确做法是,在本机linux客户端执行命令 ssh -A [email protected]跳板机ip

-A表示转发密钥,所以跳转到跳板机,密钥也转发了过来

接下来我们再在跳板机执行命令 ssh [email protected]目标机器ip

另外可以配置本机客户端的默认配置文件,修改为默认转发密钥:

修改ssh_config(不是sshd_config,一般在/etc或者/etc/ssh下):

把 #ForwardAgent no 改为 ForwardAgent Yes

方式二:

ssh [email protected]目标机器ip -p 22 -o ProxyCommand=’ssh -p 22 [email protected]跳板机ip -W %h:%p’

也可以修改配置文件 ~/.ssh/config , 若没有则创建:

Host tiaoban #任意名字,随便使用 HostName 192.168.1.1 #这个是跳板机的IP,支持域名 Port 22 #跳板机端口 User username_tiaoban #跳板机用户 Host nginx #同样,任意名字,随便起 HostName 192.168.1.2 #真正登陆的服务器,不支持域名必须IP地址 Port 22 #服务器的端口 User username #服务器的用户 ProxyCommand ssh [email protected] -W %h:%p



Host 10.10.0.* #可以用*通配符 Port 22 #服务器的端口 User username #服务器的用户 ProxyCommand ssh [email protected] -W %h:%p

配置好后, 直接 ssh nginx 就可以登录 192.168.1.2 这台跳板机后面的服务器。 也可以用 ssh [email protected] 来登录10.10.0.27, 10.10.10.33, …. 等机器。

windows SecureCRT密钥转发

windows下SecureCRT配置转发,需要做以下设置

输入图片说明