Serve static assets and media separately from meteor with nginx? Serve static assets and media separately from meteor with nginx? nginx nginx

Serve static assets and media separately from meteor with nginx?


If all your static assets have a common path, for instance /static/..., then you can tell nginx to alias requests to that path with a directory

location /static  {    alias /path/to/static/assets;}


You can set up a file server within your meteor app to serve them using the node fs module then serve using something like Picker or there's an example of adding a connect handler to Meteor here if that doesn't work out.

Meteor will ignore hidden directories by default so you could put the assets inside your app directory if you want in something like .assets/, otherwise anywhere on the filesystem that the user running meteor can access.

Here's a working server for text files that I just threw together. You'll need to do meteor add meteorhacks:picker first:

if (Meteor.isServer) {  var fs = Npm.require('fs');  Picker.route('/asset/:name', function(params, req, res) {    fs.readFile('/home/users/someuser/media/' + params.name, function(err, file) {      res.writeHead(200, {'Content-Type': 'text/plain'});      res.end(file.toString());    });  });}


If you would want this for performance and scalability reasons (see my comment) you should go with a Caching Proxy. Initially the proxy will fetch a static file from the Meteor app directly, but subsequent requests will be served from its cache.

This article (nginx-caching) is a good read about how to set-up a caching proxy with Nginx in general. Also, Meteorpedia has an article explaining how to setup an Nginx cache specifically tailored for a Meteor app.