Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have the following Apache config:

ServerName mydomain.com

<VirtualHost 1.2.3.4:80>     # contains my IP
    NameVirtualHost 1.2.3.4   # contains my IP
    ServerName mydomain.com
</VirtualHost>

My assumption is that only requests to mydomain.com should go to that VirtualHost. Unfortunately even requests to the IP address (1.2.3.4 in the code above) are handled by the VHOST.

1) What is the right procedure to distinguish between IP and Host?

2) What is the right approach to point requests to the IP i.e. to a 403 (i.e. with a separate VHOST)?

Many thanks!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I don't think there is a way to prevent a this by messing around with vhost defs. You're always going to have a default vhost.

However, you can accomplish it with mod_rewrite. Something like

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^mydomain.com [NC]
RewriteRule ^/(.*) /$1 [F]

will return 403 (F=forbidden) when the server is reached by IP unless somebody is being clever (i.e., inserting a Host: http header while still using the IP; normal browsers don't so this).

You could also attempt to redirect to mydomain.com instead of forbid:

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^mydomain.com [NC]
RewriteRule ^/(.*)  http://mydomain.com/$1 [R=301]
share|improve this answer

The first VirtualHost is the default, this is why it also gets your IP requests.
I'd do this:

NameVirtualHost *:80

<VirtualHost 1.2.3.4:80>  
    ServerName ip.mydomain.com   
    # first VH is default!
</VirtualHost>

<VirtualHost 1.2.3.4:80>
    ServerName mydomain.com
</VirtualHost>
share|improve this answer
    
I just tested it, but now all traffic goes to the default VHOST and none to the second, although I set the ServerName accordingly. –  MrG Oct 8 '10 at 12:38
    
I think omitting the ServerName in the first VH caused this, edited it. Sorry, I don't have a test environment for this at the moment. Also have a look here, there are some great examples: httpd.apache.org/docs/2.0/vhosts/examples.html –  faker Oct 8 '10 at 13:33
    
Thank you faker. Unfortunately adding ServerName didn't lead to a different bahaviour. I've also checked the webpage, but it does not include any IP related examples although it's definitely very good for the "usual" stuff. –  MrG Oct 8 '10 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.