{"id":7553,"date":"2018-06-06T17:11:38","date_gmt":"2018-06-06T09:11:38","guid":{"rendered":"http:\/\/rmohan.com\/?p=7553"},"modified":"2018-06-06T17:12:23","modified_gmt":"2018-06-06T09:12:23","slug":"reverse-proxy-for-node-js-application-using-apache","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7553","title":{"rendered":"Reverse Proxy for Node.js application using Apache"},"content":{"rendered":"<h2>Node.js<\/h2>\n<p>Node.js is widely used to build web applications with real-time, two-way connections, where both the client and server can initiate communication, allowing them to exchange data freely.\u00a0The best\u00a0side is that we don\u2019t need a host container for building a web app just means including a library that listens for HTTP requests on a port and\u00a0responds.\u00a0It is\u00a0as simple as just coding your app and let it listen on any non-privileged port. Eg. 3000.<\/p>\n<h2>Need for configuring Apache with Node.js<\/h2>\n<p>With time things started getting complicated.\u00a0The requirement\u00a0of running node applications on privileged ports like 80, 443 starts emerging up.<\/p>\n<h2>Advantages of running Node.js with Apache<\/h2>\n<ol>\n<li>It provides a cherry topping over cake, You can combine functionality from other languages like PHP with your node app<\/li>\n<li>Enhancing Security using Apache mod_proxy,\u00a0nginx\u00a0or\u00a0haproxy\u00a0to route requests to apps. This sit as a layer between your apps and the wide web.<\/li>\n<li>It Fulfills need to cache your static content and handle\/scale to more users\/connections per second than your node app probably can.<\/li>\n<li>It can also take care of the https termination,\u00a0subdomain\u00a0routing, redirects and things like that.<\/li>\n<li>Adds option to scale your application by performing load balancing leads to avoiding downtime for production environments.<\/li>\n<li>Allows the user\u00a0to run multiple Node.js on HTTP\u00a0port.<\/li>\n<li>This tutorial will guide you through to achieve these requirements.<\/li>\n<\/ol>\n<h2>Prerequisites:<\/h2>\n<p>LAMP Server with Node.js and\u00a0npm\u00a0installed over it. I am using Ubuntu machine for this demo<\/p>\n<h2>Using Apache\u2019s Mod_Proxy Module (Non \u2013 SSL)<\/h2>\n<p>Enable the mod_proxy and mod_proxy_html Apache modules. They should be available by default so just enable them with the a2enmod command.\u00a0sudo\u00a0a2enmod proxy\u00a0sudo\u00a0a2enmod proxy_http. For more information on mod_proxy<a title=\"Apache2 ProxyPass Directive\" href=\"http:\/\/httpd.apache.org\/docs\/2.2\/mod\/mod_proxy.html#proxypass\" target=\"_blank\" rel=\"noopener\">see the apache official documentation.<\/a><br \/>\n<strong>Condition:<\/strong>\u00a0You\u00a0must have started you node application and it is successfully over http:\/\/mydomian.com:3000\/<\/p>\n<pre>&lt;VirtualHost *:80&gt;\r\n  ServerName mydomain.com\r\n  ServerAlias www.mydomain.com\r\n  DocumentRoot \/var\/www\/nodeapp\/\r\n  Options -Indexes\r\n  ErrorDocument 503 \/check.html\r\n\r\n  ProxyRequests on\r\n  ProxyPass \/check.html !\r\n  ProxyPass \/ http:\/\/mydomain.com:3000\/\r\n  ProxyPassReverse \/ http:\/\/mydomain.com:3000\/\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>We have created the virtual host for domain\u00a0<strong>mydomain.com<\/strong>\u00a0on which we want to run a node\u00a0application. We have enabled the proxy module in apache and check.html will not be proxied through but will instead be served by the\u00a0Apache\u00a0server as a \u2018normal\u2019 page (the!\u00a0means it won\u2019t be sent through; for more info on the\u00a0proxypass\u00a0directive\u00a0<a title=\"Apache2 ProxyPass Directive\" href=\"http:\/\/httpd.apache.org\/docs\/2.2\/mod\/mod_proxy.html#proxypass\" target=\"_blank\" rel=\"noopener\">see the apache official documentation.<\/a><\/p>\n<p>Using above proxy configuration in a\u00a0vhost\u00a0file you will magically get your node server\u2019s responses running over\u00a0<strong>http:\/\/localhost:3000\/<\/strong>\u00a0by simply running the domain name<strong>mydomain.com<\/strong>.<\/p>\n<h2>Using Apache\u2019s Mod_Proxy Module (SSL)<\/h2>\n<p><strong>Condition:<\/strong>\u00a0you\u00a0must have started you node application and it is successfully over https:\/\/mydomian.com:3000\/<\/p>\n<pre>&lt;VirtualHost *:443&gt;\r\n  ServerName mydomain.com\r\n  ServerAlias www.mydomain.com\r\n  DocumentRoot \/var\/www\/nodeapp\/\r\n  Options -Indexes\r\n  ErrorDocument 503 \/check.html\r\n\r\n  SSLProxyEngine On\r\n  ProxyPass \/check.html !\r\n  ProxyPass \/ https:\/\/mydomain.com:3000\r\n  ProxyPassReverse \/ https:\/\/mydomain.com:3000\r\n  ProxyPreserveHost On\r\n\r\n  SSLEngine on\r\n  SSLCertificateFile \/etc\/apache2\/ssl\/mydomain.com.crt\r\n  SSLCertificateKeyFile \/etc\/apache2\/ssl\/mydomain.com.key\r\n  SSLCertificateChainFile \/etc\/apache2\/ssl\/mydomain.com:3000.ca-bundle\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<h2>Secondary Option:<\/h2>\n<h2>Routing Application Using IPTABLES<\/h2>\n<p>One way is route traffic of port 80 to desired port internally using\u00a0iptables. In below\u00a0example, we have routed all incoming traffic on port 80 to port 3000.<\/p>\n<pre>sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Node.js <\/p>\n<p>Node.js is widely used to build web applications with real-time, two-way connections, where both the client and server can initiate communication, allowing them to exchange data freely. The best side is that we don\u2019t need a host container for building a web app just means including a library that listens for HTTP requests on [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7553"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7553"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7553\/revisions"}],"predecessor-version":[{"id":7555,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7553\/revisions\/7555"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}