Recently live fire, live push stream software everywhere, so how to use NGINX push stream? The RTMP module of NGINX can be used to push the video stream.

  • Push the flow
  • Pull flow
  • Push flow certification
  • Pull flow certification
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()
	router.Any("/auth", func(context *gin.Context) {
		name := context.PostForm("name")
		username := context.PostForm("username")
		password := context.PostForm("password")
		fmt.Println(name)
		fmt.Println(username)
		fmt.Println(password)
		if username == "hanyun" && password == "123456" {
			context.JSON(http.StatusOK, nil)
		} else {
			context.AbortWithStatus(http.StatusUnauthorized)
		}
	})
	router.Run(": 8686")}Copy the code

We set up a local golang service that listens on port 8686 and returns HTTP status code 200 to user hanyun with password 123456 and other backwater status code 401. Why do you do that? The reasons are explained later. Nginx configuration file

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        application live {
            live on;
			on_publish http://127.0.0.1:8686/auth;
			on_play http://127.0.0.1:8686/auth;
        }
		
        application hls {
            live on;
            hls on;  
            hls_path temp/hls;  
            hls_fragment 8s;  
        }
    }
}

http {
    server {
        listen      8080;
		
        location / {
            root html;
        }
		
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root html;
        }
		
        location /hls {  
            #server hls fragments  
            types{  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            aliastemp/hls; expires -1; }}}Copy the code

We set up a service that plays our stream content, but also simulates the stream, access

http://192.168.0.101:8080/
Copy the code

You can change the IP address to your own IP address. Here’s a focus on nginx’s pull and push limitations

rtmp { server { listen 1935; application live { live on; On_publish http://127.0.0.1:8686/auth; On_play http://127.0.0.1:8686/auth; } application hls { live on; hls on; hls_path temp/hls; hls_fragment 8s; }}}Copy the code

Limit of push flow

On_publish http://127.0.0.1:8686/auth;Copy the code

Limit of pull flow

On_play http://127.0.0.1:8686/auth;Copy the code

Nginx uses post to request a push or pull address. If we return an HTTP status code of 200, nginx will reject a push or pull address if we return an HTTP status code of 401.

Here to explain to you the definition of the address of the push stream

RTMP: / / 192.168.0.101 / live/stream? username=hanyun&password=123456Copy the code
  • RTMP: / / 192.168.0.101 / live our current address
  • Stream is the stream name. The backend can post a key pair name=stream and z is the default
  • ? Username =hanyun&password=123456 these are user-defined, with strong flexibility, small friends can define their own

So we’re just going to know how to pull flow, push flow, authentication, and guys can try it out for themselves. Give you attach already installed RTMP nginx code and go links: pan.baidu.com/s/1iG2e0Adh… Extract code: 0Y0i Copy this section of content after opening Baidu web disk mobile App, more convenient operation oh