Apache2 wildcard + static subdomain - apache

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>

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>

how does apache Name-based virtual host work

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.

Name-based VirtualHosts by subfolder

I'm trying to configure Apache with three different VirtualHosts, such that a specific VirtualHost will be used when someone requests either the corresponding subdomain (e.g. foo.example.com) or the corresponding subfolder (e.g. example.com/foo).
I thought the following httpd.conf would do the trick, but the ServerAlias directives are simply being ignored:
<VirtualHost *:80 *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/http
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName foo.example.com
ServerAlias example.com/foo
DocumentRoot /usr/share/web
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName bar.example.com
ServerAlias example.com/bar
DocumentRoot /var/www/html
</VirtualHost>
When bar.example.com is requested the third VirtualHost is used, as intended. However, in the case of example.com/bar the first VirtualHost takes precedence despite the ServerAlias. Similarly, requesting example.com/foo matches the first VirtualHost, not the second.
How can I fix this configuration to produce the desired behavior?
ServerAlias takes a hostname, not a hostname and a path. This mechanism doesn't do what you want it to do. Just create an Alias or Redirect in the virtual host being accessed.

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

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.