Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have set up port forwarding to point to my works webserver so that selected users can access a site via our external IP address when off site. The forwarding to the server works, but on this server there are several Configs, and at the moment it is just stopping at the first one, so the wrong website is shown.

For example, take these two configs, and our external IP address is 11.22.33.44 - what I would like to be able to do is go to http://11.22.33.44:789 (random port, can be any) and have the website in Config 1 show. However, the website in Config 2 is showing, because it comes first alphabetically in the list of configs. Is there a way (similar to the ServerAlias) that I can check which port has been accessed and use the corrisponding config?

Config 1

Listen *:789
NameVirtualHost *:80
NameVirtualHost *:789

<VirtualHost *:80 *.789>
        ServerName test.dynedrewett.com
        DocumentRoot /var/www/html/website/test
        ErrorLog /var/log/httpd/test.dynedrewett.com-error
        CustomLog /var/log/httpd/test.dynedrewett.com-access combined
        ErrorDocument 401 /wp-content/themes/dyne_drewett/401.php
</VirtualHost>

Config 2

<VirtualHost *:80>
        ServerName dummy.dynedrewett.com
        ServerAlias dummy
        DocumentRoot /var/www/html/intranet/intranet_test
        ErrorLog /var/log/httpd/dummy.dynedrewett.com-error
        CustomLog /var/log/httpd/dummy.dynedrewett.com-access combined
        ErrorDocument 403 /wp-content/themes/dyne_drewnett/403.php
</VirtualHost>

Is there away I can amend the configs so that the correct website is shown when doing this? Thanks.

share|improve this question
    
What webserver are you talking about? IIS, Apache? –  Paulo Scardine Jul 31 '12 at 10:35
    
Apache. Thanks. –  David Gard Jul 31 '12 at 10:54
    
@PauloScardine - i suspect he's talking about Apache 2, given that the post is tagged as such :) –  Oliver G Jul 31 '12 at 11:05
    
Oh dear, I found why this was not working as I wished. What I have above is actually correct, but I had accidently forwarded to the wrong port, so that is why it was using the first config, as it didn't know which one it should actually use! Thanks. –  David Gard Jul 31 '12 at 11:36

1 Answer 1

up vote 2 down vote accepted

When you have several domain names hosted at the same IP address, the webserver uses the domain present at the request headers to figure out what content is to be served. Most apache instalations came with a "VirtuaHost" configuration by default even if it is serving only one site.

You can make one site the default when the request does not have enough information to pick the right VirtualHost, check the Apache documentation about a default virtual host configuration.

The other way is to point some domain name to the external IP and include this domain name in the VirtualHost config using a ServerAlias directive.

[update]

You can't serve more than one website on the same ip/port using name-based vhosts, but you can serve them at alternative TCP ports (or IP adresses, if you have more than one available).

If you can't set up an external DNS, in order to use name-based vhosts you can resort to this trick: edit the client machine /etc/hosts (or %WINDIR%\system32\drivers\etc\host) file to point the same internal dns name to your external IP.

share|improve this answer
    
Thanks for the reply. I am using Virtual Hosts (about 7 of them), and I use the ServerAlias directive successfully internally, where our DNS includes entries for these website. What I'm doing is connecting to the webserver via port forwarding thourgh our external IP, so there is no ServerAlias to lookup. –  David Gard Jul 31 '12 at 11:13
    
@DavidGard: what prevents you from mapping the external IP to a domain name like 'external.example.com' and include another ServerAlias to the desired vhost? –  Paulo Scardine Jul 31 '12 at 11:16
    
If I add an entry to the internal DNS then you would need to be on site to use it. And I can't add am external DNS entry as I've been asked not to by the powers that be. –  David Gard Jul 31 '12 at 11:25
    
@DavidGard: so you need to setup a _default_ vhost or make the desired vhost the default by moving it to the first vhost configuration, please refer to the link in the answer. –  Paulo Scardine Jul 31 '12 at 11:29
    
I can see that in the short term that is probably the answer, but in the future if I need to do this for another of our internal websites then I'd hit the same problem. –  David Gard Jul 31 '12 at 11:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.