Virtualhost not working on different port

You need to decide whether you're using name based virtualhosts or IP/port based ones. You've currently got the two mixed up together.

If you're going to put servers on their own ports, then you should not define NameVirtualHost for those ports. When you do define that for the host and port that the request arrives on, apache choses the virtalhost configuration to use on the basis of the servername and serveraliases.

I'm not clear on whether Apache will be prepared to choose a virtualhost on the basis of the domain name when that virtualhost block is defined using a different IP/port. What you are describing sounds like that might be what's happening.

In your /etc/hosts file you specify first-site.com and second-site.com. Neither of these are defined virtual hosts, since for those you use the www. prefix. If you are using name based virtualhostsYou presumably want to add ServerAlias directives to your virtualhost blocks. Meanwhile, your opening paragraph uses an unhelpful shorthand for the sites you're refering to - you'd be better off changing that to reference the exact URL(s) you are having trouble with.


Try making the Listen statement match your VirtualHost.

If you want to only bind to the public IP 62.210.xx.xx, use it everywhere.

Listen 62.210.xx.xx:80
Listen 62.210.xx.xx:8080

<VirtualHost 62.210.xx.xx:80>
  ServerName first-site.com
  ServerAlias www.first-site.com
  DocumentRoot /var/www/first-site
</VirtualHost>

<VirtualHost 62.210.xx.xx:8080>
  ServerName second-site.com
  ServerAlias www.second-site.com
  DocumentRoot /var/www/second-site
</VirtualHost>

--or--

If you want to bind to all interfaces on that server, use wildcards.

Listen 80
Listen 8080

<VirtualHost *:80>
  ServerName first-site.com
  ServerAlias www.first-site.com
  DocumentRoot /var/www/first-site
</VirtualHost>

<VirtualHost *:8080>
  ServerName second-site.com
  ServerAlias www.second-site.com
  DocumentRoot /var/www/second-site
</VirtualHost>

Essentially, the Listen statement tells apache to bind to that IP/port. Specifying 0.0.0.0:80 would only listen on ipv4 addresses. Specifying just the port number would bind to all interfaces on port 80.

Similarly, when SYN packets come in, the VirtualHost statement attempts to match based on the Listen statements.


What you are experiencing is the default behavior as per the Apache documentation.

enter image description here

Any request to an address other than 172.20.30.50 will be served from the main server. A request to 172.20.30.50 with an unknown hostname, or no Host: header, will be served from www.example.com.

Therefore, to be able to reach the second-site.com virtualhost, you have to do it through http://second-site.com:8080/ .

Make sure that you include these domain names in your local hosts file (e.g. /etc/hosts , C:\Windows\System32\drivers\etc\hosts , etc.) if you haven't configured DNS yet. If this is not an option, you can also set up a dynamic hostname with DynDNS or other provider, and use it for your ServerName/ServerAlias.