Apache – How to redirect different sub domain requests to different port

apache

I have two applications, one is the www.myexample.com, another is the blog.myexample.com. I am using PHP and Apache.

Now, I want to let www.myexample.com runs on port 82 of my machine, and blog.myexample.com on port 83, on the same machine. How to configure the apache and/ or the PHP scripts so that when the requests for the requests are served properly?

Edit: Thanks for everyone who responds, but I afraid I don't get the question clear– my bad!

What I really want is to simulate a condition whereby the www.myexample.com and blog.myexample.com are located on different machines. So when a request comes in, the gateway server ( the one which is also hosting www.myexample.com) will check whether this is a request for www.myexample.com or for blog.myexample.com and does the necessary reroutes.

How to do this? Thanks.

Best Solution

I will assume that you have your own reason for wanting the two sites (www and blog) to run on different ports - and in different processes. If this is not what you intended, e.g. you did not want to have two distinct processes, then having different ports may not be what you intended either: use VirtualHost instead, to co-host the two domains within the same apache+php instance on port 80. Otherwise, read on.

Assuming that you have your two apache+php processes listening on localhost:82 and localhost:83 respectively, bring up a third, apache-only process to act as a reverse proxy. Have the reverse proxy apache instance listen for requests coming on port 80 from the internet, with two virtual host definitions. The first virtual host definition, www, would forward requests to localhost:82, whereas the second virtual host definition, blog, would forward requests to locahost:83, e.g.:

NameVirtualHost *:80

# www
<VirtualHost *:80>
  ServerName www.myexample.com
  ProxyPass               /       http://localhost:82/
  ProxyPassReverse        /       http://localhost:82/
</VirtualHost>

# blog
<VirtualHost *:80>
  ServerName blog.myexample.com
  ProxyPass               /       http://localhost:83/
  ProxyPassReverse        /       http://localhost:83/
</VirtualHost>
Related Question