Node.js server with apache server

How to make Node.js work in parallel to apache or How node applications can run with port like http://example.com and not as http://example.com:3000 or http://example.com:8000?

The question may seem strange for some people as why would I need to run apache with node if a node web app itself runs its own HTTP server. Though you can run it as standalone but there are many advantages having running node with apache. I am listing some of the advantages of running node with apache.

Advantages

Might be you are building a hybrid application where you need to use various web development technologies like php, python etc then in this case running node in parallel with other servers could be beneficial.

Might be you already have some sites running on a dedicated server with apache and you don’t want to purchase a new dedicated server. So running node paralelly can save you on costs too.

Not only you can run one node app, but you can run multiple node apps and even non node app.

How to make Node run in Parallel with Apache?

Lets find out how to make Node run in parallel with Apache. The process is so simple and you may find it interesting because you may discover hidden talents of apache too while learning this. Inorder, to make this happen we need to follow reverse proxy concept. Apache has reverse proxy functionality which works in reverse. Lets understand how proxy works –

“Let’s say you are on a closed network and you need to request a document from the internet; you connect to a proxy server instead of directly to the server that has the document and then ask it to retrieve it for you. The proxy server goes out to the actual server that holds the document you want, retrieves it, and then passes it along to you. A reverse proxy is just same like that – you request a document from the server that you believe directly holds it, but that server in fact goes out and fetches it from some internal, pre-configured other server and then passes it along to you.”

Step by step view of reverse proxy:-

User requests example.com/test.html.

The apache server at example.com recognizes the example.com virtualhost, but does not have the test.html document – it is on a node server.

The apache server would have been preconfigured to take all requests for test.html (or some regex, or all requests) and forward them to some other server (in our example, a node web app) let’s say localhost:3000 on the same apache server for our example.

The node web app at localhost:3000 receives the request from the apache server which is acting like a proxy, processes the request, and sends it back the test.html document.

The apache server now forwards test.html document contents to the user who originally requested it.

The user has now received test.html; however they are aware that the file came from example.com not from localhost:3000 on the same server.

Incase server is down we can add additional apache directives that can describe which page to return in that case.

Configuring Apache as a Reverse Proxy:-

For this you need to create proper virtual host. Lets assume that we need to setup mycrm node app as mycrm.example.com where example.com is configured to point to some server running apache on port 80. Our mycrm application is listening on port 3000 and we have it running via forever in a continuous loop. Now, Open up your apache2 configuration (for me it is located in /etc/apache2/sites-available with a separate file per vhost/site) and create the virtualhost as below –


ServerName mycrm.example.com
ServerAlias www.mycrm.example.com
DocumentRoot /var/www/html/crm/
Options -Indexes
ErrorDocument 503 /maintenance.html

ProxyRequests on
ProxyPass /maintenance.html !
ProxyPass / http://localhost:8000/

Let’s try and understand each line of code –

Apache is going to listen on port 80 at any address/host for the connections to this vhost.

The specific host header we are looking for is mycrm.example.com.

We allow an alias called ‘www.mycrm.example.com’ to allow for www requests too.

All the requests are pointed to the document root of /var/www/html/crm/

Disable indexes for security.

Set up a custom 503 (“Service Unavailable”) error page to be /maintenance.html on the server.

Last three lines indicates – “we enabled the proxy module in apache and instructed maintenance.html not to be proxied through but will instead be served by the apache server as a ‘normal’ page (the ! means it won’t be sent through). This directive is necessary because in the next line we pass all requests through to localhost:3000 running our node app which means if it crashed, the 503 “service unavailable” custom error page would not be reachable as it will be passing the request for /maintenance.html to the node server which is down.”

Finally you are done. But you need to tighten the security here by intending users to only use your app via the apache reverse proxy, ensuring that your firewall is blocking outside requests from going directly to the node app!. Also if use proxy you may not eb able to get remote ip of use directly. Inorder to get remote ip you may need to find X-FORWARDED-FOR in request header. Proxy servers by default append a header to their request called “X-Forwarded-For” which is a comma separated list of IP’s for which the request was proxied.

Partial Serving through reverse proxy:-

For some applications you may want to serve your frontend stuff via apache directly, and possibly even use PHP to generate some content and then just have node acting as an API. To do that, simply change your ProxyPass directive from the virtualhost configuration to read something like below:

ProxyPass /api http://localhost:3000/

Now all requests sent to /api will be instead re-routed to the node app, but all other requests will go through to the usual document root.

If you like this post, please share.

On page seo

Seo and web tools Whatever you are writing a post, or article, or an html page, it should have proper utilization of all the H tags i.e H1, H2, H3. Your post should use bold and italics properly for seo optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and […] Continue reading →

Collection of seo and web tools

Seo and web tools Looking for best in class seo and web tools, find a to z list of all seo and web tools here. Directory submission sites http://www.abc-directory.com http://www.dmoz.org http://www.highrankdirectory.com http://www.sitepromotiondirectory.com http://www.cyndislist.com http://www.sitepromotiondirectory.com http://www.traveltourismdirectory.net http://www.socialmonkee.com/index.php Website Speed and Performance Optimization GTmetrix YSlow PageSpeed Pingdom WebPageTest Keyword Planners Keyword Revealer Google Trends Keyword tool Keyword tool Word tracker Cloud […] Continue reading →

Travel apis

If you are looking for travel booking apis or travel search apis then please find below a to z list of travel apis   Active.com Camping API: Access campground database   Add To Trip API: Social trip planning service   Adriatic.hr API: Croatian travel accommodations service   AdventureLink API: Travel search and booking service   AgentFactor Travel API: Travel booking […] Continue reading →