<VirtualHost> matches when missing ServerName directive - apache

I am in the process of migrating some sites from a server running Apache 2.4.7 to a new installation (Ubuntu 18) running Apache 2.4.29 and running into some issues with VirtualHost matching.
On my old server I have a config like this:
<VirtualHost 12.34.56.78:80>
ServerAlias *.dev.example.com
VirtualDocumentRoot /var/www/dev/%1
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/
</VirtualHost>
As you can see, the ServerName directive is intentionally absent from the first host which uses a VirtualDocumentRoot to serve directories based on the subdomain. This has been working fine on the old server.
In my new environment everything worked fine at first, but today (no updates, nothing changed), oddly things changed. For some reason apache started matching example.com to the first vhost and after some amount of debugging I have determined that this is due to the lack of ServerName directive. When I add one - anything really - the problem goes away. So to be clear, a working config now looks like this:
<VirtualHost 12.34.56.78:80>
ServerName anything.dev.example.com
ServerAlias *.dev.example.com
VirtualDocumentRoot /var/www/dev/%1
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/
</VirtualHost>
The apache docs have this to say about omitting the ServerName directive:
If you omit the ServerName directive from any name-based virtual host,
the server will default to a fully qualified domain name (FQDN)
derived from the system hostname.
My system hostname is something like servername.example.com - which shouldn't match example.com.
My question:
Has there been some change in the apache's behaviour with this newer version that would break my previous configuration? I'm looking for an explanation about why this worked fine in the old environment and now suddenly does not. I'm also looking for any explanation at all about why apache would be matching my first vhost when ServerName is absent.

Related

Apache Virtual Hosts serves the vhost NOT matched

I set up an Apache Server with two Virtual Hosts and it has a very weird behaviour. I have a normal webserver, that should be server in all cases, except given the case, that the domain name is "biblio.name" or "biblio-intra.name", which should be redirected to a virtual machine located on my laptop, serving another webservice on Linux. On my laptop I use xampp for the Apache Server. I have the following "httpd-vhosts.conf" in my apache/conf/extra folder:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName sis.name/
ServerAlias *
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
ServerName biblio.name/
ServerAlias biblio-intra.name/
</VirtualHost>
So I expected it to redirect all requests to "biblio.name" and serve the rest as normal. However, it didn't!
When I enter my static ip-adress (assigned from the router) in the browser, I get served htdocs as normal, perfectly fine. When I enter biblio.name:8080 I also get normally served the virtual machine as expected (obviously, since 8080 automatically uses the redirect rule of the virtual machine.) However, when I type "sis.name" it redirects me to the virtual machine and when I type "biblio.name" it serves me from the htdocs.
I never experienced a behaviour like that and I don't get, why it serves the opposite host of the one supposed to.
Am I missing something?
For some reason I don't know (anymore), all my apache vhost configurations have the same value for ServerName and ServerAlias. Also the / in it seems odd. You can try listing biblio-intra.name as second option, but it should at first repeat the value from ServerName:
<VirtualHost *:80>
ServerName sis.name
ServerAlias sis.name
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName biblio.name
ServerAlias biblio.name biblio-intra.name
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
</VirtualHost>

Apache Virtual Host using mod_vhost_alias

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.
To do this I set the following in my 000-default.conf file in the sites-available folder.
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
This worked great.
Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.
Can someone please indicate to me what I am doing wrong?
Full Code Example In apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.
VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).
Edit:
Now you need some other changes:
- ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4).
- Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).
I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:
SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.
In your 000-default.conf site config file, write the following code:
UseCanonicalName Off
Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.*
DocumentRoot path/to/your/folder
</VirtualHost>
Then add the next block to point all other generic domains to a generic folder:
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias www.*
VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *
VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>
The first block will direct all domains, starting with www. to a folder matching the name after the www.
The second block is to direct the same domains, when no www. is specified, to the same folder.
For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Apache Multiple Sub Domains With One IP Address

