9

I am primarily a front-end developer/designer, however recently, I've been exploring end to end solutions. Yesterday I finished a TODO application using the mean stack and would like to start exploring deployment options to my VPS.

That being said, I've been advised to use nginx as a reverse proxy is for serving up static resources? Unfortunately, I'm getting stuck on simple questions.

What are the example static resource?

What factors define static resources?

What are examples of non-static resources?

Lastly, are there any weird edge-cases I should be aware of?

Sorry about the noobness of this question.

Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

4 Answers4

13

In this case, a static resource refers to one that is not generated with code on the fly, meaning that its contents won't change from request to request.

Images, JavaScript, CSS, etc., are all candidates for this. Basically, you set a large cache time for these resources, and your Nginx servers can keep a copy on disk (or in Redis or something similar) so that they are ready to return to the client without hitting your application servers.

It's important to remember to use versioned file names when setting large cache times. header-image-20140608.png for example, means you can have a later version without worrying about the old one still being in the cache.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • What would be example of dynamic resources in a single page application? – Armeen Moon Jun 08 '14 at 17:16
  • 1
    @MatthewHarwood Possibly nothing, but if you are talking to a back-end API, then any call you make to that API is a request to a dynamic resource. – Brad Jun 08 '14 at 17:16
  • Yeah thats why I'm getting confused. Like some people have said that the nodeserver should handle only websockets. but it feels like nginx does everything? – Armeen Moon Jun 08 '14 at 17:18
  • @MatthewHarwood Who said Node should only handle Websockets? The important thing is to let your application server (your code on Node.js) do what it needs for the application, and any general web serving tasks left up to a general web server (Nginx). It makes little sense to serve up your images and CSS from Node.js (unless of course you're generating those on the fly programmatically, but most don't) because Node.js isn't very efficient at serving static resources when compared to a fully compiled web server like Nginx. – Brad Jun 08 '14 at 17:20
  • @MatthewHarwood Your application server is what does all the work for your application. Your web server is what sends data to clients. Just because Node.js speaks HTTP doesn't mean it should be used for general web serving. HTTP is a convenient transport for APIs and what not, which is why it is available. While there is nothing stopping you from serving static resources from Node.js... why would you want to? And, do proxy your API calls to Node.js through Nginx. Nginx is better at dealing with slow clients. Keep the load off Node where it makes sense to do so. – Brad Jun 08 '14 at 17:21
  • This was a better answer. Is it safe to say a dynamic resource is anything you wouldnt want to cache? – Armeen Moon Jun 08 '14 at 17:59
  • @MatthewHarwood No. You certainly **can** cache dynamic resources. For instance, I have a web service available where folks can check the current artist and title on internet radio stations. This is dynamically generated upon request, but if my users were to get a ton of traffic and hit it thousands of times per second, there is no reason for my server to check what's playing on that stream thousands of times per second. So, I set a cache time of 5 seconds and let Nginx handle it. My Node.js box only gets hit a maximum of once every 5 seconds, and I can handle a large load. – Brad Jun 08 '14 at 18:01
  • @MatthewHarwood It is typical to not cache dynamic resources (since many of them are one-time use or return data for a specific user), but there is nothing stopping you from having them cached. – Brad Jun 08 '14 at 18:02
  • last question I have a request to get all awesome things that is stored in my MONGO DB would that be served up by express or nginx it's just a json object of strings no images $http.get('/api/awesomeThings').success(function(awesomeThings) { $scope.awesomeThings = awesomeThings; }); – Armeen Moon Jun 08 '14 at 18:04
  • @MatthewHarwood That would be served up by your Node.js server, and you would be proxying to that Node.js server with Nginx. Nginx doesn't know how to talk to MongoDB. (That being said, there is some very interesting stuff folks are doing with the OpenResty bundle of Nginx... but it's a real hassle to work with.) – Brad Jun 08 '14 at 18:07
6

A static resource is something that isn't generated dynamically.

An example of a static resource is an image. It's the same for each and every request. It's a file on the filesystem that doesn't require any processing - you simply tell nginx send this file as-is to the user.

An example of a dynamic resource is json data specific to the user requesting it (it has to be generated specifically for that user).

With a dynamic resource these is also often your own domain specific code executed, a request to the database etc.

The reason nginx should serve static content is because it excels at serving this content in a parallel way - it was designed exactly for this.

If you are using Ruby/Python/node.js/Java etc, you can also serve static resources through these processes (Just call File.open() and start streaming the data) - however it would be much slower, and also lower the number of simultaneous dynamic requests you could serve.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • so what about serving up the index.html is that a static asset? – Armeen Moon Jun 08 '14 at 17:14
  • Yes index.html is a static asset. If it's just a file on the filesystem that can be sent directly to the client it's static. – Martin Konecny Jun 08 '14 at 17:18
  • So I guess the better question would be what would be a real usecase example of express serving up resource over nginx. could you just give me a real example and I'll mark correct? – Armeen Moon Jun 08 '14 at 17:20
  • 1
    @MatthewHarwood You would use Express to serve up responses to APIs, or anything else you need generated in your application. – Brad Jun 08 '14 at 17:22
  • 2
    You wouldn't use Express to serve up an nginx resource. What happens is that nginx is controlling what requests hit Express (it's in front of express). So you configure nginx to intercept any requests to the `images` directory for example (since these are static). For any other directory, you tell nginx to delegate/proxy to Express. – Martin Konecny Jun 08 '14 at 17:25
1
  • A static resource is resource which will not be changed frequently and this can be stored on client's browser end unless required , to prevent load on web server and loading the site faster at client end.
  • Some examples of these are : images, javascript, css
  • A dynamic resource is the content that changes on a web resource which is mainly data that keeps changing on a page which is specific to a user or items.
  • In order to make sure that your static data reduces the load on your server and ensures fast performance on client end you need to take care of various server specific configurations like enabling the compressing of js files , rendering the header content for images properly.

When you want to change the file content make sure you prevent the browser from picking this static old content from cache, attach a time stamp with the urls of these static resources which will ensure upldated resource is loaded when needed

Chhavi Gangwal
  • 1,166
  • 9
  • 13
0

Static resources mean resources that don't change and do not involve server-side code.

This typically means images, CSS, and somethimes client-side Javascript.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964