阻止用户访问特定网页有助于防止他们接触敏感信息。如果您将站点托管在 Apache 服务器上,则可以通过锁定特定 URL 来锁定站点。如果您只需要阻止访问单个页面,则可以锁定 URL,而无需锁定整个站点。使用 Apache 有多种方法可以做到这一点,因此让我们看一下在 Apache 中保护特定 URL 并阻止入侵者的七种方法。
本文将帮助您保护 Apache 中的特定 URL。例如,某个站点有一个安全区域,例如http://example.com/admin/”并且我们需要只有授权用户或IP地址才能访问/admin/部分。
1. 通过IP地址限制特定URL
首先,编辑 Apache 配置文件并在 VirtualHost 中添加以下条目。这将允许/adminURL 为 192.168.10.11 或 IP 范围(如 192.168.1.0/24)。
<Location /admin>
Order deny,allow
Deny from all
Allow from 192.168.10.11
Allow from 192.168.1.0/24
</Location>
保存 Apache 配置文件并使用以下命令之一重新启动 apache 服务。
sudo systemctl restart httpd
#On RedHat based systems
sudo systemctl restart apache2
#On Debian based systems
让我们尝试从任何其他 IP 地址访问您的网站。另外,检查配置文件中给定的 IP 地址。
2. 在特定 URL 上设置用户身份验证
您还可以为 Apache Web 服务器中的特定 URL 启用登录屏幕。为此,请编辑 Apache 配置文件并在网站 VirtualHost 部分添加以下条目。
<Location /admin>
AuthUserFile /var/www/htpasswd/.htpasswd
AuthName "Password Protected Area"
AuthType Basic
Require valid-user
</Location>
现在使用以下命令创建一个新的 htpasswd 文件并添加一个新用户。
htpasswd -cm /var/www/htpasswd/.htpasswd myuser
Output
New password:
Re-type new password:
Adding password for user myuser
重新启动 Apache 服务并访问您的站点 URL。它将提示输入登录详细信息。
sudo systemctl restart httpd
#On RedHat based systems
sudo systemctl restart apache2
#On Debian based systems
感谢您使用本文,希望本文能满足您的需求。点击这里阅读有关 apache location 指令的更多详细信息。