git clone https://github.com/arut/nginx-rtmp-module /www/server/nginx_rtmp_module/ //拉取nginx-rtmp-module待编译文件 cd /www/server/panel/install //进入安装配置文件路径 cp nginx.sh nginxcopy.sh //复制一份安装shell脚本 所有的修改在复制的脚本上进行 vi nginxcopy.sh //开始修改 /configure //找到nginx编译配置文件 --add-module=/www/server/nginx_rtmp_module //增加一个模块配置 :wq //保存推出 sh nginxcopy.sh install 1.12 //编译安装 nginx -V //安装完成后 查看配置信息
安装成功后,配置rtmp来实现推拉流
//配置文件所在目录 cd /www/server/nginx/conf //修改nginx.conf文件 user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } include rtmp.conf;//新增 http { include mime.types; #include luawaf.conf; include proxy.conf; default_type application/octet-stream; server_names_hash_bucket_size 512; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]."; limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; server_tokens off; access_log off; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; #error_page 404 /404.html; include enable-php.conf; location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } //其中rtmp.conf位于/www/server/nginx/conf目录下,配置内容为 rtmp_auto_push on; rtmp { server { listen 19350; ping 30s; chunk_size 4000; notify_method get; application hls { live on; hls on; hls_path /www/tmp/hls; hls_sync 100ms; hls_fragment 5s; } application live { live on; } } } //然后在http访问配置中增加 location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /www/tmp; add_header Cache-Control no-cache; }
配置完成后,
推流地址:rtmp://ip:19350/hls
拉流地址:http://ip/hls/密钥串.m3u8
拉流播放器我接入的是腾讯云,文档地址:https://cloud.tencent.com/document/product/454/7503
例子:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>直播</title> <script src="//imgcache.qq.com/open/qcloud/video/vcplayer/TcPlayer-2.3.2.js" charset="utf-8"></script> </head> <body> <div id="id_test_video" style="100%; height:auto;"></div> </body> <script> var player = new TcPlayer('id_test_video', { "m3u8": "http://地址/hls/123456.m3u8", //"flv": "http://地址/123456.flv", "autoplay" : true, "poster" : "https://obsproject.com/assets/images/new_icon_small.png", "width" : '480', "height" : '320' }); </script> </html>