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

Nodejs-cluster module knowledge points summary and example usage

The interviewer will sometimes ask you, tell me h...

Example of implementing the Graphql interface in Vue

Note: This article is about the basic knowledge p...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

Perfect solution to Google Chrome autofill problem

In Google Chrome, after successful login, Google ...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

How to install Odoo12 development environment on Windows 10

Preface Since many friends say they don’t have Ma...

Vue achieves the top effect through v-show

html <div class="totop" v-show="...

DOCTYPE Document Type Declaration (Must-Read for Web Page Lovers)

DOCTYPE DECLARATION At the top of every page you w...

MySQL index optimization: paging exploration detailed introduction

Table of contents MySQL Index Optimization Paging...

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution Recently,...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

JQuery implements hiding and displaying animation effects

This article shares the specific code of JQuery t...

Automatic backup of MySQL database using shell script

Automatic backup of MySQL database using shell sc...