Configuring Reverse Proxy for Node.js application using Apache | Linux Administration
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’t need a host container for building a web app just means including a library that listens for HTTP requests on a port and responds. It is as simple as just coding your app and let it listen on any non-privileged port. Eg. 3000.
Need for configuring Apache with Node.js
With time things started getting complicated. The requirement of running node applications on privileged ports like 80, 443 starts emerging up.
Advantages of running Node.js with Apache
- It provides a cherry topping over cake, You can combine functionality from other languages like PHP with your node app
- Enhancing Security using Apache mod_proxy, nginx or haproxy to route requests to apps. This sit as a layer between your apps and the wide web.
- It Fulfills need to cache your static content and handle/scale to more users/connections per second than your node app probably can.
- It can also take care of the https termination, subdomain routing, redirects and things like that.
- Adds option to scale your application by performing load balancing leads to avoiding downtime for production environments.
- Allows the user to run multiple Node.js on HTTP port.
- This tutorial will guide you through to achieve these requirements.
LAMP Server with Node.js and npm installed over it. I am using Ubuntu machine for this demo
Using Apache’s Mod_Proxy Module (Non – SSL)
Enable the mod_proxy and mod_proxy_html Apache modules. They should be available by default so just enable them with the a2enmod command. sudo a2enmod proxy sudo a2enmod proxy_http. For more information on mod_proxysee the apache official documentation.
Condition: You must have started you node application and it is successfully over http://mydomian.com:3000/
<VirtualHost *:80> ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/nodeapp/ Options -Indexes ErrorDocument 503 /check.html ProxyRequests on ProxyPass /check.html ! ProxyPass / http://mydomain.com:3000/ ProxyPassReverse / http://mydomain.com:3000/ </VirtualHost>
We have created the virtual host for domain mydomain.com on which we want to run a node application. We have enabled the proxy module in apache and check.html will not be proxied through but will instead be served by the Apache server as a ‘normal’ page (the! means it won’t be sent through; for more info on the proxypass directive see the apache official documentation.
Using above proxy configuration in a vhost file you will magically get your node server’s responses running over http://localhost:3000/ by simply running the domain namemydomain.com.
Using Apache’s Mod_Proxy Module (SSL)
Condition: you must have started you node application and it is successfully over https://mydomian.com:3000/
<VirtualHost *:443> ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/nodeapp/ Options -Indexes ErrorDocument 503 /check.html SSLProxyEngine On ProxyPass /check.html ! ProxyPass / https://mydomain.com:3000 ProxyPassReverse / https://mydomain.com:3000 ProxyPreserveHost On SSLEngine on SSLCertificateFile /etc/apache2/ssl/mydomain.com.crt SSLCertificateKeyFile /etc/apache2/ssl/mydomain.com.key SSLCertificateChainFile /etc/apache2/ssl/mydomain.com:3000.ca-bundle </VirtualHost>
Routing Application Using IPTABLES
One way is route traffic of port 80 to desired port internally using iptables. In below example, we have routed all incoming traffic on port 80 to port 3000.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000