Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Virtual host redirect to secondary server 1

Status
Not open for further replies.

iamregistered

Programmer
Oct 3, 2006
27
SE
Greetings all,

I was wondering, is it possible to add a virtual host in apache so that when using two different dns-addresses they will be directed to a certain server?

For exampel server1.mine.nu will be the default and handled by apache server, but server2.mine.nu will be redirected to a mail server?

I'm trying a mail server and since I can't use the same port for both, I would like to use my secondary dns to point to that server, through apache, if possible.

So instead of using to access the mail server I would like to use that address with port :80 and have apache direct it to the other server. This to make it easier to access the mail server for others so they do not need to remember wich port it is using.

Thanks in advance,
/Martin

***
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
***
Sun setting far away
Tree leaves turn from green to gold
Cold nights approaching
***
 
Before making things overly complicated, why do you think "since I can't use the same port for both"? If these are 2 seperate machines (and apache installations), both can use port 80 and operate entirely seperately. DNS would need to be configured to point the different names at the different IP addresses. The only reason that you might not be able to do this, would be if both machines are behind the same NAT device (router) & the NAT device only has 1 public IP that can be forwarded.

 
Sorry for my bad explanation, the servers are running on the same machine thus I cannot use the same port. I did find one way to do it:
Code:
<VirtualHost server1.mine.nu>
Servername server2.mine.nu
ServerAlias *
Redirect permanent / [URL unfurl="true"]http://server2.mine.nu:8080/[/URL]
</VirtualHost>
This did work, but it made both server1 and server1 point to server2.mine.nu:8080

I tried various ways to configure apache, and alter that specific but was not successfull.

***
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
***
Sun setting far away
Tree leaves turn from green to gold
Cold nights approaching
***
 
You can do this with proxies. See my post as same day as yours for what it looks like. You basically direct to as many servers as you like based on internal IP and PORT if need be.

Hope that helps
 
techvr :: thanks for your advice, it was a more neater way than redirect as the ":8080" part was left out from address-field in internet explorer.

By adding following code:
Code:
<VirtualHost smail.ath.cx>
	ServerAlias smail.ath.cx
	ServerName smail.ath.cx
	DocumentRoot /
	ProxyPass / [URL unfurl="true"]http://smail.ath.cx:8080/[/URL]
	ProxyPassReverse / [URL unfurl="true"]http://smail.ath.cx:8080/[/URL]
</VirtualHost>
the result is the same as redirect, it "proxys"/"redirects" both dns-addresses to my email-server (server1.mine.nu:8080). The dns-addresses (not my real dns) server1.mine.nu and server2.mine.nu points toward the same IP-number, my globe IP through which I am connected to the internet.

