Proxy Node.js Application Requests through Apache

Running a Node.js application along side other non node web applications can work well if configured correctly. This tutorial will show you how to easily configure a Virtual Host to proxy Node.js application requests through Apache.

First, we need to make sure that we have Node.js running on a port other than the port Apache is running on (usually port 80). Our goal is to run the Node.js server on a port other than 80, and then use Apache to proxy incoming Node.js application requests to the port that the Node.js server is running on.

First, we need to make sure that Apache has the and modules loaded. If they are not loaded, go uncomment them in the Apache configuration file, and then restart the server.

Next, we need to configure an Apache Virtual Host just like we would any other VHost, except we want to proxy all domain requests for your Node.js application to the Node.js server.

**Note: The process is also the same if only a portion of an application is using Node.js.

Let’s take a look.

Apache Virtual Host for the Node.js Application

    DocumentRoot /your/site/location
    ErrorLog logs/
    CustomLog logs/ common

    ProxyRequests off
        Order deny,allow
        Allow from all

**Note: This configuration assumes that the node.js server is already running locally on port 3000, but it could be configured to use any unused port and/or an IP, that points to a system that is not the same as the one that Apache is running on.

Next, we need to tell Apache to proxy all node.js requests to node.js. This is done in our location directive with ProxyPass. All domain requests for will proxy to the node.js server running locally on port 3000.

Our ProxyPassReverse is used if/when our application performs a redirect, however its usually good practice to add even if your application isn’t redirecting.

For example, if our Virtual Host proxies incoming requests to our node.js application running at localhost on port 3000, and if our application performs a HTTP 301 or 302 redirect for one of those requests, there we be an error.

Our response headers will contain a location of when sent back to Apache.

And as you can tell, if Apache sends this location back to the client, then there will more than likely be an error.

ProxyPassReverse solves this by rewriting the location in our response headers so that Apache sends back to the client’s browser.

After that, save your Virtual Host configuration for your node.js application, and restart Apache.

And that’s it!

All incoming requests to should now be proxied to your node.js application.

Happy Coding! 😀

Leave a Reply

Your email address will not be published. Required fields are marked *