user  root;
daemon on;
master_process on;
worker_processes  1;
#worker_rlimit 4g;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

worker_rlimit_nofile 102400;
worker_rlimit_core   2G;
working_directory    /tmp;

pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  4096;
    multi_listen unix:/tmp/http 8080;
    multi_listen unix:/tmp/rtmp 1935;

    dynamic_refresh_interval 5s;
    dynamic_domain_buckets   1001;
    resolver 114.114.114.114  valid=1m;
    resolver_timeout 30s;
}

#stream_zone buckets=1024 streams=4096;

#dynamic_conf conf/nginx_dynamic.conf 10;
#dynamic_log logs/dynamic.log info;

rtmp {
    log_format log_bandwidth '{"app":"$app","name":"$name","bitrate":$bitrate,"args":"$args","timestamp":$ntp,"ts":"$time_local","type":"$command","remote_addr":"$remote_addr","domain":"$domain"}';
    access_log logs/bandwidth.log log_bandwidth trunc=60s;

    server {
        listen 1935;
        serverid 000;
        out_queue 2048;
        server_name live.pingos.io;
        rtmp_auto_pull on;
        rtmp_auto_pull_port unix:/tmp/rtmp;
   
        application push {
            live on;
            push rtmp://127.0.0.1/live app=live;
        }

        application live {
           live_record on;
           live_record_path /data/record;

            recorder r1{
                record all;
                record_path /data/record;
            }

            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 4000ms;
#            hls_max_fragment 10000ms;
            hls_playlist_length 12000ms;
            hls_type live;

            hls2 on;
            mpegts_cache_time 20s;

            hls2_fragment 2000ms;
            hls2_max_fragment 3000ms;
            hls2_playlist_length 6000ms;

            wait_key on;
            wait_video on;
            cache_time 1s;
            send_all on;
            low_latency off;
            fix_timestamp 2s;
# h265 codecid, default 12
            hevc_codecid  12;
        }
    }
}

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" "$http_X-Real-IP" "$host"';


    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #reset_server_name www.test1.com www.test2.com;
    #gzip  on;

    upstream hlsm {
#        hash $remote_addr consistent;
        hash $arg_session consistent;
        # 这里需要注意，你要开几个进程，就要按这个规则写几条记录
        server unix:/tmp/http.0;
#        server unix:/tmp/http.1;
#        server unix:/tmp/http.2;
#        server unix:/tmp/http.3;
    }

    server {
        listen 8080;
#        listen 443 ssl;
#        ssl_certificate     /usr/local/pingos/cert/full_chain.pem;
#        ssl_certificate_key /usr/local/pingos/cert/privkey.pem;
        location /rtmp_stat {
            rtmp_stat all;
            rtmp_stat_stylesheet /stat.xsl;
        }

        location /xstat {
            rtmp_stat all;
        }

        location /sys_stat {
            sys_stat;
        }
        location ~ .mp4$ {
            root html;
            #mp4;
        }

        location /control {
            rtmp_control all;
        }
         location /flv {
             flv_live 1935 app=live;
             add_header 'Access-Control-Allow-Origin' '*';
             add_header "Access-Control-Allow-Credentials" "true";
             add_header "Access-Control-Allow-Methods" "*";
             add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
             add_header "Access-Control-Expose-Headers" "*";
         }
         location /ts {
             ts_live 1935 app=live;
             expires -1;
             add_header 'Access-Control-Allow-Origin' '*';
             add_header "Access-Control-Allow-Credentials" "true";
             add_header "Access-Control-Allow-Methods" "*";
             add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
             add_header "Access-Control-Expose-Headers" "*";
         }
         location /hls {
            # Serve HLS fragments
             types {
                 application/vnd.apple.mpegurl m3u8;
                 video/mp2t ts;
             }
             root /tmp;
             expires -1;
             add_header Cache-Control no-cache;
             add_header 'Access-Control-Allow-Origin' '*';
             add_header "Access-Control-Allow-Credentials" "true";
             add_header "Access-Control-Allow-Methods" "*";
             add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
             add_header "Access-Control-Expose-Headers" "*";
         }

        location /hlsm {
             hls2_live 1935 app=live;
             add_header 'Access-Control-Allow-Origin' '*';
             add_header Cache-Control no-cache;
             add_header "Access-Control-Allow-Credentials" "true";
             add_header "Access-Control-Allow-Methods" "*";
             add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
             add_header "Access-Control-Expose-Headers" "*";
         }

         location /hls2 {
             proxy_buffering on;
             proxy_buffer_size 4k; 
             proxy_buffers 8 1M;
             proxy_busy_buffers_size 2M;
             proxy_max_temp_file_size 0;
             set $hls_args location=/hls2&scheme=$scheme;
#             if ($args) {
#                 set $hls_args $args&location=/hls2&scheme=$scheme;
#             }
             proxy_set_header Host $host:$server_port;
             rewrite ^/(.*)/(.*)\.(.*)$ /hlsm/$2.$3?$hls_args break;
             proxy_pass http://hlsm;
         }

         location / {
             chunked_transfer_encoding on;
             root html/;
         }
    }
}
