Simple Apache config for Node.js apps

By Evasive Software
On August 19, 2015
In Blog

As fast as Node.js is for some operations, it doesn’t exactly earn a gold medal for speed on delivering assets.

On production Node.js apps, I have found it far faster, and more reliable to serve all traffic through apache, by utilising mod_proxy

Setting up an example on Ubuntu

Assuming you have a genereal LAMP install of apache, make sure you have enabled both mod_proxy and mod_proxy_http.

sudo a2enmod proxy proxy_http
sudo service apache2 restart

5 line apache config for Node.js / asset delivery

A simple virtual host created in apache can be easily configured to serve all assets and static content through apache (this is also beneficial for automatic gzipping and caching support). By configuring the proxy, you can set any request containing “/api/” (or anything else) to be handed straight to Node.js.

In the example below, all requests to www.nodeexample.com/api/* are routed to our Node.js app (running locally on port 8081). All other requests are served via apache and will follow normal configuration and rules.

 <VirtualHost *:80>

    ServerName www.nodeexample.com
    DocumentRoot /var/www/nodeexample.com
    ProxyPreserveHost On
    ProxyPass /api/ http://localhost:8081/
    ProxyPassReverse /api/ http://localhost:8081/

</VirtualHost>
Blog comments powered by Disqus