Most people are fine just loading up their NodeJS server by a quick localhost:8080 request, but sometimes it's also useful to have an actual hostname available, especially when you're integrating with OAuth services that may require a qualified domain name for their redirect url. In this event, we need a way to tie Apache and Node together. Luckily it's easy.

Let's assume we have a node application running on :4567

var http = require('http'),
server = http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');


We can successfully load up localhost:4567, but now we'd like to tie it up to be proxied by a hostname, say

A typical Apache configuration would normally look like this, for serving your standard web directory

<VirtualHost *:80>
  DocumentRoot "/var/www/html/com.example.local"
  ErrorLog     "/var/log/httpd/com.example.local-error_log"
  CustomLog    "/var/log/httpd/com.example.local-access_log" common

  <Directory "/var/www/html/com.example.local">
    AllowOverride All
    Order allow,deny
    Allow from all

That should look pretty familiar to anyone who's spawned up a couple apache servers before. This would allow us to hit, granted we stuff that into our /etc/hosts, and be served whatever is in our /var/www/html/com.example.local directory.

For anyone following along hands on, for this to work, make sure you edit your /etc/hosts file (sudo required) and enter:

This is required for the hostname to resolve to our local machine. Moving forward, here's the changes we'll want to make so that our Apache server will, instead of serving a directory, proxy requests into our node application:

<VirtualHost *:80>
  ErrorLog   "/var/log/httpd/com.example.local-error_log"
  CustomLog  "/var/log/httpd/com.example.local-access_log" common

  ## Here's our magic
  ProxyRequests off

  <Proxy *>
    Order deny,allow
    Allow from all

  <Location /> # no, this closing tag is not a typo
    ProxyPass        http://localhost:4567 # Our port goes here
    ProxyPassReverse http://localhost:4567

Now you will be able to successfully load your node application from http://localhost:4567 over