nginx rtmp http_flv直播推流

安装配置nginx

yum install epel-release -y
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
yum install ffmpeg ffmpeg-devel -y
yum install gcc -y
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y
yum install wget -y
mkdir -p /opt/sort/
cd /opt/soft/
wget -c http://nginx.org/download/nginx-1.25.0.tar.gz
tar -xf nginx-1.25.0.tar.gz 
git clone https://github.com/winshining/nginx-http-flv-module.git
mkdir -p /usr/local/nginx
mv nginx-http-flv-module /usr/local/nginx/

cd /opt/soft/nginx-1.25.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_secure_link_module --add-module=/usr/local/nginx/nginx-http-flv-module
make
make install
 
cd /usr/local/nginx/conf/
mkdir -p vhosts

配置
[root@localhost conf]# cat nginx.conf|grep -ivE ‘#|$| #’

worker_processes  1;
error_log  logs/error.log  warn;
 
events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}

    include flv.conf;
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    log_format nginx_json '{    "time": "$time_local", '
                               '"remote_ip": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"domain":"$host", '
                               '"responsetime":$request_time, '
                               '"request": "$request", '
                               '"response": "$status", '
                               '"bytes": $body_bytes_sent, '
                               '"referrer": "$http_referer", '
                               '"upstreamtime":"$upstream_response_time", '
                               '"upstreamaddr":"$upstream_addr", '
                               '"x_forwarded_for":"$http_x_forwarded_for", '
                               '"agent": "$http_user_agent" }';
     access_log  logs/access.log  nginx_json;
    sendfile        on;
    tcp_nopush     on;
    server_tokens off;
    keepalive_timeout  65;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types      text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    client_header_timeout 10;
    client_body_timeout 10;
    client_body_buffer_size 10K;
    proxy_buffering off;
 
    reset_timedout_connection on;
    send_timeout 10;
 
 
    charset utf-8;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /live {
            flv_live on;
            chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回复
            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的HTTP头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
        }
 
        location /flv {
             flv_live on;
             chunked_transfer_encoding on;
             add_header 'Access-Control-Allow-Origin' '*';
             add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
        }
 
 
        location /stat {
                   #推流播放和录制统计数据的配置
                 rtmp_stat all;
                 rtmp_stat_stylesheet stat.xsl;
          }
                   
        location /stat.xsl {
                 root html; #指定 stat.xsl 的位置
         }
                                                            
    }
}

[root@localhost conf]# cat flv.conf |grep -vE ‘^$’

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
    out_queue           4096;
    out_cork            8;
    max_streams         128;
    timeout             15s;
    drop_idle_publisher 15s;
 
    log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
    log_size     1m; #buffer size used by log module to log in access.log
    server {
        listen 1935;
        application live {

            live on; #开启直播
            record off; 
            #可以把转完码的视频放在这个文件里,这样可以拉这个视频进行播放
            #play /opt/video;
 
            # 允许从任何源push流
            allow publish all;
            # 允许从任何地方来播放流
            allow play all;
            # 20秒内没有push,就断开链接。
            drop_idle_publisher 20s;
            ##打开 GOP 缓存,减少首屏等待时间
            gop_cache on; 
        }
        }
}

启动

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# 
[root@localhost conf]# /usr/local/nginx/sbin/nginx
[root@localhost conf]# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      36990/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      36990/nginx: master 


注册服务

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# 
[root@localhost conf]# /usr/local/nginx/sbin/nginx
[root@localhost conf]# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      36990/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      36990/nginx: master 




vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

测试 - 推流

[root@localhost src]# ffmpeg -re -i 33335e4ffdce4f1f918bbde54b363a73.mp4 -c copy -f flv rtmp://192.168.1.60:1935/live/test            
ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '33335e4ffdce4f1f918bbde54b363a73.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isommp41mp42
    creation_time   : 2022-12-06 09:44:01
    copyright       : 
    copyright-eng   : 
  Duration: 00:00:16.93, start: 0.047891, bitrate: 494 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280, 447 kb/s, 25.56 fps, 29.83 tbr, 600 tbn, 1200 tbc (default)
    Metadata:
      creation_time   : 2022-12-06 09:44:01
      handler_name    : Core Media Video
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 41 kb/s (default)
    Metadata:
      creation_time   : 2022-12-06 09:44:01
      handler_name    : Core Media Audio
[flv @ 0x2268de0] Codec for stream 0 does not use global headers but container format requires global headers
[flv @ 0x2268de0] Codec for stream 1 does not use global headers but container format requires global headers
Output #0, flv, to 'rtmp://192.168.1.60:1935/live/test':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: isommp41mp42
    copyright-eng   : 
    copyright       : 
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: h264 ([7][0][0][0] / 0x0007), yuv420p, 720x1280, q=2-31, 447 kb/s, 25.56 fps, 29.83 tbr, 1k tbn, 600 tbc (default)
    Metadata:
      creation_time   : 2022-12-06 09:44:01
      handler_name    : Core Media Video
    Stream #0:1(und): Audio: aac ([10][0][0][0] / 0x000A), 44100 Hz, mono, 41 kb/s (default)
    Metadata:
      creation_time   : 2022-12-06 09:44:01
      handler_name    : Core Media Audio
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[flv @ 0x2268de0] Failed to update header with correct duration.ate= 496.6kbits/s    
[flv @ 0x2268de0] Failed to update header with correct filesize.
frame=  432 fps= 25 q=-1.0 Lsize=    1031kB time=00:00:16.99 bitrate= 496.7kbits/s    
video:923kB audio:86kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.089851%
[root@localhost src]# echo $?
0


-----------------end

你可能感兴趣的:(LNMP,#,Nginx,nginx,http,运维)