nginx搭建rtmp服务器

1、下载nginx,地址http://nginx.org/en/download.html

2、下载nginx-rtmp-module,https://github.com/arut/nginx-rtmp-module#example-nginxconf

3、我这边下载的是最新版本nginx-1.10.3,解压后进入nginx-1.10.3,执行以下命令:

./configure --prefix=/usr/local/nginx  --add-module=/home/samba/shb/rtmp/nginx-rtmp-module  --with-http_ssl_module
/home/samba/shb/rtmp/nginx-rtmp-module是 rtmp/nginx-rtmp-module解压的路径。

4、make&&make install

5、安装成功后,打开

vi /usr/local/nginx/conf/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  1024;
}

rtmp {  
        server {  
            listen 1935;  
      
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /tmp/hls;  
            }  
        }  
    }  


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


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

}

增加rtmp配置

rtmp {  
        server {  
            listen 1935;  
      
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /tmp/hls;  
            }  
        }  
    }  

6、启动服务器
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

启动的时候有可能会出现:

/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:

1、首先确认已经安装好pcre 软件(nginx 依赖该软件)
2、创建软连接
对于/lib/* 32位系统来说:
1 #查看lib库
2 #  ls /lib/ |grep pcre
3 libpcre.so.0
4 libpcre.so.0.0.1
5 #添加软连接
6 # ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
对于/lib64/* 64位系统来说:
1 #查看lib库
2 #  ls /lib64/ |grep pcre
3 libpcre.so.0
4 libpcre.so.0.0.1
5 #添加软连接
6 # ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1

7、使用ffmpeg推送码流

ffmpeg -i rtsp://admin:[email protected] -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://172.16.51.106:1935/myapp/test1

nginx搭建rtmp服务器_第1张图片


使用VLC进行播放:

rtmp://172.16.51.106:1935/myapp/test1

nginx搭建rtmp服务器_第2张图片







你可能感兴趣的:(nginx)