Making IP address in URL work for Apache
I have an Apache2 setup working fine for several virtual hostnames. But when a request is made to the IP address with that IP address as the hostname of the URL, I just get the "It works!" page. I have inserted a virtual host directive giving the IP address as the virtualhost and the servername trying to get that to take over when just the IP address is used. But that is having no effect. This used to work this way in Apache1 (last time I needed to do this). Now it doesn't. Is there a new way to do this?
Here's the concatenation of my site configs now (with substitutions for the real domain and address). The 2nd VirtualHost section is where I'm trying to make the "IP address in URL" work (and do a redirect). Code:
Listen 44.33.22.11:80 |
Hi,
If this is ubuntu as in your profile distro image, then you need to use a2ensite to enable vhosts Take a look here for details Regards |
Virtual hosts are already working fine. It's just that one where the IP itself is used that is not working. None of the virtual host documents I have read say how to do that.
|
If your vhosts work as expected, then you can remove the 1st vhost and add a ServerAlias to the 2nd one.
Code:
Listen 44.33.22.11:80 |
Quote:
1. (this case) redirect IP-in-URL visitors to the main page, no matter what path 2. redirect IP-in-URL visitors to the hostnamed site with the same path 3. redirect IP-in-URL visitors to a specific site and path, on another server 4. keep the IP-in-URL as is and show distinctive content for this case I could do all these in Apache1 by making the IP address based virtual host section have whatever directives will accomplish the desired goal. Does Apache2 no longer support this? |
Well I'm not sure if I can understand what you mean by these cases. Anyway, if I get it right:
1. Just a plain RedirectMatch: Code:
RedirectMatch /(.*) http://example.com/ 2. Code:
RedirectMatch /(.*) http://example.com/$1 3. Plain Redirect or mod_rewrite 4. Don't get it, but I guess you mean something like a reverse proxy, so use mod_proxy |
The problem is that the VirtualHost section where I use ServerName as the IP address is not being recognized, not matter what I put in there (Redirect, DocumentRoot, Alias, etc). When I put in a host NAME then it works. Something changed in the way Apache2 handles IP addresses as names, compared to Apache1.
|
Quote:
Anyway you shouldn't care too much, because having example.com as the 1st vhost, it makes it also the default one, so any request to a hostname that resolves to your apache IP that does not correspond to an existing vhost (and of course a direct IP request) will bring up the default vhost (example.com). |
Quote:
1. No hostname specified at all (no Host: header or maybe even an empty Host: header) 2. The IP address given as the Host: name (presumably the correct one) 3. Decimal number name (like 740365835 for 44.33.22.11) but this may not be usable since browsers now seem to disallow it due to spammers having used it. For any of these, or at least the first 2, I would want control over how it behaves. For example I might want to have a list of links to click on for the actual named vhosts (I'd build that page). Or I might want to just have an error page. Or I might want a redirect that could go to a selected site that may not even be on this IP address or even this server. So clearly, I need for this kind of request to be recognized distinctly from named vhosts on this IP. To the extent that Ubuntu's and/or Debian's "helpful" Apache pre-configuration breaks this, then I guess I need to depart from that configuration. And they managed to have some rather crazy init scripts to select the configuration (trying to decide if I should replace "/etc/apache2" entirely, or just start up a whole new configuration at another filesystem location). |
Hi,
All the requests that point to your apache IP, but do not have a hostname defined in any of your vhosts, are served by the default one. This is the 1st vhost (example.com in your case). In ubuntu you should take care to use a filename like aaa for the default vhost, as the Include directive that is used to include enabled vhosts, works alphabetically. Or you can configure a _default_ vhost to catch any unmatched request. Regards |
Quote:
|
Here's a new config file. As far as I can see, this follows what you say. But it still fails. In each directory I have a file named "id.txt" that contains the name of that directory. So I can see which virtualhost section matches. If the request has no Host: header, it does go to the first section. But, if he request as a Host: header that gives the IP address in dotted decimal string form, just as given on the servername directive in the 2nd virtualhost section, it really uses the 1st virtualhost section.
How can I make it use the 2nd virtualhost section ... the one that matches it? I want to have these two different cases (case 1 is Host: not given and case 2 is Host: is given as the IP address) use different sections. This did work in Apache1. It does not work (at least not this way) in Apache2. Code:
listen 44.33.22.11:80 |
Well, it works here (apache 2.2.21). 2 different vhosts:
Code:
<VirtualHost *:80> Quote:
|
Your example seems to ONLY be something that outputs information about how the Host header is coded in the HTTP request. That's NOT the issue. I know how to make the HTTP requests and have already verified they are being made as expected. The issue I am having is Apache is NOT distinguishing 2 of the cases (which it did in version 1).
Try the experiment this way. Set up 3 VirtualHost sections, each with a different DocumentRoot. Put files in those DocumentRoot directories so you can readily determine which VirtualHost section was selected. Now configure those 3 VirtualHost sections in such a way that each of the following 3 types of HTTP requests will each go to a different VirtualHost section. Code:
telnet 192.168.1.8 80 Code:
telnet 192.168.1.8 80 Code:
telnet 192.168.1.8 80 If Apache2 has had this ability intentionally removed, I'd like to find out why. Otherwise if it cannot, I'd have to consider it a regression issue (e.g. they broke it and haven't fixed it, yet). Also, duplicate the same 3 kinds of VirtualHosts, but on a 2nd different IP address, with 3 more different DocumentRoot entries. Code:
telnet 192.168.1.9 80 Code:
telnet 192.168.1.9 80 Code:
telnet 192.168.1.9 80 For extra credit, add in 3 more sections for an IPv6 address and hostname. Apache had some issues handling IPv6 in some previous things I did (but I think it was a bad parser issue). Code:
telnet fc00::a 80 Code:
telnet fc00::a 80 Code:
telnet fc00::a 80 |
Quote:
The "strange" thing I've noticed, is that I had to give a ServerName (default in this case), otherwise a request with Host: www.domain.com was pointing to the 1st vhost too. A request with Host: x.x.x.x point to the correct vhost. So this works as you want: Quote:
Regards |
All times are GMT -5. The time now is 09:38 PM. |