Beginner developer question: streaming mjpeg in nginx

Hi to all,

I’d like to stream live, continuous Motion JPEG throught nginx web
server. Right now, I have a little program that captures the frames from
my hardware, separates them with boundaries and send them to stdout.
Here’s, schematically, what my program does:

// As soon as someone connects

fprintf (stdout, “Connection: close”
“\r\n”
“Cache-Control: no-store, no-cache, must-revalidate,
pre-check=0, post-check=0, max-age=0”
“\r\n”
“Pragma: no-cache”
“\r\n”
“Content-Type: multipart/x-mixed-replace;boundary=”
BOUNDARY
“\r\n”
“\r\n”
“–” BOUNDARY “\r\n”);

[…]

while (1) {

 //Magically take the frame from the shared memory

 fprintf(stdout, "Content-Type: image/jpeg\r\n" \
                 "Content-Length: %d\r\n" \
                 "\r\n", frame_length);

 DBG("Sending MJPEG frame\n");

 if ((fwrite(frame_data, frame_length, sizeof (char) , stdout)) == 0 

) {
ERR (“Error sending frame”);
break;
}

 DBG("Sending boundary\n");

 fprintf(stdout, "\r\n--" BOUNDARY "\r\n");
 }

I’ve seen examples of nginx modules that stream h264 files, but this
case is quite different: live streaming means a potentially infinite
MJPEG stream and no buffering done by the webserver.

I’m an nginx beginner, so I’m asking for some directions.

How can I stream this (potentially infinite) series of frames? A CGI is
sufficient o will I need to write a module? Is it even possible to do
something like this with nginx?

Thanks,
Cristiano.