用到了   https://github.com/winshining/nginx-http-flv-module

nginx 流媒体服务器,nginx tcp http负载均衡,nginx 流转发,nginx 推流,nginx rtmp

user www-data;
worker_processes auto;
pid /run/nginx.pid;
#worker_cpu_affinity  auto; #1.9.10 以及之后的版本

include /etc/nginx/modules-enabled/*.conf;

load_module modules/ngx_http_flv_live_module.so;


worker_rlimit_nofile 200000;


events {
  worker_connections 10240;
  #multi_accept on;
}

http {

  ##
  # Basic Settings
  ##

  sendfile on;
  tcp_nopush on;
  types_hash_max_size 2048;
  # server_tokens off;

#aio threads;

  # server_names_hash_bucket_size 64;
  # server_name_in_redirect off;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  ##
  # SSL Settings
  ##

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  ssl_prefer_server_ciphers on;

  ##
  # Logging Settings
  ##

  #access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
access_log off;



  ##
  # Gzip Settings
  ##

  gzip on;

  # gzip_vary on;
  # gzip_proxied any;
  # gzip_comp_level 6;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # Virtual Host Configs
  ##

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;




    #http balance
    upstream myproject {
    ##后端目标服务器
        server 10.10.10.1:8080;
        server 10.10.10.2:8080;
        server 10.10.10.3:8080;
        server 10.10.10.4:8080;
    }
    server {
    ##前端接受流量入口
        listen 8888;
        server_name localhost;
        location / { 
            proxy_pass http://myproject;
        }
    }



server {
        listen 8083; #不是默认的 80 端口

        location /live {
        #url 重写 把  http://localhost:8083/live/3.flv 转为 http://localhost:8080/live?port=1935&app=live&stream=3
         rewrite ^/live/(.*)(.flv) http://localhost:8080/live?port=1935&app=live&stream=$1 permanent;
            
        }
    }




server {
        listen 8080; #不是默认的 80 端口

        location /live { #http://localhost:8080/live?port=1935&app=live&stream=2

            flv_live on; #打开 HTTP 播放 FLV 直播流功能 
            chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复 由于一些播放器不支持 HTTP 块传输, 这种情况下最好在指定了 flv_live on; 的 location 中指定 chunked_transfer_encoding off,否则播放会失败

            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }
    }

}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}




 

rtmp { #apt-get install libnginx-mod-rtmp 或使用https://github.com/winshining/nginx-http-flv-module 这支持http播放
 
    server {#接收推流
 
        listen 1935;
 
 
 
        application live {
 
            live on;
             gop_cache on; #打开 GOP 缓存,减少首屏等待时间
            max_connections 1024;
            push 192.168.2.238:1936;#推流到别的服务器,可以多台,写多行
           
          # exec ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec libx264 -acodec aac 
          -f flv rtmp://localhost:1939/$app/${name};#上面的push向ZLMediaKit推送有问题,可用这行
            
 
        }
 
        application zbcs {
 
            live on;
 
            record off;
 
        }

application mypush {
            live on;

            # Every stream published here
            # is automatically pushed to
            # these two machines
           # push rtmp1.example.com;
            push 192.168.2.238:1936;
        }
 
       # application hls{
 
        #    live on;
 
         #    hls on;
 
          #   hls_path /usr/local/var/www/hls;
 
          #   hls_fragment 1s;
 
        # }
 
    }



 
}

#stream{
#  server{#流转发
#     listen 8088;
 #    proxy_pass 127.0.0.1:1935; #推流服务

 # }
 # server{
 #    listen 8081;
 #    proxy_pass 127.0.0.1:3306; #数据库
 # }
 
#}




#nginx tcp loadbalance config
stream {
    upstream MyServer{
       #hash $remote_addr consistent;
       server 127.0.0.1:1935;#weight=1 max_fails=3 fail_timeout=30s
       server 192.168.2.238:1936;#weight=1 max_fails=3 fail_timeout=30s
     }

     server {
     proxy_connect_timeout 1s;
     #proxy_timeout 3s;
     listen 8000;
     proxy_pass MyServer;
     tcp_nodelay on;
   }
}





 

测试:

使用 ffmpeg 推流  ffmpeg -re -i 223.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/live/3

 

用flv播放器,打开地址rtmp://localhost:1935/live/3

或地址 http://localhost:8083/live/3.flv 或地址 rtmp://192.168.2.238:1936/live/3(推流到另外服务器),可以播放。

 

如果不想用nginx,也可以用 SRS,非常简单好用 https://ossrs.net/lts/zh-cn/

最后更新于 2022年8月24日

nginx 流媒体服务器,nginx tcp http负载均衡,nginx 流转发,nginx 推流,nginx rtmp
标签: