Nxing HLS 反向代理

Nxing HLS 反向代理

我使用 5 个 nginx 服务器作为我的源站的反向代理。我的源站仅发送 HLS 内容,因此是 .ts、.aac、.vtt 和 .m3u8 文件。

在大多数情况下,这种方法效果很好,但当我有大约 8k - 12k 个活动连接,速度大约为 150rq/s 时,我开始看到性能下降。

我服务的最大文件约为 3M,或 2.5mbps。

我希望找到一种方法来允许更多的活动连接或至少拥有 12k 个连接而不会降低性能。

user  nginx;
worker_processes  8;
worker_priority -15;
worker_rlimit_nofile 1048576;

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

events {
    worker_connections  1048576;
    multi_accept on;
    use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$host" sn="$server_name" ' 'rt=$request_time ' 'ua="$upstream_addr" us="$upstream_status" ' 'ut="$upstream_response_time" ul="$upstream_response_length" ' 'cs=$upstream_cache_status' ;
error_log /var/log/nginx/error.log warn;
access_log  /var/log/nginx/access.log  main_ext;
access_log syslog:server=unix:/dev/log,tag=amplify,severity=info main_ext;
    sendfile        on;
    #tcp_nopush     on;
    #tcp_nodelay on;

    keepalive_timeout 120;
    keepalive_requests 100000;
    connection_pool_size 8192;

    #gzip  on;
    open_file_cache max=400000 inactive=30s;
    open_file_cache_errors off;
    open_file_cache_min_uses 1;
    open_file_cache_valid 60s;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}


        location /nginx_status {
           stub_status on;
           allow 127.0.0.1;
           deny all;
       }

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

}

upstream backend {
    ip_hash;
    keepalive 100;
        server server1;
        server server2; 
}

proxy_cache_path "/mnt/Delta/nginx_cache/nginx5/tmp/" levels=1:2 keys_zone=hls:10m inactive=2h use_temp_path=off;
proxy_cache hls;
proxy_cache_min_uses 1;

server {
        listen       80;
        #server_name  localhost;


        location = /healthz {
           return 200;
           access_log off;
        }

    location / {
        root /mnt/Delta/nginx_cache;
            try_files $request_uri @proxy_origin;
    }
#        location ~* \.(mpd)$ {
#                rewrite /middle/([^/]+)/$1 break;
#                proxy_cache off;
#                expires -1;
#                proxy_pass http://backend$1;
#        }
    location ~* \.(m3u8)$ {
        rewrite /middle/([^/]+)/$1 break;
        #proxy_cache off;
        #expires -1;
        proxy_cache_valid 200 302 5s;
            proxy_ignore_headers "Set-Cookie";
            proxy_hide_header "Set-Cookie";
            add_header X-Proxy-Cache $upstream_cache_status;
            add_header Host Nginx5;
        proxy_buffering on;
            proxy_buffer_size 1024k;
            proxy_buffers 5 1024k;
            proxy_pass http://backend$1;
        }
    #location ~* \.(ts)$ {
        #       root /mnt/Delta/nginx_cache/tmp;
        #       try_files $request_uri @proxy_origin;
        #       proxy_pass http://backend$1;
        #}
    location @proxy_origin {
        #resolver 10.196.160.160;
        #proxy_buffering on;
        #proxy_buffer_size 4096k;
        #proxy_buffers 5 4096k;
        proxy_pass http://backend$1;
        proxy_cache_valid 404 3s;
        proxy_cache_methods GET HEAD;
        proxy_redirect off;
            proxy_http_version 1.1;
            proxy_read_timeout 10s;
            proxy_send_timeout 10s;
            proxy_connect_timeout 10s;
                proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
                proxy_cache_background_update on;
                proxy_cache_lock on;        
        #proxy_temp_path "/mnt/Delta/nginx_cache/nginx2/tmp";
        #proxy_store "/mnt/Delta/nginx_cache/nginx2/tmp/$request_uri";
        #proxy_store_access user:rw group:rw all:r;
        #proxy_method GET;
        proxy_set_header Host $host;
        proxy_ignore_headers "Set-Cookie";
                proxy_hide_header "Set-Cookie";
                add_header X-Proxy-Cache $upstream_cache_status;
                add_header Host Nginx5;
    }

    }

相关内容