Emeric
Emeric

Reputation: 6915

Nginx audio files (wav/ogg/mp3) not working

Audios on prod are not working while working fine on dev environment (Angular 7).

Prod config (VPS):

AudioService:

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

CORS are enabled on Nodejs side (using Nestjs). main.ts:

app.enableCors();

Chrome log:

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

Firefox log:

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

Looking at Network console we can see myaudio.wav with:

Status Code: 206 Partial Content

Note: Loading images works fine !

EDIT:

Nginx config /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
}

On dev environment localhost:4200/assets/audio/myaudio.wav → works fine

On prod environment https://mywebsite.com/assets/audio/myaudio.wav → returns home page

While https://mywebsite.com/assets/image.jpg → works fine

Only audios don't work.

Upvotes: 1

Views: 2529

Answers (1)

Guerric P
Guerric P

Reputation: 31825

Set max_ranges to 0.

For your case, this would look like something like this:

location ~ \.wav$ {
    max_ranges 0;
}

Meaning the rule applies to every wav file regardless of their location.

Upvotes: 1

Related Questions