You need to enable JavaScript to run this app. Apache2如何基于Location匹配将请求路由到指定系统目录
优惠活动
大模型
产品
解决方案
定价
更多

Apache2如何基于Location匹配将请求路由到指定系统目录

Apache实现/maintenance路径映射到指定目录方案

你只需要在现有VirtualHost配置中添加Alias映射、目录权限配置即可,和Nginx的location修改root逻辑类似,完整修改后的配置片段如下:

<VirtualHost *:443>
    ServerName blaah.com
    SSLCertificateFile /etc/ssl/certs/blaah.crt
    SSLCertificateKeyFile /etc/ssl/private/blaah.key
    SSLCertificateChainFile /etc/ssl/certs/DigiCertCA.crt
    DocumentRoot /var/www/blaah
    Alias /webalizer /var/www/webalizer
    # 新增:将/maintenance路径映射到指定维护目录
    Alias /maintenance /var/www/maintenance
    <Directory /var/www/webalizer>
        Allow from 192.168.0.0/16
        Deny from all
        AuthType Basic
        AuthName webalizer
        AuthUserFile /var/www/webalizer/.htpasswd
        Require valid-user
    </Directory>
    # 新增:维护页目录权限配置
    <Directory /var/www/maintenance>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        DirectoryIndex index.html
        Require all granted
    </Directory>
    ProxyPass /gogs http://127.0.0.1:3000
    <LocationMatch "^/blaah/blaah/">
        Deny from all
    </LocationMatch>
    <LocationMatch "^/maintenance">
        # 替换为你自己的测试IP,多个IP用空格分隔
        Allow from 192.168.1.100
        Deny from all
    </LocationMatch>
    # 可选:后续需要全站切维护时可添加以下规则(需先启用rewrite模块:a2enmod rewrite)
    # RewriteEngine On
    # # 排除你自己的测试IP
    # RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.100$
    # # 排除维护页路径避免死循环
    # RewriteCond %{REQUEST_URI} !^/maintenance
    # # 返回503维护状态码跳转至维护页
    # RewriteRule ^(.*)$ /maintenance/index.html [R=503,L]
</VirtualHost>

如果你的Apache版本为2.4及以上,推荐使用Require ip 你的IP替代旧的Allow from语法,兼容性更好

生效验证

  1. 先测试配置语法正确性:
    • Debian/Ubuntu系统执行:apache2ctl configtest
    • CentOS/RHEL系统执行:httpd -t
  2. 确认返回Syntax OK后重载配置生效:
    • Debian/Ubuntu系统执行:systemctl reload apache2
    • CentOS/RHEL系统执行:systemctl reload httpd
  3. 用你配置的允许IP访问blaah.com/maintenance即可看到维护提示页,非允许IP访问会返回403拒绝。

内容的提问来源于stack exchange,提问作者Taavi Ansper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 20:06:03