CC's blog

CC的技术博客 专注于 it 互联网的技术爱好者

标签 直播 下的文章

怎么在 linux 上搭建一个 nginx + rtmp 直播服务器

1.安装编译工具及库文件(使用yum命令安装)

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng* libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libX* libtiff libtiff* make mpfr ncurses* ntp openssl nasm nasm* openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* wget zlib-devel libxml2 libxml2-devel libxslt libxslt-devel unzip

2.源码下载

nginx源码

nginx-rtmp源码

3.解压源码

tar -zxvf ***.tar.gz

4.编译安装

cd nginx-1.10.2

./configure --add-module=../nginx-rtmp-module-master

make

sudo make install

5.环境配置

首先要做的(如果之前配置过nginx,比如反向代理),就是stop当前正在运行的nginx,并关闭开机自启动

/etc/init.d/nginx stop
chkconfig --level 2345 nginx off

之后流媒体服务器(点播、直播)的配置,参考http://lib.csdn.net/article/57/37915?knId=1549,不再重复造轮子

注意:点播支持flv、mp4 视频h264音频aac格式

6.ffmpeg测试命令

推流
ffmpeg -re -i test.avi -vcodec h264 -acodec aac -f flv rtmp://219.216.87.99/live/test1
转码
ffmpeg -i test.avi -acodec aac -vcodec h264 -f flv test.flv
7.开机自启

./usr/local/nginx/sbin/nginx

重点来了

在此视频直播的基础上,继续配置nginx反向代理

主要修改nginx.conf就可以实现,这里有个问题,就是配置rtmp时,使用了http服务器的部分,相当于主域名www会由配置流媒体服务器的那个http server响应。原来nginx只是作为反向代理,自己不提供内容。这样原来由apache响应的index就要交给nginx。

直接看配置吧

user  nginx;
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;
}
rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application vod {
            play /home/yourname/videoServer/vod;
        }
        application vod_http {
            #play http://video.yourname.cn/vod;
            play http://localhost/vod;
        }

        application live {
            live on;
        }

        application hls {
            live on;
            hls on;
            hls_path /home/yourname/videoServer/hls;
            wait_key on;
            hls_fragment 10s;
            hls_playlist_length 60s;
            hls_continuous on;
            hls_cleanup on;
            hls_nested on;
        }
    }
}


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  www.yourname.cn;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /usr/local/nginx/livestats/;
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            add_header Cache-Control no-cache;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }



    upstream tomcat{
        server localhost:8081;
    }
    server {
        listen      80;
        server_name     tom.yourname.cn;
        location / {
            proxy_pass      http://tomcat;
            proxy_redirect      off;
            proxy_set_header    Host $host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }

    }

    upstream apache_index{
        server localhost:9080;
    }
    server {
        listen          80;
        server_name     video.yourname.cn;#此前这是www,现在变成二级域名
        location / {
            proxy_pass              http://apache_index;
            proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

    }


    upstream apache_blog{
        server localhost:9090;
    }
    server {
        listen          80; 
        server_name     blog.yourname.cn;
        location / {
            proxy_pass              http://apache_blog;
            proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

    }
    upstream apache_bbs{
        server localhost:9095;
    }
    server {
        listen          80;
        server_name     bbs.yourname.cn;
        location / {
            proxy_pass              http://apache_bbs;
            proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

    }

}
阅读全文