# Beginner developer question: streaming mjpeg in nginx

**URL:** <https://www.ruby-forum.com/t/beginner-developer-question-streaming-mjpeg-in-nginx/216578>\
**Category:** NGINX\
**Created:** [February 8, 2012, 12:30pm UTC](https://www.ruby-forum.com/t/beginner-developer-question-streaming-mjpeg-in-nginx/216578 "2012-02-08T12:30:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 8, 2012, 12:30pm UTC](https://www.ruby-forum.com/t/beginner-developer-question-streaming-mjpeg-in-nginx/216578/1 "2012-02-08T12:30:41Z")

</div>

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.
