简体   繁体   中英

Name based virtual host?

I have a new site that uses Apache2. Looking at my server logs I see that there is a growing bunch of other domain names (all hosted with GoDaddy) that are mysteriously pointing to the IP address for my server. I do not know that this does any harm but I cannot see any reason why I should allow the situation to persist. From the bit of Googling I have done I have gathered that the way to stop this is to set up a name based virtual host and reject any attempts by other domain names to point to my content. However, I am not at all sure how I set up the relevant entries. Looking at commentary in my /etc/apache2/apache2.conf file I get the impression that I should do something along the lines of

NameVirtualHost mysite.com:80
<VirtualHost mysite.com:80>
ServerName mysite.com
ServerAlias:www.mysite.com
ErrorLog:/var/log/mysite.error.log
CustomLog:/var/log/mysite.access.log
</VirtualHost>

This may or may not be right but I have never tinkered with apache configuration files in the wild and that apart it is not at all clear to me that this will stop anothercrappysite.com pointing to my IP address and displaying my content.

I'd be much obliged to anyone who might be able to tell me how I do things right here.

ps - from what I can see I do not appear to have an httpd.conf file. Just apache2.conf.

There is perhaps another way to do this by writing an htaccess rule that specifically redirects all requests that do not contain mysite.com or www.mysite.com to the default 404 page?

For the benefit of anyone who comes here in quest of a solution to a similar issue here is what to do

<VirtualHost ip_address:80>
ServerAdmin admin@...
DocumentRoot /var/www/html/
ServerName mysite.com
ServerAlias www.mysite.com
ErrorLog /var/log/mysite.error.log
CustomLog /var/log/mysite.access.log combined
RewriteEngine On
RewriteCond  %{HTTP_HOST}  !^(www.mysite|mysite).com$ [NC]
RewriteRule ^ [F]
</VirtualHost>

It goes without saying that you should

  • Put in the IP address of your server
  • Use the domain name of your site
  • Ensure that mod_rewrite is enabled

You can check to see what modules are enabled via a simple PHP script

<?php
print_r(apache_get_modules());
?>

If mod_rewrite does not figure in that list you can usually get it by simply copying it from

/etc/apache2/mods-available to
/etc/apache2/modes-enabled

and then restarting Apache

service apache2 restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM