Friday, February 2, 2018

apache 2.4 - Default Virtual Host not catching the request




I have an Apache 2.4 server with two enabled virtual hosts:



/etc/apache2/sites-enabled/000-default.conf




ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined




/etc/apache2/sites-enabled/my-website.conf




ServerAdmin myself@my-website.com
ServerName my-website.com
Redirect permanent "/" "https://my-website.com/"





....




I created a domain name pointing to the same machine: my-other-website.com.



When I issue an http request (browser, curl,...) on my-other-website.com, I would expect it to serve the default virtual host. Instead, I get 301 redirected to https://my-website.com/.




How come?



Changing into didn't help.


Answer



VirtualHost marching is a bit peculiar and particular.



The “problem” is that you mix a *:80 with hostname:80 and the latter is the preferred match when a request for that ip-address & port number combination is received.



And *:80 is only the default VirtualHost for requests that don’t have the same ip-address as your my-website.com domain.




See https://httpd.apache.org/docs/2.4/vhosts/details.html#hostmatching




When the connection is first received on some address and port, the server looks for all the VirtualHost definitions that have the same IP address and port.



If there are no exact matches for the address and port, then wildcard (*) matches are considered.



If no matches are found, the request is served by the main server.




If there are VirtualHost definitions for the IP address, the next step is to decide if we have to deal with an IP-based or a name-based vhost.



No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...