Search code examples
nginxstreamstreamingbroadcasticecast

Icecast2 running under nginx not able to connect


I want to start saying that I've looked all over the place to find an answer to this problem and it just seems like either nobody else ran into this problem or nobody is doing it. So, I recently install icecast2 on my Debian server, The thing is that I'm completely able to broadcast to my server from my local network connecting to its local IP on port 8000 and hear the stream over the internet on radio.example.com since I proxy it with nginx, so far no problems at all. The problem lies when I want to broadcast to the domain I gave with nginx stream.example.com

I have two theories, one is that the proxy is not giving the source IP to icecast so it thinks it's beign broadcasted from 127.0.0.1 and the other is that nginx is doing something strange with the data stream and thus not delivering the correct format to icecast.

Any thoughts? Thanks in advance!

Here is the nginx config

server {
    listen 80;
    listen [::]:80;
    server_name radio.example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP $remote_addr;


    location / {
            proxy_pass http://127.0.0.1:8000/radio;
            subs_filter_types application/xspf+xml audio/x-mpegurl audio/x-vclt text/css text/html text/xml;
            subs_filter ':80/' '/' gi;
            subs_filter '@localhost' '@stream.example.com' gi;
            subs_filter 'localhost' $host gi;
            subs_filter 'Mount Point ' $host gi;
    }
}

server {
    listen 80;
    listen [::]:80;
    server_name stream.example.com;

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP $remote_addr;
    location / {
            proxy_pass http://localhost:8000/;
            subs_filter_types application/xspf+xml audio/x-mpegurl audio/x-vclt text/css text/html text/xml;
            subs_filter ':8000/' ':80/' gi;
            subs_filter '@localhost' '@stream.example.com' gi;
            subs_filter 'localhost' $host gi;
            subs_filter 'Mount Point ' $host gi;
    }
}

And this is what I get on icecast error.log

[2018-08-10  14:15:45] INFO source/get_next_buffer End of Stream /radio
[2018-08-10  14:15:45] INFO source/source_shutdown Source from 127.0.0.1 at "/radioitavya" exiting

