IP and Wildcard VirtualHost Conflict - virtualhost

I'm trying to setup an Apache web server that has a couple WordPress Multisite installations. My goal is to have clients point their domain at one of the IP addresses say 209.50.xxx.xxx and that traffic gets directed to the WP Multisite directory. I also have other domains that need to go to a specific directory regardless of what their IP address is. I can't seem to get both IP Based and Wildcard VirtualHosts working together. I can only get one or the other. In the current setup below, I can get traffic on any domain pointed at 209.50.xxx.xxx to go to the correct directory, but app.example.com and www.example.com will just get an error "ERR_TOO_MANY_REDIRECTS".
## All Traffic to IP: 209.50.xxx.xxx on Port 80 (Catch All) ##
<VirtualHost 209.50.xxx.xxx:80>
DocumentRoot /var/www/example/site/public
</VirtualHost>
## All Traffic to IP 209.50.xxx.xxx on Port 80 with Domain app.example.com ##
<VirtualHost 209.50.xxx.xxx:80>
ServerName app.example.com
DocumentRoot /var/www/example/app/public
</VirtualHost>
## All Traffic to any IP on Port 80 with Domain www.example.com or example.com ##
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example/main/public
</VirtualHost>
## All Traffic to any IP on Port 80 with Domain www.other-example.com or other-example.com ##
<VirtualHost *:80>
ServerName www.other-example.com
ServerAlias other-example.com
DocumentRoot /var/www/other-example/main/public
</VirtualHost>
I have tried different ordering with the current virtualhost catchall at the beginning flipped to the end with the same results.
Any ideas on how to fix this?

I setup a virtualbox with 2 IP addresses so that I could carefully test different virtual host configurations, and I believe that I have the solution. It seems that if I start using IP addresses in the VirtualHost line (<VirtualHost 192.168.1.201:80> instead of <VirtualHost *:80>) then I need to do that for every VirtualHost block. Then for the "Catch-All" blocks for each IP, I keep that first at the top of the file (The order is important.)

Related

Apache: subdomains, virtual host, and dynamic dns

My current setup is like this. I have a registered domain name. Through my domain provider I have pointed my domain to a dynamic IP address. So mysite.com points to 97.89.120.x
I have dd-wrt router and it is keeping my dynamic IP updated and port forwarding all port 80 and 443 request to my apache server IP of 192.168.121.50 ( Nothing real crazy here)
My apache server redirects all port 80 request to port 443
cat /etc/apache2/sites-available/default
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
cat /etc/apache2/sites-available/default-ssl
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mycloud
</VirtualHost>
This all works like a champ and is very straight forward.
I want to add a subdomain of cloud.mysite.com. So that mysite.com and cloud.mysite.com both point to the same dynamic IP 97.89.120.x From my dd-wrt router I am port forwarding all port 80 and 443 request to a new server in my environment 192.168.121.60
I want the apache server on this new host to host mysite.com and to forward request for cloud.mysite.com to the other server 192.168.121.50 and be redirected to https / ssl / ports 443.
So how exactly do I setup the virtual server to do this? I have tried similar to this without success.
NameVirtualHost *
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName www.cloud.mysite.com
ServerAlias 192.168.121.50
DocumentRoot /var/www/mycloud
</VirtualHost>
How can I redirect the subdomain to a different server and get this to work?
You have a wrong understanding of virtual hosts. The purpose of vhosts is to host multiple sites on a single webserver. It is not impossible to do this with your approach, but in the case that you have several webservers and services that you want to publish to the www in one domain, you will organize this in another way.
You can do this by DNS, Gateway, Reverse Proxing, Routing and maybe other approaches.
I suggest to find an approach where the ddwrt router will organize the requests and hand them to the desired webserver.
On ddwrt, just as example for a reverse proxy and loadbalancer, you can do this with Pound: http://www.dd-wrt.com/wiki/index.php/Pound
Please let me know if you have further questions. I will edit my post then. But this is from this point a question for serverfault and not for stackoverflow.
You will need to use 192.168.121.60 as a proxy of 192.168.121.50. But this may not be the best/smart way to achieve this. Proxying a web server for another ??
You already have setup for cloud.mysite.com on 192.168.121.60. Using mod_proxy_balancer ,this will look something like below.
<VirtualHost *:80>
ServerName www.cloud.mysite.com
ServerAlias cloud.mysite.com
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://192.168.121.50
</Proxy>
ProxyPass / balancer://mycluster
</VirtualHost>
The best way as pointed out by #LukasF is to have both the sites hosted on a single web server.
Hope that helps!

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 VirtualHost is not working

I have a server running Apache 2.4 on Windows, and I have set up a VirtualHost in the httpd-vhosts.conf file, and an 'A' record in my DNS server that points subdomain.mydomain.com to my IP address. Unfortunately, connecting to subdomain.mydomain.com just shows the same page as mydomain.com. Here is the code I used in the httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin admin#mydomain.com
DocumentRoot "c:/Apache24/subdomain/htdocs"
ServerName subdomain.mydomain.com
ErrorLog "c:/Apache24/subdomain/logs/errors.log"
CustomLog "c:/Apache24/subdomain/logs/access.log"
</VirtualHost>
What am I doing wrong?
Make sure your domain provider configuration doesn't redirect # to www, you need them to be configured separately to redirect to your machines IP address

VirtualHost - point all domains to root folder and one domain to a subfolder in the root folder

I have tried but nothing seems to work for me here. I have an Apache server with multiple domains set up. Currently they all point to the root folder (C:\xampp\htdocs) but I'd like to organize things a little better here on. I'd like to use Apache's VirtualHost to point each new domain to a subfolder inside this folder but have all older websites point to C:\xampp\htdocs in the mean time until I can migrate each.
The issue is that when I use the following code, all my domains point to the subfolder
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain\"
ServerName www.newdomain.com
</VirtualHost>
I need to add a Virtual that listens to all other domains and points them to C:\xampp\htdocs
But I'm running out of ideas. Please help
Try that:
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName _default_
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain"
ServerName www.newdomain.com
</VirtualHost>
You need a default virtualhost, that's the catch_all thing, when the Host Header defined in the HTTP query does not match any ServerName directive the default VH is useD. And the default VH is the first one defined in the configuration.

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.