Apache virtual host discussion

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Apache virtual host discussion

Postby Altrea » 19. May 2016 20:06

[EDIT by Altrea: splitted from original topic https://community.apachefriends.org/f/viewtopic.php?f=16&t=74007]


My recommend virtual host configuration would look like this:

Code: Select all
# blank default vhost - only used for debug reasons
<VirtualHost _default_:80>
    DocumentRoot "C:/xampp/vhosts/honeypot"
    ErrorLog "logs/honeypot-error.log"
    CustomLog "logs/honeypot-access.log" common
<VirtualHost *:80>

# localhost vhost
<VirtualHost *:80>
    Servername localhost
    DocumentRoot "C:/xampp/htdocs"
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

# foo.dev vhost
<VirtualHost *:80>
    Servername foo.dev
    DocumentRoot "C:/xampp/vhosts/foo.dev/htdocs"
    ErrorLog "logs/foo.dev-error.log"
    CustomLog "logs/foo.dev-access.log" common
    <Directory "C:/xampp/vhosts/foo.dev/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

# bar.dev vhost
<VirtualHost *:80>
    Servername bar.dev
    DocumentRoot "C:/xampp/vhosts/bar.dev/htdocs"
    ErrorLog "logs/bar.dev-error.log"
    CustomLog "logs/bar.dev-access.log" common
    <Directory "C:/xampp/vhosts/bar.dev/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>


Explaination:
  • Create a default vhosts to not get confused in cause of not matching vhosts (otherwise the very first vhosts would take effect)
  • if you still want to use the XAMPP Dashboard create a localhost vhost
  • Each used vhost gets its own log files
  • Each used vhost gets its own Access Rules (i know that there is already an Access Rule defined for the localhost vhost, but having it all in the vhost config is cleaner in my opinion)
  • I never configure any vhost in a subfolder of another vhost (this can have sideeffects with webapplications if the wrong URL is used accidentically)
  • I place the DocumentRoot folder for each vhost one folder deeper than needed at minimum to keep the possibility to cleanly place files above the DocumentRoot

I would like to have an active discussion about my recommantdation with the experts here so i can write a sticky for this topic :D

@gsmith: Thanks for telling me about Define. I will take a look at this. Could be very powerful
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8293
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Sticky for virtual hosts - aka XAMPP will not start

Postby gsmith » 19. May 2016 21:59

@Altrea: In your example what about the ServerName and DocumentRoot in httpd.conf. The reason I ask is that the first vhost should have the same ServerName and DocumentRoot (I just use docroot) as the one in httpd.conf

See "Main host goes away" in the docs.

In my experience not following that golden rule can cause problems and people would not get the right host for the hostname as they expected. In the case of your example, assuming that your document in httpd.conf is c:/xampp/htdocs, you have not done this, at least not mentioned anything about this. Something to touch on since it is part of the equation.

Altrea wrote:
  • I never configure any vhost in a subfolder of another vhost (this can have sideeffects with webapplications if the wrong URL is used accidentically)
  • I place the DocumentRoot folder for each vhost one folder deeper than needed at minimum to keep the possibility to cleanly place files above the DocumentRoot
1) I figured the user would figure this out quick enough that I didn't mention it.
2) This is the way to go, the way I go. /websites/sitename/public_html
gsmith
 
Posts: 194
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win XP to 2012R2/VS 6,9,11,14

Re: Sticky for virtual hosts - aka XAMPP will not start

Postby Altrea » 19. May 2016 23:01

gsmith wrote:See "Main host goes away" in the docs.

Well, i knew that this hint exists, but to be true i have absolutely no idea what this really means.

The default ServerName in XAMPPs Apache is localhost (which is not a real world scenario for live webservers). So let's assume a request cannot get routed to an explicit vhost, so that in your configuration the global ServerName localhost takes effect. Cannot this have much more sideeffects for LAN/WAN requests because localhost will always get translated to 127.0.0.1 or ::1 instead of for example the LAN IP? Or does DNS only takes effect if you use a Servername inside a <VirtualHost> definition like so: <VirtualHost localhost:80>?

gsmith wrote:In my experience not following that golden rule can cause problems and people would not get the right host for the hostname as they expected.

So what else do they get?

Scenario 1: let's assume i am using namebased vhosts and only define one single vhost which is not matching my global ServerName setting, DocumentRoot etc. If i send a request not matching that vhost (but for example the global ServerName, or a complete different ServerName), what get's responsed?

Scenario 2: I create two vhosts, one default one and a second one matching my global settings. If a request is not matching any of the vhosts what gets responsed?

Maybe i should simply test this :roll:

http://httpd.apache.org/docs/2.4/en/vhosts/name-based.html#using wrote:Any request that doesn't match an existing <VirtualHost> is handled by the global server configuration

I don't understand this too, while a different place of the manual is saying the following
https://httpd.apache.org/docs/2.4/en/vhosts/examples.html#purename wrote:Due to the fact that the virtual host with ServerName http://www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.

global server configuration or first VirtualHost. isn't that a contradiction? What is true?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8293
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Apache virtual host discussion

Postby gsmith » 20. May 2016 02:43

1. The virtual host, hence the title of the section "main host goes away" in the docs.
2. Request not matching any servername, you get first configured vhost for the port.

I'm gonna give your config a try, you may have found a way to bend the rule a little.

Off topic but if you have many vhosts (excluding _default_) that are configured with the same options, overrides, requires and path layout, mod_macro is pretty cool.
gsmith
 
Posts: 194
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win XP to 2012R2/VS 6,9,11,14

Re: Sticky for virtual hosts - aka XAMPP will not start

Postby Nobbie » 20. May 2016 12:15

Altrea wrote:isn't that a contradiction? What is true?


No, it is not. The documentation is quite poor at this issue. I will try to explain more precisely:

If you have one or more VirtualHosts, these usually point to an IP and a Port or to an array of IPs. Let's assume, you have this configuration:

Code: Select all
ServerName localhost
...
<VirtualHost 192.168.178.20:80>
ServerName server1
...
</VirtualHost>

<VirtualHost 192.168.178.20:80>
ServerName server2
...
</VirtualHost>


Now the client enters http://192.168.178.20. That means, he requests a host from the scope of these VirtualHosts (the scope is 192.168.178.20:80), but the ServerName is not known to Apache (its the IP), therefore Apache answers in the scope of the first VirtualHost, that is the VirtualHost with ServerName server1.

Now the client enters http://127.0.0.1 in his browser (he is working at the Xampp Server), therefore the whole VirtualHost does NOT match the request. In that case, Apache does NOT answer in the scope of this VirtualHost, instead the global ServerName localhost is taken. Same thing if the client enters http://localhost, in that case also the VirtualHost has no effect and the global ServerName is taken.

Now we look at a different configuration:

Code: Select all
ServerName localhost
...
<VirtualHost *:80>
ServerName server1
...
</VirtualHost>

<VirtualHost *:80>
ServerName server2
...
</VirtualHost>


Now the question: what happens now, if the client enters http://localhost? Now localhost (127.0.0.1) is in the scope of the VirtualHost (as the scope contains all IPs *:80), therefore Apache will follow the VirtualHost, but there is no ServerName localhost in the VirtualHosts definitions, therefore Apache will respond in the scope of the first VirtualHost with ServerName server1. That means, in this case, the global ServerName localhost "has gone", it cannot be reached anymore.

You see the difference and why both documentations are right? They are badly explained, but you should get it now (I hope so).
Nobbie
 
Posts: 8770
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 60 guests