Apache: disable redirecting to default vhost on mismatching server name - apache

I have many services running on the same machine, one of them using SSL, let's say "c3po.com".
The url for my servers are https://c3po.com, http://r2d2.com and http://jarjar.com.
The problem is, if I type https://r2d2.com apache redirects me to https://c3po.com, without even changing the url. In other words, I will be seeing c3po service, with my browser showing http://r2d2.com.
I understand that when Apache can't exactly match a vhost it uses the first one loaded that matches the ip:port, so as there is no:
<VirtualHost *:443>
ServerName r2d2.com
...
It will pick up the only vhost on :443 found, which is:
<VirtualHost *:443>
ServerName c3po.com
...
What I really want is that when the user types https://r2d2.com or https://jarjar.com an error page is shown up, because those services (with ssl enabled over http) simply doesn't exist! How can I achieve that?

Check that
NameVirtualHost *:443
is enabled in your main config. Then create a VirtualHost that listens on port 443 with your error page, and create another VirtualHost with the config for c3po.com. If any user resolves a name to the IP of your server (which they will for all sites), they will go to the default site unless they're going to c3po.
Something along the lines of the following should work:
Default:
<VirtualHost *:443>
ServerName _default_https
DocumentRoot /path/to/error/page
<Directory /path/to/error/page>
...
</Directory>
</VirtualHost>
c3po:
<VirtualHost *:443>
ServerName c3po.com
ServerAlias www.c3po.com
DocumentRoot /path/to/c3po
<Directory /path/to/c3po>
...
</Directory>
</VirtualHost>

Related

Xampp unable to load multiple sites

I am working on a project using xampp server. I have more than one project. So for this I have set my httpd-vhosts like below
<VirtualHost *:8080>
ServerName khpos.com
ServerAlias www.khpos.com
DocumentRoot "D:/xampp/htdocs/pos"
<Directory "D:/xampp/htdocs/pos">
Require all granted
</Directory>
</VirtualHost>
#my second site
<VirtualHost *:8080>
ServerName demopos.com
ServerAlias www.demopos.com
DocumentRoot "D:/xampp/htdocs/demopos"
<Directory "D:/xampp/htdocs/demopos">
Require all granted
</Directory>
</VirtualHost>
Whenever I try to hit localhost:8080/demopos it's redirecting towards .../pos
hosts file
127.0.0.1:8080 khpos.com
127.0.0.1:8080 demopos.com
How to set it
Any help would be highly appreciated
You should change the portnumbers of the second etc. sites. Now all port addresses are on port 8080, if you change those to an other port number you should be fine.
<VirtualHost *:8080> to <VirtualHost *:{port-number}>
Where port-number is an other port-number
127.0.0.1:8080 khpos.com
127.0.0.1:{port-number} demopos.com
you have 2 virtual hosts defined and also the names in hosts file. Please first remove the port numbers in the hosts file:
ServerName khpos.com
ServerName demopos.com
127.0.0.1 khpos.com
127.0.0.1 demopos.com
if you call khpos.com:8080 or demopo.com:8080 it should reach your correct virtual hosts.
But: if you call localhost:8080 it will match none of your virtual hosts.
In that case Apache always enters the first virtual host matching the port that you have in your conf file ignoring its server name. That is a strange behavior of Apache - in my opinion a bug. It is exactly what you observe.
For that reason I always place a dummy virtual server in front of all others that can catch all non fitting requests with a dummy message in a simple html file.
Just a hint: you have defined
DocumentRoot "D:/xampp/htdocs/pos"
so there currently is no khpos.com:8080/pos because pos is part of the root unless you create another folder pos below

Virtualhost configuration not manage correctly requests

I have the following Virtualhost config in httlp-vhosts.conf:
<VirtualHost *:80>
ServerName rest.budgettracker.loc
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop/api/public"
ErrorLog "logs/rest.budgettracker.loc-error.log"
CustomLog "logs/rest.budgettracker.loc-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName dev.budgettracker.loc
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop"
ErrorLog "logs/budgettracker.loc-error.log"
CustomLog "logs/budgettracker.loc-access.log" common
</VirtualHost>
When I enter dev.budgettrackerpro.com in the browser it goes to the rest.budgettrackerpro.loc virtualhost container.
If I remove the Virtualhost container for the rest request it directs correctly to the correct html/javascript code. Obviously I need the rest call to make it work correctly. I have researched this until I am blue in the face, what am I doing wrong? Please help
You are asking for dev.budgettrackerpro.com. Your configuration is for dev.budgettrackerpro.loc.
What happens is:
Apache sees that your request is on port 80 (http://...)
It checks which VirtualHost are configured to take traffic from port 80.
Here it finds 2. 1) rest.budgettracker.loc 2) dev.budgettracker.loc
Since the domain you asked (the .com) does not match either 1) or 2), Apache assumes that the VirtualHost it should use is the first one it found.
Apache when finding multiple VirtualHost or finding none that match uses the first one in the file (top to bottom).
To solve this:
Request http://dev.budgettracker.loc
Modify your VirtualHost to accept traffic form .com as well, like so:
<VirtualHost *:80>
ServerName dev.budgettracker.loc
ServerAlias dev.budgettracker.com
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop"
ErrorLog "logs/budgettracker.loc-error.log"
CustomLog "logs/budgettracker.loc-access.log" common
</VirtualHost>
Notice the new line, ServerAlias dev.budgettracker.com. You can have multiple ServerAlias in a VirtualHost, but only one ServerName.

NamedVirtualHost in apache configuration

I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml

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.

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.