前言

新业务需要测试小程序的直播组件,没有推流,拉流地址。通过自建直播平台辅助完成小程序及其他功能的测试。

服务器环境

N年前的vultr服务器,5美元/月一直用来做测试服务器

  • 地区:日本
  • vCPU/s: 1 vCPU
  • RAM: 512.00 MB
  • OS: CentOS 8 Stream x64(重装了,之前的系统太老了)

部署版本

Nginx:nginx-1.25.3
nginx-http-flv-module:Version 1.2.11


步骤

xshell连接服务器,服务器环境为全新环境,无提前安装nginx

下载

进入home目录(个人习惯),cd /home

下载nginx和nginx-http-flv-module

wget https://nginx.org/download/nginx-1.25.3.tar.gz
wget https://github.com/winshining/nginx-http-flv-module/archive/refs/tags/v1.2.11.tar.gz

扩展




解压

tar -zxvf nginx-1.25.3.tar.gz
tar -zxvf v1.2.11.tar.gz

安装依赖环境

yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel

进入nginx目录

cd nginx-1.25.3/
./configure --add-module=/home/nginx-http-flv-module-1.2.11
make
make install

找到nginx

安装完成之后,nginx位置在/usr/local/nginx/sbin, 启动nginx

cd /usr/local/nginx/sbin
./nginx -s reload

查看nginx是否启动成功

ps -ef | grep nginx

防火墙开放端口

# 查看以开放端口
firewall-cmd --list-ports

# 开放tcp,80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent

优先控制端口,图方便可以关闭防火墙(如果服务器测试使用,建议先关闭,本文为关闭状态进行的后续操作)

# 查看防火墙状态
systemctl status firewalld.service

# 开启防火墙
systemctl start firewalld.service

# 关闭防火墙
systemctl stop firewalld.service

# 禁用防火墙
systemctl disable firewalld.service

验证服务器

游览器中输入服务器地址

成功访问nginx即可

开始配置nginx

cd /usr/local/nginx/conf
vim nginx.conf

配置文件源码


#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  10240;
}



rtmp {

    out_queue           4096;
    out_cork            8;
    max_streams         128;
    timeout             15s;
    drop_idle_publisher 15s;

    log_interval 5s; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
    log_size     1m; #log 模块用来记录日志的缓冲区大小

    server {
        listen 1935; #默认的 1935 端口
    chunk_size 4096;

        application live {
            live on;
        gop_cache on;
            record off;
        }
    }
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';


    #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;
        }
    

    location /live {
                flv_live on;
                chunked_transfer_encoding on;
        }



        #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;
        #}
    }


    server {
        listen 8080; #不是默认的 80 端口
        
    location / {
            root   html;
            index  index.html index.htm;
        }
    

    



    location /live {
                flv_live on;
                chunked_transfer_encoding on;       
        }       


    }


    # 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;
    #    }
    #}


    # 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;
    #    }
    #}

}

下载obs

https://obsproject.com/

填写推流的地址,推流码可以随便填写

rtmp默认端口号为1935,如果上方配置文件更改了端口号,此处ip地址后需要填写端口号

rtmp://你的服务器ip地址/live

确定后点击开始直播

下载PotPlayer或者VLC

两个播放器都行,操作如截图所示

https://potplayer.daum.net/
https://www.videolan.org/vlc/index.an.html


成功直播

相关文档

https://github.com/winshining/nginx-http-flv-module
https://nginx.org/en/download.html

最后修改:2023 年 10 月 28 日
如果觉得我的文章对你有用,奖励一杯咖啡吧!