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

configurationnginxperformancereverse-proxyvideo streaming

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

Best Answer

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.