I am going to deploy my node.js application.
I can start my own web application with purely node.js only.
But my fd told me that it is better to serve that web app under apache or nginx.
Anyone have such kind of experience, why do we need apache as I can start my web app on node.js+ express only?
I would like to know more on deployment knowledges. Thanks for any help.
Kit
Putting Apache in front of Node is not typical in a greenfield app. The only case I can grant to this is if your company has an existing investment in an Apache based infrastructure (monitoring/security/routing/caching etc..) on the frontend and the sysadmin insist on this setup.
Some folks prefer to put nginx or haproxy in front to manage routing rules, so they can channel requests for static files (assets) away from Node.js (node.js wasn't always performant when handling static files), or do fancy load balancing or failover. In the early days of 0.2.x etc... even Ryan Dahl advocated running something in front of Node.js for security mainly; although, I don't think any significant issues were discovered. I personally run nginx in front of Node.js as we have several sites and services that hit the frontend shared IP which we proxy back to various node instances listening on different internal ports.
Nginx is better suited than Apache as it is light and single threaded vs Apache thread per request (in most normal setups). But nowadays there's even a reliable (node-http-proxy excluded) frontend Node.JS based proxy http://www.github.com/substack/bouncy which one of the celebrity node.js developers uses/will-use to frontend his PaaS.
First of all, yes use nginx not Apache - it's far easier to configure nginx, and it's lighter and more efficient.
With an nginx proxy up front you get several advantages:
And probably other things I've missed.
Once really nice thing nginx can do is the "try_files" directive, which will look for local files first, and if it doesn't find them it passes off to the Node backend.
You don't want to use Apache because Nginx is better suited, since Nginx is built for async I/O. You want to use Nginx as a forward proxy service that will point your clients to the actual node.js web servers. This allows for horizontal scaling as your application grows to deal with increased load. So if you outgrow your first Nginx server, you can install another one and another... You'll also be able to do the same with your Node.js web servers.
client web browser <--> Nginx <--> Express app.js
You will also be able to serve static content faster if you serve from nginx and then use Express for your dynamic content. For deployment, you might want to write an sh script to just copy and run your Express server like normal and run your Nginx server like normal, but with a forwarding proxy setup. You could use a script in Nginx like this:
upstream your_domain_app {
server 127.0.0.1:8000;
}
server {
listen 0.0.0.0:80;
server_name your_domain.com your_domain;
access_log /var/log/nginx/your_domain.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://your_domain_app/;
proxy_redirect off;
}
}
Caching and Superior Request handling abilities of webserver like nginx make them a killer choice to be used as proxy servers in front of nodejs easing off load from them.
Also of serving static files using webservers are very common instead of nodejs due to above mentioned reasons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.