NGINX Reverse Proxy, Config Video(MJPG) Stream to use a single connection to the Backend Server

7,275

Solution 1

Nginx will always create new proxy connection for incoming connection unless it is cached localy. As MJPG is an infinite loop, it doesn't seem as an option here.

What I would however try is setup a ffmpeg/vlc on the proxy server to consume mjpg stream from rpi. Nginx then can connect to local ffmpeg/vlc to get stream. You will then have only one connection to the backend server.

Solution 2

As @Yarik has answered, this is currently not possible with how Nginx handles the proxy. You need some system that is designed to take the one stream and can rebroadcast that to however many clients would like it.

There are some other projects that are designed to fill this void. You could always run one of these behind Nginx on the same server. Nginx will still see individual connections for each client, and forward those to the broadcaster, but the broadcaster will only ever load up the camera with one client.

Share:
7,275

Related videos on Youtube

Flo
Author by

Flo

Updated on September 18, 2022

Comments

  • Flo
    Flo over 1 year

    I have a question/problem weather it is possible to configure a NGINX Reverse Proxy so that it does not Proxy every Request of a Video Stream to the Backend Server, instead Opening a Single Connection to get the Video stream and distribute it to the Clients. Right now i use following configuration.

    server {                                                                        
          listen 9000 ssl;                                                              
          server_name some.domain www.some.domain;                                        
          error_log /var/log/nginx/error.cam.log;                                       
          access_log /var/log/nginx/access.cam.log;                                     
                                                                                    
          ssl on;                                                                         
          ssl_certificate /etc/letsencrypt/live/some.domain/fullchain.pem;        
          ssl_certificate_key /etc/letsencrypt/live/some.domain/privkey.pem;       
                                                                                    
          location / {                                                              
                                                                                    
            auth_basic "Login";                                       
            auth_basic_user_file /etc/nginx/auth/Somefile;   
            proxy_pass http://XXX.XXX.XXX.XXX:9000/;                                  
            proxy_http_version 1.1;                                                 
            proxy_buffering off;        
                                                                                                                    
          }                                                                                                                                                                                                                                
    }  
    
        
    

    Which leads to a new Backend to NGINX Connection with the exact same Video Stream everytime a Client Requests requests that site from the NGINX server. Network Activity Backend=Green , NGINX=Blue Above Graph shows the Outgoing Network traffic: Green=Nginx Server and Blue=Backend Server. Every Peak means a new client accessing the Video MJPG stream.

    So the Problem is i have a verry limited Performance from Backend to the NGINX Proxy because the Backend is a Raspberry Pi which cant deliver more that 30Mbit stable stream. I already experimented with buffering etc. but had no success. Is there any method to use a single Connection to the Backend?

    Thanks and Regards from Germany, Flo

  • Flo
    Flo almost 8 years
    Thank you for your respons, in my case the (live)stream is a Network camera producing a Mjpg (en.wikipedia.org/wiki/Motion_JPEG) so its an infinite video stream once startet upon closing the connection thats why i dont know how to cache it :)
  • Yarik Dot
    Yarik Dot almost 8 years
    I see, sorry I missed that information. Check the answer again. Hopefully you will find it useful.
  • Flo
    Flo almost 8 years
    Wow thank you , that was the information/hint i needet :)
  • jannej
    jannej over 2 years
    Just curious, 5 years later ;-)...How did you solve it?