AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 950327
Accepted
Tobia
Tobia
Asked: 2019-01-23 23:57:17 +0800 CST2019-01-23 23:57:17 +0800 CST 2019-01-23 23:57:17 +0800 CST

Apache 配置:如何将非主机名请求限制到某些页面?

  • 772

我的 Apache 服务器有一个虚拟主机配置:

<VirtualHost *:80>
    DocumentRoot "/app/www"
    ServerName myhostname
    <Directory "/app/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

我想只允许访问myhostname作为主机名的请求。但我想拒绝主机名或服务器 IP 发出的所有其他请求:

http://myhostiname/ ALLOW
http://1.2.3.4/ (this is one of the server ip addresses) DENY

我的虚拟主机配置按预期工作。

现在我必须编辑配置以让用户通过 ip 访问一个特定路径,因为客户端无法解析本地主机名。

这是一个例子:

http://myhostiname ALLOW
http://1.2.3.4/ DENY
http://1.2.3.4/any/path DENY
http://1.2.3.4/allowed/path ALLOW
http://1.2.3.4/allowed/path/subpath ALLOW

我在新的虚拟主机中尝试了该<Location>元素:

<VirtualHost 0.0.0.0:80>
    DocumentRoot "/app/www"
    <Directory "/app/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    <Location "/">
      AllowOverride None
      Order Deny,Allow
      Deny from all
    </Location>
    <Location "^/allowed">
      Allow from all
    </Location> 
</VirtualHost>

但这拒绝了除了主机名请求之外的所有内容。我错过了什么?

apache-2.4
  • 1 1 个回答
  • 306 Views

1 个回答

  • Voted
  1. Best Answer
    Freddy
    2019-01-24T07:38:39+08:002019-01-24T07:38:39+08:00

    我会将您的虚拟主机配置拆分为两个或更多虚拟主机。可以在一个虚拟主机中完成所有操作,但我发现“分离的”配置更易于阅读和记录。

    <VirtualHost *:80>
        # default match for port 80
        # matches domain set by ServerName (and possibly other domains unless specified in other vhosts)
        ServerName myhostname
        DocumentRoot "/app/www"
    
        <Location "/">
            # copied from your example. if not needed, you can remove this <Location/>-block.
            Options Indexes FollowSymLinks
            AllowOverride All
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error-myhostname.log
        CustomLog ${APACHE_LOG_DIR}/access-myhostname.log combined
    </VirtualHost>
    
    <VirtualHost 1.2.3.4:80>
        # matches all requests to ip address and port
        DocumentRoot "/app/www"
    
        # initially: all requests forbidden
        <Location "/">
            # Options and AllowOverride copied from first vhost. remove if not needed.
            Options Indexes FollowSymLinks
            AllowOverride All
    
            Require all denied
        </Location>
    
        # allow access to everything below "/allowed/path/"
        <Location "/allowed/path/">
            Require all granted
        </Location>
    
        # use separate logfile
        ErrorLog ${APACHE_LOG_DIR}/error-1234.log
        CustomLog ${APACHE_LOG_DIR}/access-1234.log combined
    </VirtualHost>
    
    <VirtualHost *:*>
        # default match
        # fallback for any other ports/ip addresses/domains we might have forgotten/misconfigured
        DocumentRoot "/app/www"
    
        # all requests forbidden
        <Location "/">
            Require all denied
        </Location>
    
        # again, use separate logfile
        ErrorLog ${APACHE_LOG_DIR}/error-default.log
        CustomLog ${APACHE_LOG_DIR}/access-default.log combined
    </VirtualHost>
    
    • 1

相关问题

  • Apache2 - SSL 不工作

  • Apache <VirtualHost> 标签,如果客户端的 IP = x 则不重定向... 怎么做?

  • 我无法启用我的网站?[关闭]

  • SELinux 阻止 Apache 写入文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve