We’re Hiring!

New Bamboo Web Development

Bamboo blog. Our thoughts on web technology.

Upload progress with Nginx

by Damien Tanner

Last month Brice Figureau released a little Nginx module which tracks the progress of uploads going through Nginx. Like Lighttpd's mod_uploadprogress it's very nice to have the proxy take responsibility for reporting the progress.

The Nginx upload progress module adds track_uploads and report_uploads directives which do what they say on the tin. When the user submits a form to the location supporting track_upload, we also send an extra X-Progress-ID parameter with a unique id for that upload. Requests made to the location supporting report_uploads (typically /uploads) will return JSON with the current progress for an upload. The X-Progress-ID header is added to the ajax requests to identify which upload we're wanting to know the status of.

Having Nginx track the progress gets around an issue with using the merb upload progress method. Nginx (un)fortunately does not pass along the upload to its destination until it has fully completed uploading. So when you upload a file, your Merb application won't know about it until it has been upload to Nginx and passed along. The only gripe with this whole process is the possibility of a small delay as Merb handles the request once the upload is complete. If you're using Rails however this can be an advantage, as it won't be tied up by the upload as it happens in realtime.

To get this all going you'll first want to download the Nginx upload progress module and recompile Nginx with the following configure option.

--add-module=path/to/nginx_uploadprogress_module

Locate and open your nginx configuration file and add in the upload_progress, track_uploads and report_uploads directives.

If you're going to upload files of any considerable size you'll also want to the max_body_side to something reasonable.

http {
    upload_progress proxied 1m;

    server {
        listen 80;
        server_name _ *;
        root /usr/local/www;

        location / {
            proxy_pass http://127.0.0.1:4000;
            track_uploads proxied 30s;
            client_max_body_size 500m;
        }

        location ^~ /progress {
            report_uploads proxied;
        }
    }
}

Nginx is now ready to track uploads! Generate a Merb project and add a new upload controller.

 1 class Uploader < Application
 2     def index
 3         render
 4     end
 5 
 6     def upload
 7         FileUtils.mv params[:file][:tempfile].path, \
 8 MERB_ROOT+"/files/#{params[:file][:filename]}"
 9     end
10 end

Then add the upload view.

 1 <div id="pandaloader">
 2     <div id="uploader">
 3         <form id="upload" enctype="multipart/form-data" \
 4 action="/uploader/upload" method="post">
 5             <input name="file" type="file">
 6             <input type="submit" value="Upload">
 7         </form>
 8     </div>
 9 
10     <div id="uploading">
11         <div id="progress" class="bar">
12             <div id="progressbar">&nbsp;</div>
13         </div>
14     </div>
15 </div>

In the header of your application's layout you'll also need to include the javascript and css.

1 $(function() {
2         $('#upload').nginxUploadProgress();
3     });

Aside from jQuery, you'll notice the jquery.nginxUploadProgress.js plugin which has been included. This is a little jQuery library which will query Nginx for the upload's progress and update the length of the progress bar accordingly. Grab the file here: jquery.nginxUploadProgress.js

Here is some styling for the progress bar.

 1 .bar {
 2   width: 300px;
 3 }
 4 
 5 #progress {
 6   background: #eee;
 7   border: 1px solid #222;
 8 }
 9 #progressbar {
10   width: 0px;
11   height: 24px;
12   background: #333;
13 }

Put all these together and you'll have a great little progress bar for uploads!