Solution

  • Not sure how much of this is directly relevant to the OP's question, but here's a few snippets from my config.

    These are the basics of my block to serve streams to clients over SSL on port 443.

    In the first location block any requests with a URI of anything other than /ogg, /128, /192 or /320 are rewritten to prevent clients accessing any output from the Icecast server other than the streams themselves.

    server {
      listen 443 ssl http2;
      server_name stream.example.com;
      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
      location / {
        rewrite  ~*(ogg) https://stream.example.com/ogg last;
        rewrite  ~*([0-1][0-5]\d) https://stream.example.com/128 last;
        rewrite  ~*(?|([1][6-9]\d)|([2]\d\d)) https://stream.example.com/192 last;
        rewrite  ~*([3-9]\d\d) https://stream.example.com/320 break;
        return  https://stream.example.com/320;
      }
    
      location ~ ^/(ogg|128|192|320)$ {
        proxy_bind $remote_addr transparent;
        set $stream_url http://192.168.100.100:8900/$1;
        types        { }
        default_type audio/mpeg;
        proxy_pass_request_headers on;
        proxy_set_header Access-Control-Allow-Origin *;
        proxy_set_header Host $host;
        proxy_set_header Range bytes=0-;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        tcp_nodelay on;
        proxy_pass $stream_url;
      }
    }
    

    Setting proxy_bind with the transparent flag:

    allows outgoing connections to a proxied server originate from a non-local IP address, for example, from a real IP address of a client

    This addresses the issues of local IP addresses in your logs/stats instead of client IPs, for this to work you also need to reconfigure your kernel routing tables to capture the responses sent from the upstream server and route them back to Nginx.

    This requires root access and a reasonable understanding of Linux networking configuration, which I appreciate not everyone has. I also appreciate not everyone who uses Icecast and might want to reverse proxy will read this. A much better solution would be making Icecast more Nginx friendly, so I had a go.

    I cloned Icecast from github and had a look over the code. I've maybe missed some but these lines looked relevant to me:

    ./src/logging.c:159:  client->con->ip,
    ./src/admin.c:700:    xmlNewTextChild(node, NULL, XMLSTR(mode == OMODE_LEGACY ? "IP" : "ip"), XMLSTR(client->con->ip)); 
    

    For servers which do not support the PROXY protocol the Nginx default method of passing the client IP upstream is via the X-Real-IP header. Icecast seems to be using the value of client->con->ip for logging listener IPs. Let's change things up a bit. I added this:

    const char *realip;
    realip = httpp_getvar (client->parser, "x-real-ip");
    if (realip == NULL)
      realip = client->con->ip;
    

    And changed the previous lines to this:

    ./src/logging.c:163:  realip,
    ./src/admin.c:700:    xmlNewTextChild(node, NULL, XMLSTR(mode == OMODE_LEGACY ? "IP" : "ip"), XMLSTR(realip));
    

    then I built Icecast from source as per the docs. The proxy_set_header X-Real-IP $remote_addr; directive in my Nginx conf is passing the client IP, if you have additional upstream servers also handling the request you will need to add some set_real_ip_from directives specifying each IP, real_ip_recursive on; and use the $proxy_add_x_forwarded_for; which will capture the IP address of each server which handles the request.

    Fired up my new Icecast build and this seems to work perfectly. If the X-Real-IP header is set then Icecast logs this as the listener IP and if not then it logs the client request IP so it should work for reverse proxy and normal setups. Seems too simple, maybe I missed something @TBR?

    OK so you should now have working listener streams served over SSL with correct stats/logs. You have done the hard bit. Now lets stream something to them!

    Since the addition of the stream module to Nginx then handling incoming connections is simple regardless of whether of not they use PUT/SOURCE.

    If you specify a server within a stream directive Nginx will simply tunnel the incoming stream to the upstream server without inspecting or modifying the packets. Nginx streams config lesson 101 is all you need:

    stream {
    
      server {
        listen pub.lic.ip:port;
        proxy_pass ice.cast.ip:port;
      }
    }
    

    I guess one problem unsuspecting people may encounter with SOURCE connections in Nginx is specifying the wrong port in their Nginx config. Don't feel bad, Shoutcast v1 is just weird. Point to remember is:

    • Instead of the port you specify in the client encoder it will actually attempt to connect to port+1

    So if you were using port 8000 for incoming connections, either set the port to 7999 in client encoders using the Shoutcast v1 protocol, or set up your Nginx stream directives with 2 blocks, one for port 8000 and one for port 8001.

    Your Nginx install must be built with the stream module, it's not part of the standard build. Unsure? Run:

    nginx -V 2>&1 | grep -qF -- --with-stream && echo ":)" || echo ":("
    

    If you see a smiley face you are good to go. If not you'll need to build Nginx and include it. Many repositories have an nginx-extras package which includes the stream module.

    Almost finished, all that we need now is access to the admin pages. I serve these from https://example.com/icecast/ but Icecast generates all the URIs in the admin page links using the root path, not including icecast/ so they won't work. Let's fix that using the Nginx sub filter module to add icecast/ to the links in the returned pages:

    location /icecast/ {
      sub_filter_types text/xhtml text/xml text/css;
      sub_filter 'href="/'  'href="/icecast/';
      sub_filter 'url(/'  'url(/icecast/';
      sub_filter_once off;
      sub_filter_last_modified on;
      proxy_set_header Accept-Encoding "";
      proxy_pass http://ice.cast.ip:port/;
    }
    

    The trailing slash at the end of proxy_pass http://ice.cast.ip:port/; is vitally important for this to work.

    If a proxy_pass directive is specified just as server:port then the full original client request URI will be appended and passed to the upstream server. If the proxy_pass has anything URI appended (even just /) then Nginx will replace the part of the client request URI which matches the location block (in this case /icecast/) with the URI appended to the proxy_pass. So by appending a slash a request to https://example.com/icecast/admin/ will be proxied to http://ice.cast.ip:port/admin/

    Finally I don't want my admin pages accessible to the world, just my IP and the LAN, so I also include these in the location above:

    allow 127.0.0.1;
    allow 192.168.1.0/24;
    allow my.ip.add.ress;
    deny all;
    

    That's it.

    sudo nginx -s reload
    

    Have fun.