首页 > 系统相关 >Nginx+Rtmp推流服务器方案

Nginx+Rtmp推流服务器方案

时间:2023-02-19 16:13:18浏览次数:41  
标签:nginx root server Nginx html Rtmp 推流 local usr

推流服务器方案一

一、前期准备

  1. 操作系统:Centos7 Linux 系统
  2. Nginx版本:nginx-1.22.0.tar.gz
  3. RTMP模块:nginx-rtmp-module
  4. 推流工具:OBS-Studio
  5. 拉流工具

二、环境搭建

1.安装依赖
yum install gcc make pcre pcre-devel openssl openssl-devel
2.下载并解压Nginx
  1. 这里下载的是nginx-1.22.0.tar.gz安装包,并将其放在了root目录下

    wget https://nginx.org/download/nginx-1.22.0.tar.gz
    
  2. /usr/local下创建nginx文件夹并进入

    cd /usr/local/
    mkdir nginx
    cd /nginx
    
  3. nginx安装包解压到/usr/local/nginx

    tar zxvf /root/nginx-1.22.0.tar.gz -C ./
    

    解压完成后,在/usr/local/nginx目录下出现一个nginx-1.22.0目录

3.下载并解压插件
  1. 这里下载的是nginx-rtmp-module模块,放在/usr/local/nginx目录下

    [root@localhost nginx] git clone https://github.com/arut/nginx-rtmp-module
    
4.配置和编译安装
[root@localhost nginx] cd nginx-1.22.0
[root@localhost nginx-1.22.0] ./configure --perfix=/usr/local/nginx --add-module=../nginx-rtmp-module
# 编译安装
[root@localhost nginx-1.22.0] make
[root@localhost nginx-1.22.0] make install
5.查看安装结果
[root@localhost nginx-1.22.0] /usr/local/nginx/sbin/nginx -v
# 输出 nginx version: nginx/1.22.0 即为安装成功

三、配置Nginx

1.设置Nginx开机启动
# 创建Nginx服务文件
vi /usr/lib/system/system/nginx.service

输入以下内容

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

启动Nginx服务

systemctl start nginx
systemctl enable nginx
2.修改Nginx配置文件
vi /usr/local/nginx/conf/nginx.conf

配置模板

rtmp_auto_push on;
rtmp {
    server {
        listen 1935;
        application live {
            live on;
            hls on;
            hls_fragment 3s;
            hls_playlist_length 10s;
            hls_path /usr/local/nginx/html/hls;
        }
        application hls {
            live on;
            hls on;
            hls_cleanup off;
            hls_fragment 3s;
            hls_playlist_length 10s;
            hls_path /usr/local/nginx/html/hls;
        }
    }
}
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
       server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   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;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    #server
    #{
    #       listen 8080;
    #       location /stat{
    #           rtmp_stat all; #所有状态
    #           rtmp_stat_stylesheet stat.xsl #state的样式表
    #           }
    #       location /stat.xsl{
    #           root /root/workspace/tmp/rtmp/nginx-rtmp-module;#state的样式表路径
    #   }
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
3.重启Nginx,开启端口
sudo firewall-cmd --add-port=1935/tcp --permanent
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

1935端口是默认的推流端口

# 重启Nginx
systemctl restart nginx
4.浏览器查看

image.png

出现Welcom to nginx ,配置成功

四、推流测试

打开OBS,在文件 -> 设置->直播中,填入rtmp://ip:port/live/,以及推流码

image.png

设置好了,添加一个屏幕捕获,点击开始直播

五、VLC拉流

打开VLC,媒体->打开网络串流->网络

输入: rtmp://ip:port/live/demo

或http://ip:port/hls/demo.m3u8

image.png

标签:nginx,root,server,Nginx,html,Rtmp,推流,local,usr
From: https://www.cnblogs.com/cnpolaris/p/17134906.html

相关文章

  • LVS(Linux Virtual Server)+Nginx 高可用集群
    LVS(Linux虚拟服务器)LVS(LinuxVirtualServer)是一个开源的负载均衡项目,是国内最早出现的开源项目之一,目前已被集成到Linux内核模块中。该项目在Linux内核中实现了基于......
  • Nginx location
                                                 c......
  • nginx之rewrite四种flag
    利用nginx的rewrite命令,可以实现URL的重写,可在nginx配置文件的server、location、if部分使用,对于rewrite有四种不同的flag。redirect:返回302临时重定向,浏览器地址栏会显示......
  • nginx 配置 详解
    2.nginx.conf配置文件Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和location(URL匹配特定位置......
  • Nginx匹配@符号的作用
    Nginx匹配@符号的作用@符号,用于定义一个Location块,且该块不能被外部Client所访问,只能被Nginx内部配置指令所访问,比如try_files或error_page.error_page400=@......
  • nginx原理学习--6
    nginx的请求处理阶段 接收请求流程  http请求格式简介 首先介绍一下rfc2616中定义的http请求基本格式: Request=Request-Line*((general-hea......
  • nginx配置无限个子域名
    nginx配置多个子域名功能:不管多少个子域名demo.liaosp.top/还是demo2.liaosp.top在servername添加域名,*的域名在前面修改:server_name*.daishua.liaosp.topdaishua.li......
  • nginx 原理学习--5
    nginx的源码目录结构(100%)nginx的优秀除了体现在程序结构以及代码风格上,nginx的源码组织也同样简洁明了,目录结构层次结构清晰,值得我们去学习。nginx的源码目录与nginx的......
  • Nginx:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化
    引言一、性能怪兽-Nginx概念深入浅出二、Nginx环境搭建三、Nginx反向代理-负载均衡四、Nginx动静分离五、Nginx资源压缩六、Nginx缓冲区七、Nginx缓存机制八、Ngi......
  • Nginx 系列1 --- 安装
    一、环境1.CentOS7.920092.Nginx1.22.1二、安装1.安装依赖sudoyuminstallyum-utils-y2.配置yum仓库sudovi/etc/yum.repos.d/nginx.repo初始化ngi......