I've never used virtualhost before and haven't found a guide that explains it simple enough for me to understand :( is there something I'm missing?

***
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
***
Sun setting far away
Tree leaves turn from green to gold
Cold nights approaching
***
 
How to set up Name Based Vitualhosts on Apache
faq65-1831
 
I simply don't get it. Is there something I need to change in the apache configuration file in order for the following code to work?

I add this at the very bottom of "http.conf", but still both radical.mine.nu and smail.ath.cx points toward the same directory, namely the first DocumentRoot entered.
Code:
<VirtualHost *>
	ServerAdmin admin@radical.mine.nu
	ServerAlias radical.mine.nu
	ServerName radical.mine.nu
	DocumentRoot z:/[URL unfurl="true"]wwwserver/radical.mine.nu[/URL]
</VirtualHost>


<VirtualHost *>
	ServerAdmin admin@smail.ath.cx
	ServerAlias radical.mine.nu
	ServerName smail.ath.cx
	DocumentRoot z:/[URL unfurl="true"]wwwserver/smail.ath.cx[/URL]
</VirtualHost>

What am I doing wrong here? :( I'm about to purchuase my own domain-name and would like to get virtualhost to work correctly for me, so I'm toying with two dns-addresses. I've read manual after manual, including the above FAQ, but nothing has made it possible for me to make it work :(

***
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
***
Sun setting far away
Tree leaves turn from green to gold
Cold nights approaching
***
 
First, make sure you have a NameVirtualHost directive.

If that's OK, this behavior can sometimes be caused with certain versions of apache when using SSL & not specifying port numbers in the virtual hosts, so to be sure, specifically add the ports to your virtualhosts & restart.
Code:
<VirtualHost *[!]:80[/!]>
    ServerAdmin admin@radical.mine.nu
    ServerAlias radical.mine.nu
    ServerName radical.mine.nu
    DocumentRoot z:/[URL unfurl="true"]wwwserver/radical.mine.nu[/URL]
</VirtualHost>


<VirtualHost *[!]:80[/!]>
    ServerAdmin admin@smail.ath.cx
    ServerAlias radical.mine.nu
    ServerName smail.ath.cx
    DocumentRoot z:/[URL unfurl="true"]wwwserver/smail.ath.cx[/URL]
</VirtualHost>
 
By the way, what you've found is apache's default behavior - when it can't determin which virtual host to server (for whatever reason), it will always serve the first. For testing purposes, you could switch the order of the VirtualHosts to confirm this condition. There are several possible reasons why apache might not be able to determine the correct virtual host. One of the more common ones is the port condition that I've briefly explained above. Another is a problem with ServerNames & ServerAlias. As I type this I notice another problem with the code above - you have the same ServerAlias for both VirtualHosts. You can't do this because how would apache determine from that name which one you're actually after? Therefore, it should actually be something like this:
Code:
<VirtualHost *[!]:80[/!]>
    ServerAdmin admin@radical.mine.nu
[!]#   ServerAlias removed[/!]
    ServerName radical.mine.nu
    DocumentRoot z:/[URL unfurl="true"]wwwserver/radical.mine.nu[/URL]
</VirtualHost>


<VirtualHost *[!]:80[/!]>
    ServerAdmin admin@smail.ath.cx
[!]#   ServerAlias removed[/!]
    ServerName smail.ath.cx
    DocumentRoot z:/[URL unfurl="true"]wwwserver/smail.ath.cx[/URL]
</VirtualHost>
 
smah :[medal]: Thank you so very much for your help! Now I've got it to work, but one part was left out, that was NameVirtualHost smail.ath.cx, in the last virtualhost

This is now my config of virtualhost, and it works flawless; so far! I'll make sure I keep a copy of this config for later usage!

You've earned your self a BIG star *. I also added ServerAlias, and it worked still! [2thumbsup]

You've save me hours of future pondering! :-D

Code:
<VirtualHost radical.mine.nu:80>
	ServerAdmin admin@radical.mine.nu
	ServerAlias radical.mine.nu *.radical.mine.nu
	ServerName radical.mine.nu
	DocumentRoot z:/[URL unfurl="true"]wwwserver/radical.mine.nu[/URL]
</VirtualHost>


<VirtualHost smail.ath.cx:80>
	ServerAdmin admin@smail.ath.cx
	ServerAlias smail.ath.cx *.smail.ath.cx
	[b]NameVirtualHost smail.ath.cx[/b]
	ServerName smail.ath.cx
	DocumentRoot z:/[URL unfurl="true"]wwwserver/smail.ath.cx[/URL]
</VirtualHost>

***
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
***
Sun setting far away
Tree leaves turn from green to gold
Cold nights approaching
***
 
The NameVirtualHost directive should be in the main part of httpd.conf, not inside the VirtualHost container, and it should have an IP address & port, not a name.

Having a VirtualHost ServerAlias that is exactly the same as the VirtualHost ServerName is not wrong, but is redundant and not needed.

Glad you got it working.
 
By the way, I've never tried the * wildcard ServerAlias. It might work, it might not. It does not seem to be a documented function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top