Reputation: 2361
I am developing app in nodejs with expressJS and for performance optimization, I have decided to use Nginx with NodeJS (Based on my conclusion, after reading lots of articles on Nginx + NodeJS).
I have divided tasks for Nginx and NodeJS as follow :
Nginx
NodeJS
Is it good idea to design app in above way?
If yes then how to handle authentication and authorization in Node JS app using Nginx?
If no then what is better way to handle load in NodeJS app?
Upvotes: 3
Views: 2953
Reputation: 3137
Your concept is fundamentally flawed. Nginx is fast at what its meant to do, serving content over http. Putting nginx infront of node.js doesn't provide any performance improvements. Nginx can be used as a load balancer to split traffic evenly among several hosts, but in itself doesn't provide any performance benefits except for the fact that its very efficient. That said Nginx is not meant to handle authorizaton/authentication except for maybe http basic auth.
You should be writing your own logic to handle authentication and authorization. To scale you should write your code statelessly, so that you can run across multiple servers. So you should either store session data in a place such as memcache or create a stateless system using something like JWT.
Additionally, you shouldn't be serving content with node.js. Nginx is faster at this. So only serve templates with Node.js, allow Nginx to cache and serve static content. This paradigm also allows you to leverage a content distribution network. This is the only real way to leverage what nginx is good at, serving content.
Optimization isn't about making things go super fast. Optimization is something that occurs when you run into a performance barrier or need to make a system more efficient to prevent premature scaling.
Upvotes: 4