This has probably been asked but I can't find a straight answer, or the ones I found don't work.
I have one domain mydomain.com, resolving to an IP; let's call it 8.8.8.8. The DNS settings also point two subdomains to that IP address with an A record. These are dev.mydomain.com and staging.mydomain.com. Both have an A-record pointing to 8.8.8.8.
On the server (8.8.8.8) I have two virtual hosts files. These are as follows:
staging.mydomain.com.conf
<VirtualHost *:80>
ServerName staging.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
And...
dev.mydomain.com.conf
<VirtualHost *:80>
ServerName dev.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
The problem is:
Regardless of whether I visit http://staging.mydomain.com or http://dev.mydomain.com, I always land on staging.mydomain.com (Apache serves these files).
I have restarted Apache and even the server. If I change the order of the .conf files so that dev is first, I always see that. Any suggestions would be so appreciated. Thanks!
update
I find myself back at this problem again! If you know that your syntax is correct, you might have a bad symlink. Delete it and recreate again, restarting apache in-between. I just did this and it solved hours of head-scratching. On CentOS you can check your available vhosts with httpd -S
update 2
I've also found this issue to exist when the apache log files for the virtual host don't exist, or aren't writable.
Sounds like you need to add NameVirtualHost directive to your configuration.
NameVirtualHost *:80
Under some circumstances Apache may not be able to handle *:80 VirtualHosts correctly. In those cases you should map VirtualHosts directly on specific IPs.
NameVirtualHost 8.8.8.8:80
<VirtualHost 8.8.8.8:80>
ServerName staging.mydomain.com
ServerAlias stage.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
<VirtualHost 8.8.8.8:80>
ServerName dev.mydomain.com
ServerAlias development.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
You can also run apachectl -t -D DUMP_VHOSTS to see how Apache parses the VirtualHost configuration.
Update: As mentioned in the comments, usually you can just use NameVirtualHost *:80. So most of the time you can configure the virtual hosts as follows.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName staging.mydomain.com
ServerAlias stage.mydomain.com
DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>
<VirtualHost *:80>
ServerName dev.mydomain.com
ServerAlias development.mydomain.com
DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>

Is www included in the server name in apache virtual host

I'm looking to set up VirtualHosts on my apache server and was looking for documentation that would tell me if these 2 entries are identical
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName example1.com
</VirtualHost>
Notice the lack of www. in the second one.
Thanks
This is from the Apache docs (http://httpd.apache.org/docs/2.0/mod/core.html#servername):
The ServerName directive sets the hostname and port that the server uses to identify itself. This is used when creating redirection URLs. For example, if the name of the machine hosting the web server is simple.example.com, but the machine also has the DNS alias www.example.com and you wish the web server to be so identified, the following directive should be used:
ServerName www.example.com:80
So I guess they are not identical.

Apache Multiple VirtualDocumentRoot

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
This is naming convention I am currently using and would like to continue to use:
host directory
127.0.0.1 domain domain.com
127.0.0.1 sub.domain domain.com_sub
Then in my vhosts section of the httpd.conf I have:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>
The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.
Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName domain.com
ServerAlias www.domain.com
VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>
That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.
You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:
VirtualDocumentRoot /var/www/%2.0.%3_%1
Regarding the OP and the issue with "/var/www/html" being set:
The problem I had to this was using %1 instead of %2. Here's my working example:
ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub
Hope that helps someone!
I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.
I finally found a configuration that allows flexible subdomain creation.
See apache docs on mod_vhost_alias
If your root dev domain has 3 parts like dev.example.com you can use %-4+ as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.
<VirtualHost *:80>
VirtualDocumentRoot "/var/www/%-4+/webroot"
ServerName www.dev.example.com
ServerAlias *.dev.example.com
php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>
This way you can create a directory named /var/www/sub.domain/webroot and access it with the url sub.domain.dev.example.com.
The line php_admin_value auto_prepend_file /var/www/setdocroot.php fixes the docroot on some systems like OSX 10.9+
Here is the content of setdocroot.php :
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root.
weird.
Update (2010-07-24):
I just wrote a blog post how to setup your http proof server
http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html