I have made a small tweak to the nginx-gridfs module (
GitHub - mdirolf/nginx-gridfs: Nginx module for serving files from MongoDB's GridFS) to serve my requirement. I am
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
be very helpful if the nginx gurus here can review the code from the
module angle and give me some feedback.
Here is a description about my requirement and my change. Or you can
right ahead and go look at the code at :
- Rails app accepts user uploaded images and stores them in GridFS,
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 -
addressed by the same (or similar) url with a query parameter specifying
- 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
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
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
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 :
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 :
- It assumes that the field type to be queried against on a String type
- It assumes that the gridfs and gridfile are reusable types - i.e same
object can be used across multiple queries.
- It is probably not super optimized, but that is ok for now.
And here is the code commit on github.cm for this change -
It would be very helpful to get some feedback from you guys.
Srirang G Doddihal
@sribrahmana - http://www.twitter.com/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.