Use Nginx to build a streaming media server to realize live broadcast function

Use Nginx to build a streaming media server to realize live broadcast function

Written in front

In recent years, the live streaming industry has become quite popular. Whether it is live streaming in traditional industries or shopping, games, and education, live streaming is involved. As a friend who has worked in the Internet industry for many years, have you ever thought about how to build a live broadcast environment using Nginx? Don’t worry, next, we will use Nginx to build a live broadcast environment.

Install Nginx

Note: Here we take CentOS 6.8 server as an example to install Nginx as the root user.

1. Install dependent environment

yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* 
libpng* freetype* 
autoconf automake zlib* 
fiex* libxml* 
libmcrypt* 
libtool-ltdl-devel* 
libaio libaio-devel 
bzr libtool

2. Install openssl

wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar -zxvf openssl-1.0.2s.tar.gz
cd /usr/local/src/openssl-1.0.2s
./config --prefix=/usr/local/openssl-1.0.2s
make
make install

3. Install PCRE

wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz
cd /usr/local/src/pcre-8.43
./configure --prefix=/usr/local/pcre-8.43
make
make install

4. Install zlib

wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd /usr/local/src/zlib-1.2.11
./configure --prefix=/usr/local/zlib-1.2.11
make
make

5. Download nginx-rtmp-module

The official github address of nginx-rtmp-module: https://github.com/arut/nginx-rtmp-module

Use command:

git clone https://github.com/arut/nginx-rtmp-module.git

6. Install Nginx

wget http://nginx.org/download/nginx-1.19.1.tar.gz
tar -zxvf nginx-1.19.1.tar.gz
cd /usr/local/src/nginx-1.19.1
./configure --prefix=/usr/local/nginx-1.19.1 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module
make
make install

It should be noted here that when installing Nginx, the source code decompression directory of openssl, pcre and zlib is specified. After the installation is complete, the full path of the Nginx configuration file is: /usr/local/nginx-1.19.1/conf/nginx.conf.

Configure Nginx

Configuring Nginx mainly involves configuring the nginx.conf file of Nginx. We can enter the following command in the command line to edit the nginx.conf file.

vim /usr/local/nginx-1.19.1/conf/nginx.conf

Add the following content to the file.

rtmp {
 server {
  listen 1935; #Listening port chunk_size 4096; 
  application hls { #rtmp streaming request path live on; 
   hls on; 
   hls_path /usr/share/nginx/html/hls; 
   hls_fragment 5s; 
  } 
 } 
}

The hls_path requires read and write permissions. Next, we create the /usr/share/nginx/html/hls directory.

mkdir -p /usr/share/nginx/html/hls
chmod -R 777 /usr/share/nginx/html/hls

Next, modify the server module in http:

server { 
 listen 81; 
 server_name localhost; 
 
 #charset koi8-r; 
 
 #access_log logs/host.access.log main; 
 
 location / { 
  root /usr/share/nginx/html; 
  index index.html index.htm; 
 } 
 
 #error_page 404 /404.html; 
 
 # redirect server error pages to the static page /50x.html 
 # 
 error_page 500 502 503 504 /50x.html; 
 location = /50x.html { 
  root html; 
 }
}

Then start Nginx:

/usr/local/nginx-1.19.1/sbin/nginx -c /usr/local/nginx-1.19.1/conf/nginx.conf

Enable OBS to stream

OBS (Open Broadcaster Software) is free and open source software for the purpose of streaming live content over the Internet. You need to download this software and use it to push the stream (it seems that it cannot be installed on computers without cameras...)

The download link of OBS is: https://obsproject.com/zh-cn/download.

After installation, there will be a chart on the desktop like the one shown below.

After opening, we need to have a scene, and in this scene there is a source of stream (it can be a window, if the video is selected, the camera will be automatically identified), and the next step is to set it up.

The most important thing to pay attention to in the configuration is the stream configuration. Since it is a self-built streaming media server, we configure it as shown below.

rtmp://your server ip:port(1935)/live #URL fill in the stream address 

After the settings are completed, we can start streaming.

Pull flow test address

Here is a recommended streaming test address, which can test streaming for various protocols. Pay attention to a few places in the picture. Since we use the RTMP protocol, we select this column, fill in the streaming address and the name of the stream configured in the above OBS settings, start, and you're done! ! !

This is the end of this article about using Nginx to build a streaming media server to realize the live broadcast function. For more related nginx to build a streaming media server to realize live broadcast content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of configuring Nginx+RTMP+HLS+HTTPFLV server in Ubuntu 18.04 to realize on-demand/live broadcast/recording functions
  • Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14
  • Nginx-rtmp realizes real-time streaming effect of live media
  • Nginx uses nginx-rtmp-module module to realize the live broadcast room function
  • Detailed steps to build nginx+rtmp live server on Mac
  • Detailed explanation of the process of building an image server with nginx (the difference between root and alias)
  • How to set up URL link in Nginx server
  • How to use nginx to access local static resources on Linux server
  • Nginx builds rtmp live server implementation code

<<:  Solve the problem of Navicat importing database data structure sql reporting error datetime(0)

>>:  Vuex combines session storage data to solve the problem of data loss when refreshing the page

Recommend

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...

Detailed steps to install MYSQL8.0 on CentOS7.6

1. Generally, mariadb is installed by default in ...

Vue implements small search function

This article example shares the specific code of ...

Detailed steps for remote deployment of MySQL database on Linux

Linux remote deployment of MySQL database, for yo...

About the problem of dynamic splicing src image address of img in Vue

Let's take a look at the dynamic splicing of ...

In-depth understanding of CSS @font-face performance optimization

This article mainly introduces common strategies ...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...

React antd realizes dynamic increase and decrease of form

I encountered a pitfall when writing dynamic form...

Solution for using Baidu share on Https page

Since enabling https access for the entire site, ...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

Detailed explanation of docker command to backup linux system

tar backup system sudo tar cvpzf backup.tgz --exc...