1

我正在尝试使用 nginx 实现 mp4 流。它正在工作,但流式传输 mp4 文件很慢。我已经在 ubuntu 服务器中完成了以下配置。

文件存储在本地磁盘上

谁能知道如何优化它以获得更快的流?

还有其他最好的流媒体方式吗?

服务器配置:

16 GB RAM、4 个 vCPU、320 GB SSD

#user  nobody;
worker_processes  2;
#worker_rlimit_nofile 300000;

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


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"';
    server_tokens off;
    access_log off;
    sendfile_max_chunk 512k;
    sendfile        on;
    tcp_nopush     on;
    log_not_found       off;
    keepalive_timeout  65;
    #limit_rate 225k;
   # client_body_buffer_size 5M;

    gzip  on;
 # add proxy caches
        proxy_cache_path /tmp/mycache keys_zone=mycache:70m;
        proxy_cache_path /tmp/cache keys_zone=myimages:10m inactive=60m;


    server {
        index  index.html index.htm;
        server_name abc.com;


       location ~* \.(mp3|aac|jpg|jpeg|png|gif|ico|css|js)$ {
         auth_request     /auth;
         auth_request_set $auth_status $upstream_status;
         root /upload/;
 expires 365d;
 proxy_cache myimages;
 autoindex off;
 
}
location = /auth {
            internal;
            proxy_pass              http://localhost/auth-0.0.1-SNAPSHOT/auth;
            proxy_pass_request_body on;
            proxy_set_header        Content-Length "";
            proxy_set_header        X-Original-URI $request_uri;
    proxy_set_header        X-Real-IP      $remote_addr;

    proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;

        }
        location ~* \.mp4$ {
            auth_request     /auth;
            auth_request_set $auth_status $upstream_status;
            root /upload/;
            mp4;
     mp4_buffer_size     5M;
            mp4_max_buffer_size 4096M;
   
           
    autoindex on;
            # enable thread bool
            aio threads=default;
   # limit_rate        on;
       # mp4_limit_rate_after  30s;
            # enable caching for mp4 videos
            proxy_cache mycache;
            proxy_cache_valid 200 300s;
            proxy_cache_lock on;

            # enable nginx slicing
            slice              5M;
            proxy_cache_key    $host$uri$is_args$args$slice_range;
            proxy_set_header   Range $slice_range;
            proxy_http_version 1.1;

            # Immediately forward requests to the origin if we are filling the cache
            proxy_cache_lock_timeout 0s;

            # Set the 'age' to a value larger than the expected fill time
            proxy_cache_lock_age 200s;

            proxy_cache_use_stale updating;

        }
    
    listen 8443 ssl http2;
    ssl_dhparam /etc/ssl-dhparams.pem;
    ssl_certificate /opt/ssl-bundle.crt;
    ssl_certificate_key /opt/media_private.pem;


}


}
4

0 回答 0