6

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

 2 years ago
source link: https://blog.p2hp.com/archives/8467
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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

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





  1. user www-data;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. #worker_cpu_affinity auto; #1.9.10 以及之后的版本
  5. include /etc/nginx/modules-enabled/*.conf;
  6. load_module modules/ngx_http_flv_live_module.so;
  7. worker_rlimit_nofile 200000;
  8. events {
  9. worker_connections 10240;
  10. #multi_accept on;
  11. http {
  12. # Basic Settings
  13. sendfile on;
  14. tcp_nopush on;
  15. types_hash_max_size 2048;
  16. # server_tokens off;
  17. #aio threads;
  18. # server_names_hash_bucket_size 64;
  19. # server_name_in_redirect off;
  20. include /etc/nginx/mime.types;
  21. default_type application/octet-stream;
  22. # SSL Settings
  23. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  24. ssl_prefer_server_ciphers on;
  25. # Logging Settings
  26. #access_log /var/log/nginx/access.log;
  27. error_log /var/log/nginx/error.log;
  28. access_log off;
  29. # Gzip Settings
  30. gzip on;
  31. # gzip_vary on;
  32. # gzip_proxied any;
  33. # gzip_comp_level 6;
  34. # gzip_buffers 16 8k;
  35. # gzip_http_version 1.1;
  36. # gzip_types text/plain text/css application/<a class="wpal-linked-keyword" href="http://json.p2hp.com/" target="_blank">json</a> application/javascript text/xml application/xml application/xml+rss text/javascript;
  37. # Virtual Host Configs
  38. include /etc/nginx/conf.d/*.conf;
  39. include /etc/nginx/sites-enabled/*;
  40. #http balance
  41. upstream myproject {
  42. ##后端目标服务器
  43. server 10.10.10.1:8080;
  44. server 10.10.10.2:8080;
  45. server 10.10.10.3:8080;
  46. server 10.10.10.4:8080;
  47. server {
  48. ##前端接受流量入口
  49. listen 8888;
  50. server_name localhost;
  51. location / {
  52. proxy_pass http://myproject;
  53. server {
  54. listen 8083; #不是默认的 80 端口
  55. location /live {
  56. #url 重写 把 http://localhost:8083/live/3.flv 转为 http://localhost:8080/live?port=1935&app=live&stream=3
  57. rewrite ^/live/(.*)(.flv) http://localhost:8080/live?port=1935&app=live&stream=$1 permanent;
  58. server {
  59. listen 8080; #不是默认的 80 端口
  60. location /live { #http://localhost:8080/live?port=1935&app=live&stream=2
  61. flv_live on; #打开 HTTP 播放 FLV 直播流功能
  62. chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复 由于一些播放器不支持 HTTP 块传输, 这种情况下最好在指定了 flv_live on; 的 location 中指定 chunked_transfer_encoding off,否则播放会失败
  63. add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
  64. add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
  65. #mail {
  66. # # See sample authentication script at:
  67. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  68. # # auth_http localhost/auth.php;
  69. # # pop3_capabilities "TOP" "USER";
  70. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  71. # server {
  72. # listen localhost:110;
  73. # protocol pop3;
  74. # proxy on;
  75. # server {
  76. # listen localhost:143;
  77. # protocol imap;
  78. # proxy on;
  79. rtmp { #apt-get install libnginx-mod-rtmp 或使用https://github.com/winshining/nginx-http-flv-module 这支持http播放
  80. server {#接收推流
  81. listen 1935;
  82. application live {
  83. live on;
  84. gop_cache on; #打开 GOP 缓存,减少首屏等待时间
  85. max_connections 1024;
  86. push 192.168.2.238:1936;#推流到别的服务器,可以多台,写多行
  87. # exec ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec libx264 -acodec aac
  88. -f flv rtmp://localhost:1939/$app/${name};#上面的push向ZLMediaKit推送有问题,可用这行
  89. application zbcs {
  90. live on;
  91. record off;
  92. application mypush {
  93. live on;
  94. # Every stream published here
  95. # is automatically pushed to
  96. # these two machines
  97. # push rtmp1.example.com;
  98. push 192.168.2.238:1936;
  99. # application hls{
  100. # live on;
  101. # hls on;
  102. # hls_path /usr/local/var/www/hls;
  103. # hls_fragment 1s;
  104. #stream{
  105. # server{#流转发
  106. # listen 8088;
  107. # proxy_pass 127.0.0.1:1935; #推流服务
  108. # server{
  109. # listen 8081;
  110. # proxy_pass 127.0.0.1:3306; #数据库
  111. #nginx tcp loadbalance config
  112. stream {
  113. upstream MyServer{
  114. #hash $remote_addr consistent;
  115. server 127.0.0.1:1935;#weight=1 max_fails=3 fail_timeout=30s
  116. server 192.168.2.238:1936;#weight=1 max_fails=3 fail_timeout=30s
  117. server {
  118. proxy_connect_timeout 1s;
  119. #proxy_timeout 3s;
  120. listen 8000;
  121. proxy_pass MyServer;
  122. tcp_nodelay on;
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/<a class="wpal-linked-keyword" href="http://json.p2hp.com/" target="_blank">json</a> 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(推流到另外服务器),可以播放。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK