Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: VirtualHost, ServerName, and main server context defaults |
|
Author |
|
stardustvega
Joined: 28 Feb 2023 Posts: 14
|
Posted: Wed 03 Jul '24 14:15 Post subject: VirtualHost, ServerName, and main server context defaults |
|
|
I'm working away on Apache in a local environment for learning purposes, and I'm trying to understand why the main server context settings don't act as a fallback in the way I would expect. (I know this probably isn't what you'd do in real life, but this is about understanding the underlying concepts.)
In my main httpd.conf file, I have added:
Code: | ServerName localhost |
Leaving the DocumentRoot and Directory directives alone in that file.
I also included my httpd-vhosts.conf file, which has:
Code: | <VirtualHost *:80>
ServerName test-1.local
DocumentRoot "${DOCROOT}/test-1"
<Directory "${DOCROOT}/test-1">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName test-2.local
DocumentRoot "${DOCROOT}/test-2"
<Directory "${DOCROOT}/test-2">
Require all granted
</Directory>
</VirtualHost> |
(I also have the relevant entry in my hosts file to send test-1.local and test-2.local to 127.0.0.1, the same IP as localhost.)
What I thought would happen:
I would have thought that:
1) Going to http://localhost would get you the default Apache "It works!" page (falling back to those settings because there's no match for the requested ServerName)
2) Going to test-1.local would show you the index file from the test-1 directory
3) Going to test-2.local would show you the index file from the test-2 directory
What actually happens:
2 and 3 do happen, but 1 doesn't. When you go to http://localhost, you get dropped on the index file from the test-1 directory.
Possible causes:
I'm looking at the Name-based Virtual Host Support and I think it most likely has to do with this:
Quote: | Configuration directives set in the main server context (outside any <VirtualHost> container) will be used only if they are not overridden by the virtual host settings. |
I also note that in their example, they show two virtual hosts and there's a comment that says:
Quote: | # This first-listed virtual host is also the default for *:80 |
But it doesn't fully explain why that's the case.
Where I'm confused is that I would have thought that the virtual host settings would only override the main server configuration directives if the Host header in the request matched the ServerName in the VirtualHost directive. So, I would expect it to override it for case #2 and #3, but not #1.
Instead it seems as if the first VirtualHost directive is acting as the configuration for anything that comes in on Port 80 and doesn't match any other ServerName.
Why is that happening? Is it just "Because Apache is set up to do that?" or is there some detail of how virtual hosts are set up that I'm not understanding? Or perhaps there's something about making a request to http://localhost that doesn't act the way I think it does?
Edit: Another theory--maybe it's a specificity thing? Like, it gets a request on Port 80, which matches either of the two virtual hosts. Then it checks if any of the ServerName directives in them matches the Host header in the request (which would be a better match). When it doesn't, it just goes with the first match found, which is the first virtual host?[/b] |
|
Back to top |
|
Otomatic
Joined: 01 Sep 2011 Posts: 212 Location: Paris, France, EU
|
Posted: Wed 03 Jul '24 14:35 Post subject: |
|
|
Hi,
I think localhost should be declared as VirtualHost.
At least that's what I have at home:
Code: | <VirtualHost _default_:80>
ServerName localhost
DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
|
|
|
Back to top |
|
stardustvega
Joined: 28 Feb 2023 Posts: 14
|
Posted: Thu 04 Jul '24 22:07 Post subject: |
|
|
Sorry, I didn't ask my question well.
My question wasn't "How do I get it to fallback to the default 'It works' page when you just go to http://localhost"; it was more "Why doesn't it fall back to that page?" More on understanding the concepts vs trying to get specific behavior, if that makes snse.
Currently, I think the theory in my edit is correct (although I still haven't been able to find anything to confirm either way), but would be curious if anyone here can confirm.
If I add another port to my main configuration file, so that it has:
Code: | Listen 80
Listen 5000 |
And then I update my virtual hosts file:
Code: | <VirtualHost *:5000>
ServerName test-1.local
DocumentRoot "${DOCROOT}/test-1"
<Directory "${DOCROOT}/test-1">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:5000>
ServerName test-2.local
DocumentRoot "${DOCROOT}/test-2"
<Directory "${DOCROOT}/test-2">
Require all granted
</Directory>
</VirtualHost> |
With this setup:
1) http://localhost, test-1.local, and test-2.local all fallback to the "It works!" page (which makes sense because they're all coming in on Port 80)
2) test-1.local:5000 gets the index file from the test-1 directory
3) test-2.local:5000 gets the index file from the test-2 directory
So I think I was getting the behavior I saw before because all requests were coming in on Port 80, so if they didn't have a host name that matched the server name, they just used the first virtual host configuration. |
|
Back to top |
|
Otomatic
Joined: 01 Sep 2011 Posts: 212 Location: Paris, France, EU
|
Posted: Fri 05 Jul '24 9:25 Post subject: |
|
|
Hi,
Code: | httpd.exe -t -D DUMP_VHOSTS | Show parsed vhost settings and will tell you which is the default server for each listening port. |
|
Back to top |
|
stardustvega
Joined: 28 Feb 2023 Posts: 14
|
Posted: Sat 06 Jul '24 0:01 Post subject: |
|
|
Ah! Yes, that is helpful. I was trying to figure out how to get more information, thanks. |
|
Back to top |
|
|
|
|
|
|