Centos6.5 nginx+nginx-rtmp配置流媒體服務器

以前使用命令方式安裝nginx並配置了反向代理,因爲想作一個視頻直播的小項目,查了流媒體服務器的方案,發現nginx有相關模塊,因而開始搞起。php

nginx-rtmp模塊須要在nginx編譯時,以模塊方式編譯進去,因此,以前搭建的nginx不能用了,只能另外下源碼編譯,可是也沒必要卸載原來的環境,後面會介紹如何處理兩個環境共存問題。html

1.安裝編譯工具及庫文件(使用yum命令安裝)linux

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源碼c++

nginx-rtmp源碼git

3.解壓源碼github

4.編譯安裝apache

cd nginx-1.10.2

./configure --add-module=../nginx-rtmp-module-master
make
sudo make install

5.環境配置tomcat

首先要作的(若是以前配置過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;
        }

    }

}

 

還有就是,以前www、blog、bbs都設置了cdn,在作了新域名解析後,網站靜態頁面的內容並無當即更新,這個把我坑了一把,等一段時間就行了,這和你設置cdn更新時間有關。還有就是,rtmp關心的是rtmp服務器的真實ip,若是域名作了cdn解析,可能會失敗。

相關文章
相關標籤/搜索