1

Ok I have a set up a working apache-virtual host configuration, which redirects the users on IP request. The setup is the following:

<VirtualHost *:80>  
DocumentRoot "C:\wamp\www\dns"
ServerName ConnectToServer

ProxyRequests off
<Proxy *>
        Order deny,allow
        Allow from all
</Proxy>
<Location />
        ProxyPass http://localhost:8080/
        ProxyPassReverse http://localhost:8080/
</Location>

The thing is that I want the apache server to redirect to the NodeJS server on request of a specific subdomain.

For example, apache will redirect to nodeJs server if the user requests www.stackoverflow.com but I want it to redirect to the server when a subdomain like www.stackoverflow.com/question is requested.

So, how to setup a Name-Based Virtual Host?

All I would like to know guys if this practice,making my server available through apache is good for production or should I find another solution like heroku or ngix? ?

drizo
  • 267
  • 6
  • 14

1 Answers1

2

There are a few ways to do this. I wouldn't recommend putting Apache in front of Node however. Nginx is better, but not really necessary anymore. What I do on my server is run a Node proxy in front of my Node apps and my Apache sites.

More details about how I implement this can be found in this answer: How to use vhosts alongside node-http-proxy?

Apache -> Node

<VirtualHost *:80>  
    ServerName www.question.com

    ProxyRequests off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    <Location /answer>
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </Location>
</VirtualHost>
Community
  • 1
  • 1
Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
  • Let me check it out.Is this solution a good practice ,production wise ? – drizo Dec 10 '14 at 19:29
  • OK Timothy first of all thanks for answering.Second, I like your setup I will defenetely try it in the near future. BUT, the thing with m setup is that I need ot upload my node.js server on a computer that runs wamp and I must not change settings etc in it because,simply, it does not belong to me. So, I want to have the minimum effect on the rest of the webserver that runs with wamp. How would I redirect froma subdomain to my nodejs server? – drizo Dec 10 '14 at 19:39
  • I think so, otherwise I wouldn't be using it in production! I haven't encountered any problems. The http proxy is simple enough that it just runs without issue. When you put Apache in front of node, it becomes a potential bottleneck for requests. – Timothy Strimple Dec 10 '14 at 19:41
  • Updated my answer to include a VirtualHost setup which should work. You don't need the DocumentRoot since it's just acting as a proxy and not serving files. – Timothy Strimple Dec 10 '14 at 19:47
  • Well,sorry maybe I didn't epxress the question as I should. As I say I have a similar setup like the one you answered. I want to take it from that stage and make it Name driven redirect. Where do we say in our code that when you type that domain (e.g. localhost:8080/question) you go to Node jsserver? – drizo Dec 10 '14 at 19:53
  • That's what the ServerName is used for. http://httpd.apache.org/docs/current/vhosts/name-based.html – Timothy Strimple Dec 10 '14 at 19:56
  • Alright, your answer is perfect and now I totally get it. Right now I can't get it work when I request localhost/subdomain, but I will figure it out. Thanks! – drizo Dec 10 '14 at 20:03
  • Are you trying to request a subdomain or a subdirectory? What exactly would you like to type in to access this site? Keep in mind, if you use localhost, it's only going to be available on this machine and you may as well use a different port. – Timothy Strimple Dec 10 '14 at 20:13
  • oh, I am so sorry I am a newbie! I mean subdirectory! Suppose I have a website called www.question.com, I want when the user types www.question.com/answer to get redirected to the Node server! – drizo Dec 10 '14 at 20:16
  • You just add that to your Location tag. I've updated my answer. – Timothy Strimple Dec 10 '14 at 20:22
  • Seems like I can't serve static files. Maybe I need DocumentRoot? – drizo Dec 11 '14 at 20:25
  • Yes, I thought you were just wanting to host Node from that domain. Since you're using a subfolder however, you'll need the document root for things outside of that subfolder. This is assuming you want Apache to handle serving those static files and not Node. – Timothy Strimple Dec 11 '14 at 20:40
  • Thanks again. I'm also trying to host node through IIS now so I will ask another question later. :) – drizo Dec 12 '14 at 10:58