在Apache的000-default中只允许localhost

y53ybaqx  于 2022-11-16  发布在  Apache
关注(0)|答案(4)|浏览(164)

如何在Apache 2中只允许本地主机?

  • 我的/etc/apache 2/已启用站点/000-默认为 *
<VirtualHost *:80>
        ServerAdmin webmaster@localhost

DocumentRoot /home/masi/Dropbox/a
<Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/masi/Dropbox/a/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                deny from all                             // Problem HERE!
        allow from 127.0.0.1
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

我浏览到http://localhost/index.php失败。我得到Forbidden

ubof19bj

ubof19bj1#

切换allow、deny的顺序(您希望先拒绝所有,然后再允许localhost)。
变更:

Order allow,deny

收件人:

Order deny,allow

(这是默认行为)

jc3wubiy

jc3wubiy2#

更简单。看看“/usr/shre/doc”的配置:)复制和粘贴!

<Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
 </Directory>
busg9geu

busg9geu3#

回复摩诃的回答

这是适合我的文件。你可以在/var/www的地方找到你想要的。

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order deny,allow
                deny from all
        allow from 127.0.0.1
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
</VirtualHost>
tcbh2hod

tcbh2hod4#

如果要配置多个虚拟主机,其中一些允许从本地主机外部访问,而另一些不允许,则在遵循此页面上其他答案中的修复后,原始问题中的方法将有效。
但是,如果希望只允许localhost,则在顶层配置中更改ListenServerName即可。因此,可能会在系统防火墙中阻塞该端口。

Listen 127.0.0.1:80
ServerName localhost:80
  • 我认为演示配置防火墙的示例超出了此处的范围,因为可能会使用许多不同的防火墙。*

相关问题