Need feedback on a small change to the nginx-gridfs module

Srirang Doddihal om.brahmana at
Sun Mar 4 21:04:58 UTC 2012


I have made a small tweak to the nginx-gridfs module ( to serve my requirement. I am not
sure if it is generic enough to raise a pull request. So I thought of
posting it here and asking for some feedback.

The code changes are related more to MongoDB and GridFS but still it would
be very helpful if the nginx gurus here can review the code from the nginx
module angle and give me some feedback.

Here is a description about my requirement and my change. Or you can skip
right ahead and go look at the code at :

Setup :
 - Rails app accepts user uploaded images and stores them in GridFS, with
unique file names
 - Nginx uses the nginx-gridfs module to serve these files from GridFS

  - Multiple versions (sizes) of every uploaded image needs to exist - each
addressed by the same (or similar) url with a query parameter specifying
the version.
  - Creating versions during the upload request results in bad user
experience (too much time) and hence needs to be done by a background
process - using a queuing server. This means additional versions are not
available right away.
  - The clients would not know when these versions are available and would
get 404 when they try to fetch a non-default version of the image.
  - So the nginx-gridfs module needs to be smart enough to fallback to the
default version when the requested version is not available.
    i.e. Look for the specified version. If present serve it. Else fetch
the default version and serve that.

I have a very rudimentary implementation of this. It compiles and works
fine - no crashes, doesn't seem to leak memory. I haven't done any
benchmarking, but it seems do just fine under simple loads.

Here is how the different versions of the image files are stored and
addressed :

The value of the 'type' query parameter is added as a suffix to the
filename before searching.
  Ex : original filename - 'images/4f4292ff1d41c80be4000013' and is
requested as 'http://hostname/files/images/4f4292ff1d41c80be4000013'
  Small version of the same image will be at :
'images/4f4292ff1d41c80be4000013_small' or
and is requested as '
http://hostname/files/images/4f4292ff1d41c80be4000013?type=small' or '

If the requested version is not found, then we fallback to the default
version, i.e. the one without any suffixes (a.k.a
'images/4f4292ff1d41c80be4000013') and serve that one.

Few notes about the changed code :
1) It assumes that the field type to be queried against on a String type
2) It assumes that the gridfs and gridfile are reusable types - i.e same
object can be used across multiple queries.
3) It is probably not super optimized, but that is ok for now.

And here is the code commit on for this change -

It would be very helpful to get some feedback from you guys.

Srirang G Doddihal
@sribrahmana -

The LIGHT shows the way.
The WISE see it.
The BRAVE walk it.
The PERSISTENT endure and complete it.

I want to do it all ALONE.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list