1

I have a file called pga-default.conf which looks like,

<VirtualHost *:8008>
    ServerName 192.168.1.10

    DocumentRoot /var/www/portals/default/public

    <Directory "/var/www/portals/default/public">
       AllowOverride All
    </Directory>
    ErrorLog /var/log/httpd/default.error.log
    CustomLog /var/log/httpd/default.requests.log combined
</VirtualHost>

Problem is whenever I try with http://192.168.1.10:8008 it always loads the default Apache Testing page. default.conf looks like,

Listen 8008
<VirtualHost _default_:8008>
    DocumentRoot "/www/default"
</VirtualHost>

But when I change the <VirtualHost *:8008> into <VirtualHost 192.168.1.10:8008> in pga-default.conf it works as expected. I want to know why is this. As far as I know, this is because the server doesn't pick the VirtualHost as the best match for the particular IP address and port. (This answer confirms that difference between _default_:* and *:* in VirtualHost Context)

I tried several other methods as well, but none of them worked except the above-mentioned method. Following are the other methods. (Changes for pga-default.conf)

  • Change ServerName 192.168.1.10 to ServerName 192.168.1.10:8008
  • Used ServerAlias 192.168.1.10

Note - With the above configuration <VirtualHost *:8008> some of my colleagues have received successful results. This problem occurred only for me.

What have I missed here? How come others getting the expected results while I am not?

1 Answers1

2

You must always define ServerName in each virtualhost.

Since probably your "default" is loaded first and has no specific servername define it is grabbing all requests. You can try this easily by setting "ServerName default" in the default virtualhost.

So when you request "http://192.168.1.10:8008" you will now land in the appropiate virtualhost.

So remember, best practice is to always define a unique ServerName directive in each and every virtualhost, and consider when no requested hostname matches the defined virtualhosts the first virtualhost will take the request, also, if you accidentally define the same servername in two virtualhosts first loaded virtualhost will take the request.

ezra-s
  • 2,215
  • 1
  • 7
  • 13
  • this solves half of my problem. Yes it did work. But why having those configurations my colleagues got expected results while I am not? (I did upvote and I will accept this as the answer if you could explain the above-mentioned scenario) Note that, they had the same configurations as mine nothing more nothing less – Lahiru Jayathilake Mar 13 '18 at 12:44
  • 1
    Fair point. Because load order also does matter. Suppose you define the "default" virtualhost last in a single file, first virtualhost would take the request. If you "Include" files for each virtualhost they are loaded alphabetically. You can check the order in which they are being interpreted or if you have duplicate servernames with "apachectl -S" command. – ezra-s Mar 13 '18 at 12:47