javascript - Nginx音频文件(wav/ogg/mp3)无法正常工作

标签 javascript node.js nginx audio vps

在开发环境中正常工作时,产品上的音频无法正常工作(Angular 7)。

产品配置(VPS):

  • Ubuntu 18的
  • Nginx
  • 让我们加密

  • 音频服务:
    export class AudioService {
    
        audio = new Audio();
    
        constructor() { }
    
        isPlaying() {
            return this.audio.currentTime > 0 && !this.audio.paused && !this.audio.ended && this.audio.readyState > 2;
        }
    
        play(name: string): void {
            this.audio.src = `assets/audio/${name}`;
            this.audio.crossOrigin = 'anonymous';
            this.audio.load();
    
            if (!this.isPlaying()) {
                this.audio.play();
            }
        }
    
        pause(): void {
            if (this.isPlaying()) {
                this.audio.pause();
            }
        }
    }
    

    在Nodejs端启用了CORS(使用Nestjs)。 main.ts :
    app.enableCors();
    

    Chrome浏览器日志:

    Uncaught (in promise) DOMException: Failed to load because no supported source was found.



    Firefox日志:

    NotSupportedError: The media resource indicated by the src attribute or assigned media provider object was not suitable.



    查看网络控制台,我们可以看到myaudio.wav与:

    Status Code: 206 Partial Content



    注意:加载图像效果很好!

    编辑:

    Nginx配置 / etc / nginx / sites-available / mywebsite :
    # Redirection
    server {
       # if ($host = mywebsite.com) {
       #     return 301 https://$host$request_uri;
       # } # managed by Certbot
    
        listen 80;
        listen [::]:80;
        server_name mywebsite.com www.mywebsite.com;
        return 301 https://$host$request_uri;
        #return 404; # managed by Certbot
    
    }
    
    # Config
    server {
         server_name mywebsite.com www.mywebsite.com;
    
         root /home/foo/mywebsite/gui;
    
         index index.html index.htm;
    
         location / {
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-NginX-Proxy true;
           proxy_pass    http://my.ip:3000/;
    
           # Websocket
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "upgrade";
        }
    
        if ($host = 'www.mywebsite.com') {
           return 301 https://mywebsite.com$request_uri;
        }
    
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
    }
    

    在开发环境上localhost:4200 / assets / audio / myaudio.wav→正常运行

    在生产环境中https://mywebsite.com/assets/audio/myaudio.wav→返回主页

    虽然https://mywebsite.com/assets/image.jpg→正常

    仅音频无效。

    最佳答案

    max_ranges 设置为0。

    对于您的情况,这看起来像这样:

    location ~ \.wav$ {
        max_ranges 0;
    }
    

    这意味着该规则适用于每个wav文件,无论它们位于何处。

    关于javascript - Nginx音频文件(wav/ogg/mp3)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576313/

    相关文章:

    django - gunicorn:错误(无此类文件)nginx + gunicorn +主管

    ssl - 带有 OpenSSL 1.0.2 的最新 Nginx 仍然无法提供 http/2

    javascript - 如何使用SASS mixin组合CSS类?

    javascript - angularjs 在 html 元素上动态设置 css

    javascript - 计算函数执行时间

    angularjs - 用户注册无效并通过 OTP 激活

    ubuntu - nginx 已启动但端口 80 未在 AWS 上使用(根据 netstat)

    javascript - 向场景添加灯光没有效果

    javascript - Jquery 不适用于 html 文件,但适用于 .js 文件

    node.js - Express REST API 不返回整数数组