how does apache Name-based virtual host work - apache

As apache doc said:
The first VirtualHost section is used for all requests that do not
match a ServerName or ServerAlias in any <VirtualHost> block.
How to understand this sentence?
If my configuration like this(assume that hostname test.com is valid):
<VirtualHost *>
DocumentRoot "/www/test"
ServerName a.test.com
</VirtualHost>
When I visit b.test.com, apache will use The first VirtualHost section,But it could not resolve host.

Related

Apache2 wildcard + static subdomain

I would like to have traffic for all subdomains directed to one directory, only for one specific subdomain, to another one.
<VirtualHost beta.home.lan:80>
ServerName beta.home.lan
DocumentRoot /var/www/beta
</VirtualHost>
<VirtualHost *:80>
ServerName beta.home.lan
DocumentRoot /var/www/others
</VirtualHost>
It seams that the first virtual serve catches ALL trafic.
What have I done wrong?
Thanks!
http://httpd.apache.org/docs/2.2/en/mod/core.html#servername:
“If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host.”
If you request anything else than beta.home.lan, neither of the ServerNames in your two VirtualHosts matches – and there for this applies,
http://httpd.apache.org/docs/2.2/en/vhosts/name-based.html#using:
“If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.”
Use a ServerAlias instead in your second VirtualHost.
<VirtualHost *:80>
ServerName beta.home.lan
DocumentRoot /var/www/beta
</VirtualHost>
<VirtualHost *:80>
ServerAlias *.home.lan
DocumentRoot /var/www/others
</VirtualHost>

How to configure dynamic subdomains for Apache2 on Ubuntu?

I need all url mydomain.com, a.mydomain.com, b.mydomain.com, whatever.mydomain.com....
point to the same DocumentRoot, the subdomain is dynamic(maybe have more than hundreds)
Now I have the following lines in 000-default.conf:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
The mydomain.com is work, but all subdomain is not found.
Can someone help me? thanks so much.....
For example:
A user register a new account, the new account is "obama" then the url would be "obama.mydoamin.com". The subdomain can be entry when the account create immediately.
Wildcard sub-domains are possible using Apache virtual hosts.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName xyz1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/wildcard
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
The first entry will become the default if you don't get an explicit match. So if you had xyz.otherexample.com point to it, it would be caught be xyz1.example.com. You need to turn on the name based virtual hosts with the first entry.
For further details you can also refers to apache documentation apache Doc

How to customise my URL which is having IP Address

Is it possible to configure my URL which has my IP address on it- like: "http://192.168.xx.yy/index.php". The situation is when I run Apache server in my PC, and load localhost in it. I know it is possible after hosting with external server, but is there any way we can configure within our localhost?
How to configure the Apache files to make this happen? I tried in my localhost, editing the "httpd.conf" by adding this inside like this - please tell me where I am getting the issue!
ServerName localhost:80
HostnameLookups Off
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot /www/otherdomain
</VirtualHost>
DocumentRoot "c:/wamp/www/"
Yes, you can play with multiple IP addresses on your machine. Configuration depends on your OS. Article Create Multiple IP Addresses to One Single Network Interface is for linux.
But, better way is to use VirtualHosts based on host names or (simplest) on ports. So you can get http://siteA.mycoputer.localhost, http://siteB.mycomputer.localhost in the first case and http://192.168.x.y:8000, http://192.168.x.y:9000 in the second case
Here is Apache Server config example from Apache Server 2.2 documentation
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here
</VirtualHost>

Apache default VirtualHost

How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost *
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?
I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias at the top of the blocks:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /defaultdir/
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost>
If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in httpd.conf or apache.conf (or what ever is your main configuration file).
# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf
The other answers here didn't work for me, but I found a pretty simple solution that did work.
I made the default one the last one listed, and I gave it ServerAlias *.
For example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.secondwebsite.example
ServerAlias secondwebsite.example *.secondwebsite.example
DocumentRoot /home/secondwebsite/web
</VirtualHost>
<VirtualHost *:80>
ServerName www.defaultwebsite.example
ServerAlias *
DocumentRoot /home/defaultwebsite/web
</VirtualHost>
If the visitor didn't explicitly choose to go to something ending in secondwebsite.example, they get the default website.
Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 EC2 instance to serve many sites (domains).
Considering that you already have Virtual Host installed and working. In my folder /etc/httpd/sites-available, I have some files with domain names (suffix .conf), for example: example.com.conf. Create a new file like that.
sudo nano /etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/domain
</VirtualHost>
For each file.conf in sites-available, I create a symbolic link:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (.conf) in sites-available folder, sorted by filename.
To have a default DocumentRoot folder when access by IP, you have to create a file named 0a.conf, then Apache will serve this site because this new file will be the first in sites-available folder.
You must create a symbolic link:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
To check serving order, use it:
sudo apachectl -S
Now, restart Apache, and check out it.
Obligatory - none of the previous answers worked for me. I inherited a strange combination of IP address-based virtual hosts and * vhosts (not assigned/catch all IP addresses) based virtual hosts in this Apache configuration messed up by ISPConfig.
I wanted Apache to serve not configured hosts with the same page.
I had: not configured hosts went to the first vhost after 000-default.conf. No matter I had *:80 catch all defined as the first vhost, instead of default Apache would load first defined site:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:
<VirtualHost 192.168.10.10:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost 192.168.10.10:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
SSLEngine On
...
</VirtualHost>
$ apachectl -S outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)
...
46.23.86.103:443 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)
...
*:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.
An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.
Example:
NameVirtualHost *:80
Listen 80
...
<VirtualHost *:80>
ServerName host1
DocumentRoot /someDir
</VirtualHost>
<VirtualHost *:80>
ServerName host2
DocumentRoot /someOtherDir
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /defaultDir
</VirtualHost>
I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost
Only supported and correct answer is:
<VirtualHost _default_:*>
DocumentRoot "/www/default"
</VirtualHost>
or my own version to return 403:
<VirtualHost _default_:*>
<Location />
Require all denied
</Location>
</VirtualHost>
The NameVirtualHost option would be a good option.
The solution is:
# apache2.conf
# #warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80
# ...
# aaaa.example.conf
<VirtualHost *:80>
ServerName aaaa.example
DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf
<VirtualHost *:80>
ServerName host1.example
DocumentRoot /someDir
</VirtualHost>
# host2.example.conf
<VirtualHost *:80>
ServerName host2.example
DocumentRoot /someOtherDir
</VirtualHost>
In my case, to work, I created a VirtualHost (n.e. VirtualHost per CNAME) called aaaa.example since I have different files for different VirtualHosts and knowing that Apache reads them in alphabetical order.

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.