App crashes with Nginx 504 gateway timeout error, for database


I have a meteor app deployed on Nginx server with for Database.

Can adding template level subscriptions or Mongo.collection (on client) cause 504 error?
Any suggestions on how to go about solving 504 error with nginx and

To improve the performance of the app, I added template level subscriptions and also used client side collections using Mongo.collection. The performance improved considerably (response time) but now my app crashes and gives 504 timeout error, after about 2 weeks or so. Here is what my nginx log looks like during that error.

2017/02/23 03:04:45 [error] 4539#0: *15640 upstream timed out (110: Connection timed out) while reading response header from upstream, 

In app logs only error i see is.

TypeError: Object.keys called on non-object
    at Function.keys (native)
    at [object Object].cursorProto.(anonymous function) (packages/meteorhacks_kadira.js:3020:42)
    at [object Object].kadira_Cursor_observeChanges [as observeChanges] (packages/meteorhacks_kadira.js:3401:41)
    at [object Object].override._depend (packages/lepozepo:reactive-publish/lib/
    at [object Object].override.fetch (packages/lepozepo:reactive-publish/lib/
    at [object Object].cursorProto.(anonymous function) (packages/meteorhacks_kadira.js:3008:32)
    at [object Object].kadira_Cursor_fetch [as fetch] (packages/meteorhacks_kadira.js:3389:32)
    at [object Object].Meteor.reactivePublish.Meteor.users.findOne._id (app/server/publications.js:84:86)
    at Computation.<anonymous> (packages/lepozepo:reactive-publish/lib/
    at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)

I had to do mup restart everytime. I have now increased the limits of proxy timeout by adding the following to the nginx.conf.

  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;

This got rid of 504 for now but at times app does not open for a long time.

Only abnormal thing i have recently started seeing on kadira is high response time on fetch operations. This happened after 504 time out error. Before that everything showed normally on kadira.

Another abnormal thing I see on compose is GBs of space being taken by CollectionFS Temp Store.

So basically my question is, can adding template level subscriptions or Mongo.collection on client cause 504 error?

This is my first time working with compose, could it be something to do with compose?

How should I go about solving this error?