Ⅰ. はじめに
タイトルの通り「nginx でストリーミングサーバを作る方法」です。
Ⅱ. 環境
- CentOS 7.5
Ⅲ. やり方
1. nginxのビルドに必要なものをインストールする
yum -y install wget gcc pcre-devel openssl openssl-devel git libxslt-devel # apt -y install wget gcc libpcre3 libpcre3-dev openssl libssl-dev git libxslt1-dev zlib1g-dev build-essential
2. nginx-rtmp-module を clone する
git clone https://github.com/arut/nginx-rtmp-module.git
3. nginx をダウンロードする
2020/12/21 時点で 1.18.0 が stable です。
wget http://nginx.org/download/nginx-1.18.0.tar.gz tar zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0/
4. nginx をビルドする
./configure --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_xslt_module \ --add-module=/root/nginx-rtmp-module make make install groupadd nginx useradd -g nginx nginx usermod -s /bin/false nginx
5. init scriptを作成する
以下を全てコピペする
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
vim /etc/init.d/nginx chmod 755 /etc/init.d/nginx
- systemd
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
vim /lib/systemd/system/nginx.service
6. nginx の設定を変更する
$ vim /etc/nginx/nginx.conf http { server { listen 80; root /var/www/html; } } rtmp { server { listen 1935; access_log /var/log/nginx/rtmp_access.log; application live001 { live on; wait_video on; hls on; hls_path /var/www/html; hls_fragment 1s; hls_type live; # 配信開始時にPOSTされる # on_publish http://localhost/api/rtmp/on_publish; # 配信終了時にPOSTされる # on_publish_done http://localhost/api/rtmp/on_publish_done; } } }
7. nginx を起動する
service nginx start firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-port=1935/tcp --permanent firewall-cmd --reload
9. 閲覧用のWebページを作成する
index.html
<link href="https://vjs.zencdn.net/6.6.3/video-js.css" rel="stylesheet"> <div> <video id="player" width="960" height="540" class="video-js vjs-default-skin" controls> <source src="http://[your ip address]/[your stream key].m3u8" type="application/x-mpegURL"> </video> </div> <script src="https://vjs.zencdn.net/6.6.3/video.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.14.1/videojs-contrib-hls.js"></script> <script> let player = videojs('player') player.play() </script>
10. 実行結果
その他
Q. 視聴者数をリアルタイムで知りたい
A. rtmpで接続されたクライアントのみ統計情報が取得できます。
https://github.com/arut/nginx-rtmp-module/wiki/Getting-number-of-subscribers
その他方法
- Node-Media-Serverを利用してストリーミングサーバを作る方法
https://kagasu.hatenablog.com/entry/2021/01/21/131209 - https://github.com/winshining/nginx-http-flv-module
- https://github.com/ossrs/srs