最近直播大火,直播推流軟件遍地開花,那麼用NGINX如何進行推流呢?下面咱們就簡單的介紹一下用NGINX的rtmp模塊如何實現視頻推流,咱們主要從一下幾點介紹:html
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")
}
複製代碼
咱們在本地起了個golang的服務,監聽8686端口,對密碼爲123456用戶名爲hanyun的用戶返回HTTP狀態碼爲200,其餘的反水狀態碼爲401。爲何要這麼作呢?後面闡述緣由。 nginx的配置文件nginx
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;
}
alias temp/hls;
expires -1;
}
}
}
複製代碼
咱們起了一個服務,用於播放咱們的推流內容,同時也能夠模擬推流,訪問git
http://192.168.0.101:8080/
複製代碼
具體狀況下小夥伴們能夠把ip地址改成本身的ip。 這裏重點說一下nginx拉流和推流的限制github
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;
}
}
}
複製代碼
推流的限制golang
on_publish http://127.0.0.1:8686/auth;
複製代碼
拉流的限制後端
on_play http://127.0.0.1:8686/auth;
複製代碼
nginx在推流和拉流的時候會採用post的方式請求咱們定義的地址,若是咱們返回的HTTP狀態碼爲200就能夠進行拉流或者推流了,若是返回其餘的狀態碼,例如401就會拒絕推流或者拉流。bash
再這裏給你們講解一下這個推流的地址的定義app
rtmp://192.168.0.101/live/stream?username=hanyun&password=123456
複製代碼
經過對這些的講解咱們就只能夠知道怎麼進行拉流,推流,鑑權,小夥們能夠本身動手試一下。 給你們附上已經安裝rtmp的nginx代碼和go的代碼 連接:pan.baidu.com/s/1iG2e0Adh… 提取碼:0y0i 複製這段內容後打開百度網盤手機App,操做更方便哦ide