Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use nginx to reverse-proxy an IP camera's mjpeg stream?

I'm using nginx on OpenWRT to reverse-proxy a motion-jpeg feed from an IP camera, but I'm experiencing lag of up to 10-15 seconds, even at quite low frame sizes and rates. With the OpenWRT device removed from the path, the camera can be accessed with no lag at all.

Because of the length of the delay (and the fact that it grows with time), this looks like some kind of buffering/caching issue. I have already set proxy_buffering off, but is there something else I should be watching out for?

Thanks.

like image 974
mikepurvis Avatar asked Sep 19 '25 00:09

mikepurvis


1 Answers

I installed mjpg-streamer on an Arduino Yun, and then in my routers settings setup port forwarding whitelisted to my webserver only.

Here is my Nginx config which lives in the sites-enabled directory.

server {
  listen      80;
  server_name cam.example.com;
  error_log /var/log/nginx/error.cam.log;
  access_log /var/log/nginx/access.cam.log;

  location    / {
    set $pp_d http://99.99.99.99:9999/stream_simple.html;
    if ( $args = 'action=stream' ) {
      set $pp_d http://99.99.99.99:9999/$is_args$args;
    }
    if ( $args = 'action=snapshot' ) {
      set $pp_d http://99.99.99.99:9999/$is_args$args;
    }

    proxy_pass $pp_d;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
  }
}
like image 65
stenius Avatar answered Sep 21 '25 14:09

stenius