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

VMware Tools installation and configuration tutorial for Ubuntu

Some time ago, the blogger installed the Ubuntu s...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

Vue+element ui realizes anchor positioning

This article example shares the specific code of ...

32 Typical Column/Grid-Based Websites

If you’re looking for inspiration for columnar web...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

Detailed explanation of JavaScript's garbage collection mechanism

Table of contents Why do we need garbage collecti...

How to deal with too many Docker logs causing the disk to fill up

I have a server with multiple docker containers d...

Example method of viewing IP in Linux

Knowing the IP address of a device is important w...

An article to understand Linux disks and disk partitions

Preface All hardware devices in the Linux system ...

SQL GROUP BY detailed explanation and simple example

The GROUP BY statement is used in conjunction wit...

The difference between mysql outer join and inner join query

The syntax for an outer join is as follows: SELEC...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

jQuery implements form validation

Use jQuery to implement form validation, for your...