大网数据广告活动
网站运营与故障问答

Apache2: Require directive not working

· 52CE 我爱测

I'm a tad confused after reading these docs: https://httpd.apache.org/docs/2.4/sections.html https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html I wrote this site definition: $ cat /etc/apache2/sites-enabled/adminer.conf Al…

问题原文

原帖链接 · Bernd Wechner · CC BY-SA 4.0

I'm a tad confused after reading these docs:

I wrote this site definition:

$ cat /etc/apache2/sites-enabled/adminer.conf
Alias /adminer  "/var/www/adminer.php"

<Location "/adminer">
        <RequireAny>
                Require local
                Require ip 192.168
                Require ip 10
        </RequireAny>
</Location>

I restart Apache:

$ sudo service apache2 restart

and yet from a WAN situated PC I can still load:

https://example.com/adminer

I am bamboozled I admit and presume I am misunderstanding something or have misread something in the above mentioned docs. If anyone can point me in the right direction here I would be most appreciative. I would rather have access to adminer only from within the LAN and not open it up to attacks from the WAN.

EDIT: I do see this in /etc/apache2/apache2.conf:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

Can that have a higher priority perhaps than my Location specifier?

已采纳解答

原帖链接 · DocRoot · CC BY-SA 4.0

I think the "problem" is with your Alias directive.

Whilst the <Location> directive would ordinarily override the <Directory> directive, since it is processed later. When there is an Alias, the <Location> directive is processed before the Alias is evaluated, so the <Directory> container that relates to the Alias target ends up being processed later. In your case, the relevant <Directory> container has a Require all granted directive, so access is ultimately granted.

From the Apache docs for the Alias directive:

Note that you may need to specify additional <Directory> sections which cover the destination of aliases. Aliasing occurs before <Directory> sections are checked, so only the destination of aliases are affected. (Note however <Location> sections are run through once before aliases are performed, so they will apply.)

UPDATE: You should be able to use a <Files> directive, as you suggest in comments, so it's not clear why this would not work for you. For example:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
<Files adminer.php>
    <RequireAny>
        Require local
        Require ip 192.168
        Require ip 10
    </RequireAny>
</Files>
</Directory>

I'm assuming all these directives are ending up in the main server config? In which case, the order they are included could be important. Ideally, you would be creating a <VirtualHost> container for this one site, which then makes it easier to override the server config.

Note also, that you shouldn't generally be using <Location> directives to control access to local filesystem resources, as they could be easily circumvented. For example, in your case, the user could simply access /adminer.php directly. As noted in the Apache docs:

<Location> sections operate completely outside the filesystem. This has several consequences. Most importantly, <Location> directives should not be used to control access to filesystem locations. Since several different URLs may map to the same filesystem location, such access controls may by circumvented.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。