apache禁止使用IP访问的实现方法

用apache搭建的WEB服务器,如何让用户只能通过设定的域名访问,而不能直接通过服务器的IP地址访问呢?百度了一下有几个方法,没有试验过.
方法一:在httpd.conf文件最后面,加入以下代码
NameVirtualHost 221.*.*.*
ServerName 221.*.*.*
Order Allow,Deny
Deny from all
DocumentRoot “c:/web”
ServerName www.xxx.com
     
说明:蓝色部分是实现拒绝直接通过221.*.*.*这个IP的任何访问请求,这时如果你用221.*.*.*访问,会提示拒绝访问。红色部分就是允许通过www.xxx.com这个域名访问,主目录指向c:/web(这里假设你的网站的根目录是c:/web)

方法二:在httpd.conf文件最后面,加入以下代码
NameVirtualHost 221.*.*.*
DocumentRoot “c:/test”
ServerName 221.*.*.*
DocumentRoot “c:/web”
ServerName www.xxx.com
说明:蓝色部分是把通过221.*.*.*这个IP直接访问的请求指向c:/test目录下,这可以是个空目录,也可以在里面建一个首页文件,如index.hmtl,首面文件内容可以是一个声明,说明不能通过IP直接访问。红色部分的意思跟方法一是一样的。
注:修改后要重启apache

方法三:建一个虚拟主机,
ServerName IPAddress (比如192.168.0.1)
DocumentRoot /usr/local/apache/nosite/
这样就可以了。
在/usr/local/apache/nosite/下面建一个index.htm,提示用户,不允许IP访问。或其它出错信息。
方法四:
ServerName IPAddress (比如192.168.0.1)
DocumentRoot /usr/local/apache/nosite/
Order deny,allow
Deny from all

Leave a comment

Your comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word