Apache2 bad matching virtualhost - apache2

I have a little problem with virtualhosts.
My configuration is:
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/domains/site1.com
ServerName www.site1.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
If you enter the following URLs into browser:
https://www.site1.com - OK (matches VHost with /var/www/domains/site1.com DocumentRoot)
http://www.site2.com - OK (matches Vhost with /var/www/domains/site2.com DocumentRoot)
but when I try site2.com with https protocol (https://www.site2.com). Apache finds wrong Vhost with /var/www/domains/site1.com DocumentRoot..
Why?
Thanks for help.

It's because you only have HTTPS configured for site1 and not site2.
You'll need to duplicate the site1 VirtualHost configuration and apply site2's information to it so it'll look something like the one below:
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
<VirtualHost *:443>
ServerName www.site1.com
DocumentRoot /var/www/domains/site1.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
I'm assuming you have a separate SSL certificate for your other site or are just testing.
You may also run into the issue of hosting multiple domains with HTTPS with only one IP address on your server, in which you should follow the instructions here: http://wiki.apache.org/httpd/NameBasedSSLVHostsWithSNI

This is simply how virtual hosts work in Apache. The match for the IP:PORT combination at the TCP layer is checked first. If there are multiple virtual hosts with the "best match", then hostnames (from the HTTP Host: header) are checked.
In your simple case, that means the best match for a request on port 443 is the single *:443 vhost. It will never look at worse matches to find a matching hostname.

Related

different error documents for different hosts listening on the same port

I have an apache2 with this similar configuration
<VirtualHost IP:8080>
ServerName IP:8080
DocumentRoot /var/www/x1
ErrorDocument 400 https://IP:8080
SSLEngine on
....
</VirtualHost>
<VirtualHost example.com:8080>
ServerName example.com:8080
DocumentRoot /var/www/example.com
ErrorDocument 400 https://example.com:8080
SSLEngine on
....
</VirtualHost>
the problem is that when I access http:/example.com:8080 the error document that redirect is the one of the ip.
What could be wrong?
I have try to put it inside the Directory directive but doesn't work either.

Apache 2 different Virtual Hosts point to first one

Well, I'm pretty new to this, so, I'm sorry if I'm making some stupid mistake but I've been trying to make my Apache Virtual Hosts work fine for different domains. I have one VPS with CentOS 6 and Apache 2 where I'm running 2 websites with diffent domain names; they were both working fine some days ago. But After I made some changes (i dont remember them) they're acting stupidly. The problem is that the both of the domains are pointing to the first document root in the vhosts file. This is my vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin support#domain.me
DocumentRoot /var/www/domainme
ServerName domain.me
ErrorLog /var/www/log/domainme.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin support#domain.com
DocumentRoot /var/www/domaincom
ServerName domain.com
ErrorLog /var/www/log/domaincom.log
</VirtualHost>
Everytime I try to access domain.com it gives me the index file of domain.me but if I put domain.com's configuration before the domain.me in vhosts file, and go to domain.me; it will give me the index of domain.com. I have two IP's for the VPS by the way.
Thank for reading, hope that its not a big issue.
Try this one
Listen *:80
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#domain.tld
DocumentRoot /path/to/domain1/
ServerName domain1.com
ServerAlias domain1.com www.domain1.com
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact#domain.tld
DocumentRoot /path/to/domain2/
ServerName domain2.com
ServerAlias domain2.com www.domain2.com
</VirtualHost>

DocumentRoot is set to docRoot of a virtualHost?

OS: centOS 6.3 Final
I've installed the mysql and apache2(httpd) packages and changed the config in /etc/httpd/conf/httpd.conf as seen below:
<VirtualHost *:80>
DocumentRoot /var/www/html/wordpress/
ServerName www.asterix.int
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/staticSite/
ServerName www.meins.lan
ServerAlias www.deins.lan
ServerAlias www.obelix.int
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/joomla/
ServerName www.example.com
ServerAlias www2.example.com
ServerAlias www3.example.com
</VirtualHost>
All ServerName and ServerAlias are working fine and I can access the 3 pages.
But when I'm trying to access a page over the server's ip something strange (at least for me) happens. apache2 returns me the index page of the first defined virtualHost(in this example wordpress). I've tried this with all 3 virtualHosts and get the same Results.
Is this a normal behavior or what I'm doing false ?
If this is a normal behavior: Can I set the DocumentRoot exclusively for all requests to the ip ?
Thx !
This is the intended behavior. If you use the IP (let's say http://123.123.123.123/), Apache will use 123.123.123.123 as HTTP the hostname. Since there is no VirtualHost with a ServerName or ServerAlias of 123.123.123.123, the first VirtualHost is used.
So if you want a VirtualHost that listens only for http://123.123.123.123/ you can simply create a VirtualHost with:
<VirtualHost *:80>
DocumentRoot /var/www/html/my-ip-site/
ServerName 123.123.123.123
</VirtualHost>
This is a normal behaviour.
You are using a vhost-method called name-based virtual hosts. As you can imagine that means, that the "routing" of the apache is then only done by the hostname in the HTTP-request, according the ServerName and ServerAlias directives in the config.
As the request to the IP of your server - e.g. 1.2.3.4 - can not be routed into any of the defined vhost, apache takes the default virtualhost.
The default virtualhost is more or less the first virtualhost defined.
The request on the IP is accepted, because you used wildcard definitions *:80.
You can check the virtualhosts set in apache by the apache
# command apache2 -S

Apache2 VirtualHost subdomains not working?

I have a static public IP and I am hosting my websites on it (Ubuntu Server 12.04). I need to set up a subdomain but It's not working, even www. is not working. I bought the domain from GoDaddy and I set up the correct NS
I saved the configuration file in /etc/apache2/sites-available under domain.me and then I did a2ensite domain.me and reloaded and restarted apache2, but still no luck.
This is my configuration file
<VirtualHost *:80>
ServerAdmin webmaster#domain.me
ServerName domain.me
ServerAlias www.domain.me
DocumentRoot /var/www
LogLevel warn
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#domain.me
ServerName me.subdomain.me
ServerAlias www.me.subdomain.me
DocumentRoot /var/www/subdomain
</VirtualHost>
curl http://domain.me outputs the index file, curl http://www.domain.me outputs curl: (6) Couldn't resolve host 'www.domain.me' and same for the subdomain
I managed to fix it, and for those of you who is having the same problem, make sure you set up the A records

Multiple hostnames, same domain, different virtualhost

I am trying to route requests to different docroots depending on the HOST name (not domain name):
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName forum.somesite.net
DocumentRoot /home/somesite/public_html/forums
</VirtualHost>
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName www.somesite.net
DocumentRoot /home/somesite/public_html
</VirtualHost>
It is not working though. Apache seems to go to the first entry regardless of what is in the hostname. What am I doing wrong?
Try changing the second Virtual host to this
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName somesite.net
ServerAlias www.somesite.net
DocumentRoot /home/somesite/public_html
</VirtualHost>

Resources