Why do we need apache under Node.js express web framework?

15,766

Solution 1

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.

Solution 2

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:

  • Ability to run several backends for different parts of your site
  • A faster static file server
  • Clean restarts if you need to take down your Node server (nginx can deliver a pretty "under maintenance" page)
  • Logging in your node app can focus on debugging, and nginx can log requests - keeps it nice and clean

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.

Solution 3

Reasons for using an webserver like apache or in most cases nginx are:

  1. Load Balancing - It is very common practise to use web-server in front of a application/web server for load balancing
  2. Caching - Webserver like nginx provide extensive caching abilities compared to that of nodejs or other such application servers
  3. Request handling - Pure webserver like nginx is far superior in terms of handling multiple request at a time i.e high concurrency.

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.

Solution 4

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;
    }
 }
Share:
15,766
TheOneTeam
Author by

TheOneTeam

I am an software engineer.

Updated on June 05, 2022

Comments

  • TheOneTeam
    TheOneTeam almost 2 years

    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

  • TheOneTeam
    TheOneTeam about 12 years
    May I ask what does horizontal scaling means? And why static files serve slower in Node.js framework???
  • TheOneTeam
    TheOneTeam about 12 years
    Still confusing, what does the proxy server serve for in front of Node.js? Can you elaborate more on the use of nginx??
  • EhevuTov
    EhevuTov about 12 years
    @KitHo as your load grows, you can add new Nginx servers and point those to new Node.js web servers of yours. Horizontal means you add more servers. Vertical would mean you upgrade to a faster server. Horizontal is looked at as generally better. You can also Google Nginx load balancing.
  • Mâtt Frëëman
    Mâtt Frëëman about 12 years
    One of the possible reasons is that public IP addresses are usually in limited availability, and you want to run all services on either 80 or 443 (for firewall constraints). So unless you proxy requests back to internal ports that 32GB quad core server is going to under utilized if you can only run one or two services that perhaps arent very active.
  • Mâtt Frëëman
    Mâtt Frëëman about 12 years
    I think for now you should just run node.js direct if your sysadmin or hosting company provide a public IP and let you the command the port 80. There are no longer any security doubts that dont exists in any other package, when you need a proxy in front the requirements/reasoning for such will be clear..
  • Mâtt Frëëman
    Mâtt Frëëman about 12 years
    From my understanding it is only marginally less efficient and wont effect you until you are ready to scale (then you look at micro-optimizations). nginx is written in pure C and make direct syscalls to handle static file serving in the optimal way, i believe in node.js the chunks of file have to be copied into a v8 buffer first and then in node user-land and back out, although node.js moves so quickly this might not be the case today.