原文地址:https://edmondscommerce.github.io/apache/blocking-direct-ip-access-in-apache.html
测试环境:
CentOS Linux release 7.3.1611 (Core)
Apache: 2.4.6
如果你是使用 Name based virtual hosts,通常你会希望禁止用户直接使用 IP 地址访问你的网站。实现方法如下:
新建配置文件:
1 | vim /etc/httpd/conf.d/block-ip-access.conf |
内容如下:
1 | <VirtualHost *:80> |
重启 Apache:
1 | systemctl restart httpd |
之后,使用 IP 直接访问,浏览器会报 403 错误,同时会在页面上显示 “Please use domain to access this site.”。
注意:如果你没有使用 Name based virtual hosts,仅仅是将域名直接解析到 Apache 服务器的话,那么同样会得到 403 错误。也就是说,需要有类似如下的设置:
1 | <VirtualHost *:80> |