LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 09-28-2011, 03:11 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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
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>
Any idea how to make it happen?
 
Old 09-28-2011, 04:03 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
 
Old 09-28-2011, 04:42 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
Old 09-28-2011, 04:56 PM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
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 44.33.22.11
        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>

<snip>
 
Old 09-29-2011, 09:56 AM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by bathory View Post
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?
 
Old 09-29-2011, 02:02 PM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
 
Old 09-30-2011, 11:56 AM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
Old 09-30-2011, 04:05 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by Skaperen View Post
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).
 
Old 10-03-2011, 01:24 PM   #9
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by bathory View Post
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).
 
Old 10-04-2011, 04:04 AM   #10
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
 
Old 10-04-2011, 12:54 PM   #11
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by bathory View Post
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
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.
 
Old 10-12-2011, 02:03 AM   #12
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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>
 
Old 10-12-2011, 03:58 AM   #13
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Well, it works here (apache 2.2.21). 2 different vhosts:
Code:
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>


<VirtualHost *:80>
        ServerName 192.168.0.77
        DocumentRoot /usr/local/apache/htdocs
        ErrorLog /usr/local/apache/logs/error_log
        CustomLog /usr/local/apache/logs/access_log combined
</VirtualHost>
Each one with an index that writes how the vhost was called. And here is what I get:
Quote:
bash-4.1$ telnet localhost 80
Trying 127.0.0.1...
Connected to ad.yieldmanager.com (127.0.0.1).
Escape character is '^]'.
GET / HTTP/1.1
Host:

HTTP/1.1 200 OK
Date: Wed, 12 Oct 2011 08:52:01 GMT
Server: Apache
Last-Modified: Wed, 12 Oct 2011 08:50:55 GMT
ETag: "448002-10-4af161f3bd9c0"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8

Called by Host:

GET / HTTP/1.1
Host: 192.168.0.77

HTTP/1.1 200 OK
Date: Wed, 12 Oct 2011 08:52:10 GMT
Server: Apache
Last-Modified: Wed, 12 Oct 2011 08:51:11 GMT
ETag: "2c4ce5-1d-4af16202ffdc0"
Accept-Ranges: bytes
Content-Length: 29
Content-Type: text/html; charset=UTF-8

Called by Host: 192.168.0.77
So I guess you're doing something wrong, or perhaps you're using some sort of rewriting based on %HTTP_HOST
 
Old 10-12-2011, 12:52 PM   #14
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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
 
Old 10-12-2011, 04:38 PM   #15
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
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:
Quote:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName default
</VirtualHost>

<VirtualHost *:80>
ServerName 192.168.0.77
DocumentRoot /usr/local/apache/htdocs
...
</VirtualHost>

<VirtualHost *:80>
ServerName www.domain.com
DocumentRoot /usr/local/apache/htdocs/domain
...
</VirtualHost>
Maybe you have to take a look at hostmatching, or the use of ServerPath here

Regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide IPv6 address in URL. BernardLinux Linux - Newbie 3 09-26-2011 01:18 AM
URL to IP address cleopard Programming 5 03-28-2008 10:39 AM
Force using URL (ServerName) instead of IP address hapytran Linux - Server 2 12-06-2007 07:24 PM
Making Apache work Ab3n Linux - Newbie 3 04-03-2005 05:41 PM
anyone can decode this URL address? sirpelidor Linux - Software 8 02-21-2004 02:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration