Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Listen 44.33.22.11:443
NameVirtualHost 44.33.22.11:80
NameVirtualHost 44.33.22.11:443
<VirtualHost 44.33.22.11:80>
ServerName 44.33.22.11
RedirectMatch permanent /.* http://example.com/
</VirtualHost>
<VirtualHost 44.33.22.11:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /home/pub/example.com
<Directory /home/pub/example.com/>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost 44.33.22.11:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /home/pub/example.com
<Directory /home/pub/example.com/>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ANY.example.com.crt
SSLCertificateKeyFile /etc/ssl/private/ANY.example.com.key
BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
</VirtualHost>
<VirtualHost 44.33.22.11:80>
ServerName example.net
ServerAlias www.example.net
ServerAdmin webmaster@example.net
DocumentRoot /home/pub/example.net
<Directory /home/pub/example.net/>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost 44.33.22.11:80>
ServerName example.org
ServerAlias www.example.org
ServerAdmin webmaster@example.org
DocumentRoot /home/pub/example.org
<Directory /home/pub/example.org/>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>
</VirtualHost>
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.
OK, that worked. But it doesn't make sense WHY it worked. I'm saying that because unlike the other ServerAlias directives which leave the browser showing the original URL that it used, this happens to do a redirect the way I want for THIS CASE, even though I no longer have a directive saying to do that. What about for the other cases:
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/
Also mod_rewrite should do.
2.
Code:
RedirectMatch /(.*) http://example.com/$1
Or you can use mod_rewrite or better mod_proxy
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.
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.
Nothing has changed. I guess it's ubuntu and it's way to enable vhosts that is responsible for this behavior.
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).
Nothing has changed. I guess it's ubuntu and it's way to enable vhosts that is responsible for this behavior.
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).
This particular instance is not the only way I would want vhost behavior. There are actually 3 different kinds of requests that could be made:
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).
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.
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
I put a named virtualhost first, and without Host: that one was in fact chosen. But with Host: <IPaddress> it still does not go to another virtualhost where I have ServerName <IPaddress>. So its part way there.
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
namevirtualhost 44.33.22.11:80
<virtualhost 44.33.22.11:80>
serveradmin webmaster@ipal.net
documentroot /home/pub/44.33.22.11_default
alias / /home/pub/44.33.22.11_default/
<directory /home/pub/44.33.22.11_default/>
options followsymlinks
allowoverride all
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost 44.33.22.11:80>
servername 44.33.22.11
serveradmin webmaster@ipal.net
documentroot /home/pub/44.33.22.11_address
alias / /home/pub/44.33.22.11_address/
<directory /home/pub/44.33.22.11_address/>
options followsymlinks
allowoverride all
order allow,deny
allow from all
</directory>
</virtualhost>
<virtualhost 44.33.22.11:80>
servername example.net
serveradmin webmaster@ipal.net
documentroot /home/pub/example.net
alias / /home/pub/example.net/
<directory /home/pub/example.net/>
options followsymlinks
allowoverride all
order allow,deny
allow from all
</directory>
</virtualhost>
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
GET / HTTP/1.0
Code:
telnet 192.168.1.8 80
GET / HTTP/1.1
Host: 192.168.1.8
Code:
telnet 192.168.1.8 80
GET / HTTP/1.1
Host: example.com
The point here is to distinguish these 3 cases like Apache version 1 could do. The case without the Host: header should go to the first section since there is nothing available to do matches with. But the case with Host: giving the IP address should match with the section having that name.
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
GET / HTTP/1.0
Code:
telnet 192.168.1.9 80
GET / HTTP/1.1
Host: 192.168.1.9
Code:
telnet 192.168.1.9 80
GET / HTTP/1.1
Host: example.net
The reason for a 2nd set is to make sure the configuration you figure out applies to cases where Apache is NOT doing a general listen to the ANYHOST (0.0.0.0) IP address (e.g. not "*").
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
GET / HTTP/1.0
Code:
telnet fc00::a 80
GET / HTTP/1.1
Host: fc00::a
Code:
telnet fc00::a 80
GET / HTTP/1.1
Host: example.org
The point here is to distinguish these 3 cases like Apache version 1 could do. The case without the Host: header should go to the first section since there is nothing available to do matches with. But the case with Host: giving the IP address should match with the section having that name.
This is exactly what happens for a HTTP/1.0 request. It points to the 1st vhost (as it's the default).
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.