arut / nginx-rtmp-module

NGINX-based Media Streaming Server
http://nginx-rtmp.blogspot.com
BSD 2-Clause "Simplified" License
13.41k stars 3.51k forks source link

Web Browser Player - Cors Error #1128

Open sinanaksu opened 7 years ago

sinanaksu commented 7 years ago

Hi, The success is somehow made up of hls files (.m3u8 and .ts). I can play the broadcast with the vlc player on the desktop. But it does not work in the web browser.

I'm trying https://video-dev.github.io/hls.js/demo/ here. But he's giving a cros fault. Error and nginx.conf file as follows. I also share my ffmpeg command.

Thank you.

Web player error.

cannot Load http://xx.xx.xx.xx/mobile/123456/index.m3u8 HTTP response code:0 this might be a CORS issue, consider installing Allow-Control-Allow-Origin Chrome Extension,network error ...

ffmpeg command : nohup ffmpeg -re -i 123456.mp4 - -sn -vcodec copy -acodec copy -f flv rtmp://127.0.0.1:1935/mobile/123456 ) &> /dev/null &

Nginx.conf content

#user  nobody;
worker_processes  1;
error_log   logs/error.log  notice;
events {
    worker_connections  1024;
    multi_accept on;
    use epoll;
}

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

    access_log on;

    sendfile        off;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  off;
    tcp_nodelay on;

    server_tokens off;
    client_body_buffer_size 128k;

    server {
        listen       80;
        server_name  localhost;        
        add_header 'Cache-Control' 'no-cache';
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Expose-Headers' 'Content-Length';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Access-Control-Allow-Headers' 'Origin,Range,Accept-Encoding,Referer,Cache-Control';
        add_header 'Access-Control-Expose-Headers' 'Server,Content-Length,Content-Range,Date';
        add_header 'Access-Control-Allow-Methods' 'GET, HEAD, POST, OPTIONS, PUT, DELETE';

            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
                application/dash+xml mpd;
                video/mp4 mp4;
            }

        location / {
            root   html;
            index  index.html index.htm;
            rewrite ^(.*)$ /crossdomain.xml;
        }       

        error_page 400 /crossdomain.xml;

        location = /crossdomain.xml {
            root /usr/local/nginx/html/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
            allow 127.0.0.1;
            deny all;
        }
        location /stat.xsl {
            root /usr/local/nginx/html;
        }

        location /nclients {
            proxy_pass http://127.0.0.1/stat;
            add_header Refresh "3; $request_uri";
        }

        location /control {
            rtmp_control all;
        }

        location /mobile {
            types {
                            application/dash+xml mpd;
                            application/vnd.apple.mpegurl m3u8;
                            video/mp2t ts;
            }
            alias /HLS/mobile;
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Expose-Headers' 'Content-Length';
        }

    }

}
karlisk commented 7 years ago

I've run into issues like this - it usually depends on the web player or the web browser if they require CORS or not (in short: this problem is not caused by the nginx-rtmp-module). What you have to do is do additional configuring of NGINX itself in the HTTP blocks related to the .m3u8 playlists or the streams. Look into the following resource about CORS: https://enable-cors.org/server_nginx.html

There's also a Mozilla web resource on this topic with detailed info on this topic: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS