Home Backend Development PHP Tutorial [Reprint] Using Nginx to build a streaming media server for http and rtmp protocols

[Reprint] Using Nginx to build a streaming media server for http and rtmp protocols

Aug 08, 2016 am 09:28 AM
http make nginx rtmp


From //http://blog.chedushi.com/archives/6532?utm_source=tuicool

Use Nginx to build a streaming media server for http and rtmp protocols

Experimental purpose:
Let Nginx support flv and mp4 format files, and support the Rtmp protocol; at the same time open the hls function of rtmp
Information:
HTTP Live Streaming (abbreviated as HLS) is A streaming media network transmission protocol based on HTTP proposed by Apple.
HLS only requests basic HTTP messages. Unlike Real-Time Transport Protocol (RTP), HLS can pass through any firewall or proxy server that allows HTTP data to pass through. It's also easy to use a content delivery network to stream media.
Use ffmpeg to complete the conversion of flv, mp4, mp3 and other formats (the on-demand experiment is not tested for the time being)
1. Preparation
Module: nginx_mod_h264_streaming (supports h264 encoded video)
Module: http_flv_module supports flv
Module: http_mp4_module supports mp4
Module: nginx-rtmp-module supports rtmp //2015-3-5 add
Download address:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
http:/ /nginx.org
https://github.com/arut/nginx-rtmp-module
1. Install dependency packages:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
2. Install git tool:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/ git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git –version
git version 1.8.1.GIT
#cd ..
3. Install ffmpeg and its dependency packages:
++++++++Yasm++++++++++
#wget http://www.tortall.net/projects /yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure –enable-shared
#make
#make install
#cd ..
++++++++LAME++++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99. 5.tar.gz
#cd lame-3.99.5
#./configure –enable-nasm
#make
#make install
#cd ..
++++++++libogg++++++++++
#wget http:/ /downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis++++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis- 1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx++++++++++
#git clone http:// git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure –enable-shared
#make
#make install
#cd ..
++++++++FAAD2++++++++++
#wget http ://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC++++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28 .tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..
++++++++Xvid++++++++++
# wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
# make
#make install
cd ..
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure –prefix=/opt/ffmpeg/ –enable-version3 –enable-libvpx – enable-libfaac –enable-libmp3lame –enable-libvorbis –enable-libx264 –enable-libxvid –enable-shared –enable-gpl –enable-postproc –enable-nonfree –enable-avfilter –enable-pthreads
#make && make install
#cd ..
Modify /etc/ld.so.conf as follows:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local /lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
2. Install Nginx related modules
#tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
#git clone git://github.com /arut/nginx-rtmp-module.git
#tar zxvf pcre-8.12.tar.gz
#cd pcre-8.12
#./configure
#make && make install
#tar zxvf nginx-1.2.6.tar. gz
#cd nginx-1.2.6
#./configure –prefix=/usr/local/nginx –add-module=../nginx_mod_h264_streaming-2.2.7 –with-http_flv_module –with-http_gzip_static_module –with-http_stub_status_module –with -http_mp4_module –add-module=../nginx-rtmp-module –add-module=../nginx-rtmp-module/hls –with-cc-opt=-I/opt/ffmpeg/include –with-ld-opt=’-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib’
#make && make install
3. Modify the nginx main configuration file and configure the virtual host (here we only configure and test on-demand for now, please see the reference address for live broadcast):
user nobody nobody;
worker_processes 4;
error_log logs/nginx_error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
#rtmp_auto_push on;
rtmp {
server {
listen 1935;
application vod {
play /opt/media/nginxrtmp/flv;
}
}
}
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 ;
limit_conn_zone $binary_remote_addr zone=perip:256k;
limit_conn_log_level notice;
sendfile on;
tcp_nopush on;
keepalive_timeout 6000;#测试并发临时调大
tcp_nodelay on;
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;
#log format
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
#支持flv
server
{
listen 8081;
server_name 192.168.0.33;
root /opt/pub/media/nginx; #http协议时候,flv视频位置
location ~ .*.(flv|swf|mp4|wma|wmv)$ {
valid_referers none blocked *.xxxx.com http://localhost;
if ($invalid_referer) {
return 403;
}
}
location ~ \.flv$ {
flv;
limit_conn one 20;#限制客户端并发连接数
limit_rate 200k;#限制每客户端最大带宽
}
location ~ \.mp4$ {
flv;
limit_conn one 20;
limit_rate 200k;
}
access_log logs/nginxflv_access.log main;
}
server
{
listen 8082;
server_name 192.168.0.33;
index index.html;
location / {
root /opt/pub/media/nginx-rtmp;
}
access_log logs/nginxrtmpflv_access.log main;
}
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/pub/soft/nginx-rtmp-module;
}
location / {
root /opt/pub/soft/nginx-rtmp-module/test/rtmp-publisher;
}
}
http://192.168.0.33:8080/stats 查看rtmp客户请求信息
http://192.168.0.33:8081/index.html 查看nginx http协议时候的flv视频
http://192.168.0.33:8082/index.html 查看nginx rtmp协议时候的flv视频
四、基本功能测试:
播放器采用开源播放器jwplayer,将播放器放到index.html同级目录下
NGINX-Http测试页面index.html(切记http的时候需要给视频添加关键帧)




test of nginx-http









ndex.cgi?P1_Prod_Version=ShockwaveFlash” flashvars=”type=http&file=http://192.168.0.33:8081/2.flv&autostart=true” wmode=”opaq
ue” />



NGINX-RTMP测试页面index.html




nginx-rtmp









ndex.cgi?P1_Prod_Version=ShockwaveFlash” flashvars=”streamer=rtmp://192.168.0.33:1935/vod&file=2.flv&autostart=true” wmode=”o
paque” />



页面能打开正常播放就说明配置是正确的。
五、并发测试
NGINX-HTTP采用loadrunner测试(略)
NGINX-RTMP采用开源软件flazr测试:
前提需要安装jdk软件
client.bat rtmp://192.168.0.33:1935/vod/2.flv -load 500
这里的500是并发数

由于局域网的限制,带宽测试到一定大小的时候就上不去了,故此步截图不忽略。

以上就介绍了【转载】利用Nginx搭建http和rtmp协议的流媒体服务器,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to allow external network access to tomcat server How to allow external network access to tomcat server Apr 21, 2024 am 07:22 AM

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Welcome to nginx!How to solve it? Welcome to nginx!How to solve it? Apr 17, 2024 am 05:12 AM

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

How to generate URL from html file How to generate URL from html file Apr 21, 2024 pm 12:57 PM

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

Can nodejs be accessed from the outside? Can nodejs be accessed from the outside? Apr 21, 2024 am 04:43 AM

Yes, Node.js can be accessed from the outside. You can use the following methods: Use Cloud Functions to deploy the function and make it publicly accessible. Use the Express framework to create routes and define endpoints. Use Nginx to reverse proxy requests to Node.js applications. Use Docker containers to run Node.js applications and expose them through port mapping.

How to deploy and maintain a website using PHP How to deploy and maintain a website using PHP May 03, 2024 am 08:54 AM

To successfully deploy and maintain a PHP website, you need to perform the following steps: Select a web server (such as Apache or Nginx) Install PHP Create a database and connect PHP Upload code to the server Set up domain name and DNS Monitoring website maintenance steps include updating PHP and web servers, and backing up the website , monitor error logs and update content.

See all articles