Apache multihost does not respect server name - apache

I have two django applications on one machine. I would like to run them on the same address and port. So the user should be point to the correct address according the request domain/host. My current apache.conf file looks like:
<VirtualHost *:80>
ServerName 35.232.14.xxx
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.my_testing_domain.com
...
</VirtualHost>
Note that there is real IP address and real domain and other stuff required by Django app. I build the file according to the documentation: https://httpd.apache.org/docs/2.4/vhosts/examples.html#page-header (apache multiple sites on one IP) and https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 (django with apache).
The thing is, that every request goes to the first VirtualHost (in this case the IP address). If I switch their order in the apache.conf, then every request goes to the domain VirtualHost.
So one of the sites is working correctly (always the first one).
Why the apache does not respect the server names? What am I missing?

I add Server alias withou "www":
ServerAlias my_testing_domain.com
to the "domain based" virtual host. Now it works.

Related

404 Not Found The requested URL /xxxxx was not found on this server

Please i need help, I have gone through a couple of similar issues and how they were resolved and it seems mine is such a pain it just wont work.
I used a Turnkey Linux Appliance [OrangeHRM] and i want to use its Apache server to host another site/software [Sentrifugo] and it is so difficult, each time i host it only the default site shows up.
i have read through several similar issues here on stack overflow and it didn't work, i need someone to please take a look at my case.
Sincere appreciations in anticipation
Below is the output of grep "<VirtualHost" /etc/apache2/sites-enabled/*
/etc/apache2/sites-enabled/adminer.conf:<VirtualHost *:12322>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:80>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:443>
/etc/apache2/sites-enabled/sentrifugo2.conf:<VirtualHost *>
Good morning, based on what we discussed:
you have 4 VH (VirtualHost): *:12322, *:443, *:80, *
*:12322 is not a default port, it would have to be specified explicitly (http://SOMETHING:12322/...)
*:443: default SSL port (https://SOMETHING/...)
*:80: default http port (http://SOMETHING/...)
*: matches all ports
Now you are using http://IP/sentrifugo2/...
Using the IP address, Apache will check the VH in order and use the first one that matches.
If you ask for http://IP/..., since this is port 80 (default for http), Apache will match the third VH (the *:80 one).
But here you want the 4th one, the one with *
Solutions:
Works, but is not the best: define your 4th VirtualHost with a port. <VirtualHost *:80>. Then modify the order in which your VH appear in Apache and put the sentrifugo VH before the orangehrm.conf one. But that could create a new problem where the orangehrm.conf is not usable anymore... Keep reading...
Best: use name virtual hosting:
<VirtualHost *:80>
ServerName orangehrm.conf
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
<VirtualHost *:80>
ServerName sentrifugo
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
So you see, this way, Apache can diffentiate between the two *:80, using the requested domain (or name). On your client system, in /etc/hosts, define the two names:
10.0.0.18 orangehrm.conf
10.0.0.18 snetrifugo
So when you type http://orangehrm.conf or http://sentrifugo Apache will know which one you want.
Note: on Apache 2.4, this works no problem. On Apache 2.2, you need to specify NameVirtualHost directive (see Apache documentation on this).
Again works but not the best: might not be possible for you. You can split between the VH using a different IP address. Ex. 10.0.0.18 is orangehrm.conf and <VirtualHost 10.0.0.18:80> is used. Then 10.0.0.19 is sentrifugo, so <VirtualHost 10.0.0.19:80> is used (with Listen 10.0.0.19:80). But you must be allowed to configure extra IP addresses on your host for this...
another possibility: split on the port. <VirtualHost *:80> is for orangehrm.com, <VirtualHost *:81> is for sentrifugo. For this, you need to specify http://10.0.0.18:81/sentrifugo. This also requires Listen *:80 and Listen *:81.
In conclusion Apache needs a way to know which one you want. It can decide with: IP, Port, Name. Name is the most flexible.
IMHO using IP address is not the way to go. It works in simple setups, but quickly limits you. Use domain names. Here I showed you how to do it in your local host file, the "best-best" way to do is to set them up in DNS.

access to a server apache that has virtualhost

I have a cloud server with lampp installed on. I had configured a virtual host here like that:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
and everything work as i expect, if i go to www.xxx.com i see my 'folder' site.
Now i need to work to another site present on the same server, but it doesn't allready have a domain, so i had imagine (by reading the apache's configuration file explanation)that i have to do it in this way:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
<VirtualHost xx.xxx.xx.xxx:80/test>
DocumentRoot "/opt/lampp/htdocs/test/"
</VirtualHost>
But it doesn't work, if i do http://xx.xxx.xx.xxx:80 i reach the 'folder' site while if i do http://xx.xxx.xx.xxx:80/test rather the reach the 'test' site i still reach www.xxx.com, why? How could i reach this objective?
The virtual host defined first (top most) acts as default host. That one is used to respond to any incoming requests that are not matched by a specific host name in the request.
You want to try this setup:
# some fallback host for testing and development
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/_default"
</VirtualHost>
# a virtual host with a specific host name
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/example.com"
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
(here xx.xxx.xx.xxx stands for the systems public and routable IPV4 address)
In your file system you have this hierarchy:
/opt/lampp/htdocs/
_default/
test1/
test2/
example.com/
This way requests to http://example.com or http://www.example.com are mapped to the folder /opt/lampp/htdocs/example.com, requests to URLs with any other host name to the default folder /opt/lampp/htdocs/_default in which you now can create as many sub folders as you like for different applications.
Another approach would be to use other host names below your existing domain name for internal tests, so something like test1.example.com or similar. That way you do not have to use raw IP addresses with their routing risk.

Apache different sites on different ports, still links to same site. Bind9 for domain names

I've been trying to create 3 different domains linking to 3 different sites on the same machine, 2 which works but the third on the different port links to the first page.
My apache config looks like this:
Listen 81
NameVirtualHost *:81
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/www
</VirtualHost>
<VirtualHost *:80>
ServerName www2.example.com
DocumentRoot /var/www/www2
</VirtualHost>
<VirtualHost *:81>
ServerName controlpanel.example.com
DocumentRoot /var/www/controlpanel
</VirtualHost>
I've used Bind9 to set up the domains.
www IN A 123.123.123.123
www2 IN A 123.123.123.123
controlpanel IN A 123.123.123.123
www and www2 works fine and shows the correct site, however controlpanel.example.com also links to the first www site. When I enter the port manualy on the ip, xxx.xxx.xxx.xxx:81, i get linked correctly. The thing is that I don't really know where I'm doing it wrong, this is the first time I'm trying anything like this. You got any ideas?
Im also running this on an old ubuntu 12.04 server.
Regarding where you're going in the comments for the previous answer:
You could add a port 80 virtualhost for controlpanel.example.com and put a single statement inside,
Redirect / http://controlpanel.example.com:81
The purpose of the ServerName is not to inform the browser what port your webserver is using. It's used for name-based virtualhosts and as a last resort for self-referential links (out of the box, self-referential links are generated with whatever the client already thought it was accessing via the Host: header)
But there is definitely something quite bizarre about your requirement. Usually the motivation is to not use custom ports, and if they are, to address the server with a low port and have the por remapped by some intermediary (load balancer, proxy).
If you want your third virtualhost to be simulataneously the defautl on port 81 and a name-based option on port 80:
Change
<VirtualHost *:81>
to
<VirtualHost *:80 *:81>
Apache finds the set of virtual hosts with the best IP:PORT based match first, then if NameVirtualHost also matches, starts looking at the ServerNames from that set.

Multiple Apache2 vhosts are pointing to the same website

I'm running Apache2 on Ubuntu 10, and I have my site configuration files laid out numerically and in order. My default server is psychedeli.ca, but I also run another site off the same box at mahoganytales.com. Currently, both of these domains point to the same site (the one for psychedeli.ca). The declaration NameVirtualHost *:80 is in my ports.conf file, so I'm pretty sure my global server config checks out. How can I fix this?
Here are my vhost files:
001-psycho
<VirtualHost *:80>
DocumentRoot /var/apps/psycho/public
ServerName psychedeli.ca
</VirtualHost>
002-mahogany
<VirtualHost *:80>
DocumentRoot /var/apps/mahogany/public
ServerName mahoganytales.com
</VirtualHost>
try create new conf file at /etc/apache2/conf.d, e.g., vhosts.conf
with this content in it:
NameVirtualHost *
It looks like the default configuration is in effect rather than your host entries. Following is the procedure that works in Ubuntu Apache2.
First,
create a VirtualHost in /etc/apache2/sites-available/somesite,
then a2ensite somesite to make it live.
Finally, /etc/init.d/apache2 restart to restart apache.
If you think, you have followed the above steps, then can you please confirm, that you have your hosts files in /etc/apache2/sites-enabled/?
Each domain name needs to have it's own single unique ip address, that's how different sites are found.
By using the *:80 in the virtual host directive, you're instructing Apache to listen on all IP addresses, port 80 and send it to this directory. With your second vhost, you're doing the same thing (All IP's port 80, and send it there). Well, since you're giving it two conflicting statements, it takes the first match, and uses it.
If you want to serve multiple websites, each must answer to it's own unique IP address, ie:
site aaa.com - 145.25.82.110
site bbb.com - 145.25.82.111
From there, each vhost entry will listen on it's own ip address and port for each site. In the OP's case the vhost needs to change to (using the example IPs):
&ltVirtualHost 145.25.82.110:80>
DocumentRoot /var/apps/psycho/public
ServerName psychedeli.ca
&lt/VirtualHost>
&ltVirtualHost 145.25.82.111:80>
DocumentRoot /var/apps/mahogany/public
ServerName mahoganytales.com
&lt/VirtualHost>
This instructs the server to listen on static IP 1 port 80 (as defined in the named.conf and associtated bind config files, and send it to the first site base directory, and any calls on the second static IP port 80 and send it to the second site base directory.
As for configuring bind/named, that's beyond the scope of this question...

Redirecting an internal path to a virtual host

I have been working on a drupal test site for a while, which has a bunch of virtual hosts set up like this:
<VirtualHost *:80>
ServerAdmin email#example.com
DocumentRoot "/path/to/root"
ServerName testsite1.example.com
</VirtualHost>
I have been using a modified host file to view each of these test sites, along the lines of:
12.0.0.1 localhost
20.02.2.22 testsite1.example.com
20.02.2.22 testsite2.example.com
20.02.2.22 testsite3.example.com
This has worked fine, however now I need to send the sites over to some people remotely who are not technical enough to modify their own host files and see it the way I do.
Is there a way I could set up Apache so that the url "http://20.02.2.22/testsite1" would forward to testsite1.example.com internally? I am using Drupal, and the site setup needs to see the "testsite1.example.com" so that it can correctly choose the instance to select. I have been looking through apache rewrite, but I am a bit of a newb at this so any help is much appreciated.
testsite1.example.com will only be resolved on your machine, so you cannot redirect. You can set up proxy with mod_proxy. Hope this works for you:
<VirtualHost *:80>
ServerAdmin email#example.com
DocumentRoot "/path/to/root"
ServerName testsite1.example.com
ServerAlias 20.02.2.22
<Location /testsite1/>
ProxyPass http://testsite1.example.com/
</Location>
</VirtualHost>
The way I show my local test sites is a combination of Dynamic DNS and port-forwarding.
Internally, my Drupal site is at [my machine ip] or localhost.
I setup a free dynamic dns name to my IP and then on my router, accept incoming requests on port to route to [my machine ip]
That way, they can see yoursite.dyndns.com, but its looking at your local copy.
Why you just don't buy a new domain name and point it to your server IP address...
Or there are free domain solutions like:
http://www.dot.tk/en/index.html?lang=en
or
http://dyn.com/dns/
If you wanna buy new domain, i recommending you this one, cheap and quality service: http://server2.elite7hackers.us/recommend.php?site=godaddy