Apache VirtualHost with ProxyPass to node.js server refuses to work (‘Could not resolve host name’)

apache-2.4node.jsproxypassvirtualhost

I'm running local Apache on my mac for development, and also have node.js server running at http://127.0.0.1:8000/.

I use .dev domain for Apache projects and would like .node domain to redirect to node.js server. I have virtual hosts set up as follows:

<VirtualHost *.dev:80>
    VirtualDocumentRoot "/www/sites/%1/wwwroot"
    ServerName sites.dev
    ServerAlias *.dev
    UseCanonicalName Off
</VirtualHost>

<VirtualHost *.node:80>
    ServerName sites.node
    ProxyPass "/" "http://localhost:8000"
    ProxyPassReverse "/" "http://localhost:8000"
</VirtualHost>

However, when I try to apply these settings, I get the error:

[core:error] [pid 8594] (EAI 8)nodename nor servname provided, or not known: 
AH00547: Could not resolve host name *.node -- ignoring!

I should note that .dev domain works as expected, and node.js server is reachable at http://localhost:8000/, just not at, say, http://test.node/.

Am I missing something here?

Best Answer

Don’t you just hate when people make rookie mistakes and run to stackexchange for an answer?

Here’s what is being done wrong here:

  1. Making sure that the custom domain actually resolves to localhost is always a kosher thing to do.

For homebrew dnsmasq on mac, this will do the trick:

echo 'address=/.no/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf

And then OS X should be told to look up custom domain locally:

sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/no'

Custom domains 101 really.

  1. VirtualHost rule for proxy redirect should appear as soon as possible in the configuration file. Otherwise, Apache might redirect your custom domain request to localhost without appending the custom port :8000.

Like this:

<VirtualHost *.no:80>
    ServerName sites.no
    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:8000/"
    ProxyPassReverse "/" "http://localhost:8000/"
</VirtualHost>

<VirtualHost *.dev:80>
    VirtualDocumentRoot "/www/sites/%1/wwwroot"
    ServerName sites.dev
    ServerAlias *.dev
    UseCanonicalName Off
</VirtualHost>
  1. Custom domain .node is not currently a valid top-level domain, according to IANA. Changing it to something valid, like .no, prevents that pesky Safari from initiating Google search on requests such as test.no. (Say goodbye to the real .no sites out there.)

And Robert’s your father’s brother.