我们经常因为各种各样的原因,不希望别人通过IP地址来访问我们的网站。对于使用Apache Web服务器的Linux 服务器来说,想要禁止别人通过IP地址来访问我们的网站非常容易,只需要在Apache的配置文件中,增加如下所示的一些代码就可以了。
对于CentOS系统来说,配置文件的路径为:/etc/httpd/conf/httpd.conf 。如果是Debian或者Ubuntu系统,则配置文件的位置为:/etc/apache2/sites-available/000-default.conf。
# CentOS
vim /etc/httpd/conf/httpd.conf
# Debian & Ubuntu
vim /etc/apache2/sites-available/000-default.conf
Apache 2.2 版本的可以使用如下代码:
<VirtualHost *:80>
ServerName 114.114.114.114
<Location />
Order Allow,Deny
Deny from all
</Location>
</VirtualHost>
禁止通过IP访问443端口:
<VirtualHost *:443>
ServerName 114.114.114.114
Redirect 403 /
DocumentRoot /
</VirtualHost>
如果是Apache 2.4 版本的,可以使用如下的版本:
<VirtualHost *:80>
ServerName 114.*.*.*
DocumentRoot /var/www
#This part here, is crucial.
<Location />
Require all denied
</Location>
</VirtualHost>
禁止通过IP访问443端口:
<VirtualHost *:443>
ServerName 114.55.169.11
Redirect 403 /
DocumentRoot /
</VirtualHost>
在Ubuntu系统上,如果为Apache 配置了多个虚拟主机,部署了多个域名网站,那么多个配置文件会按默认的排序规则,排在最前面的网站的配置最先读取和生效。这也就意味着,如果未屏蔽通过服务器IP访问网站,则访问的是配置文件排序最靠前的网站。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/configuration-block-ip-access-website/