【问题标题】:Apache2 mod_auth_form too many redirects under LocationApache2 mod_auth_form Location 下的重定向太多
【发布时间】:2015-08-22 21:36:50
【问题描述】:

我正在尝试使用mod_auth_form 构建代理 HTTP 授权页面

我的目标是在DocumentRoot 目录中有一个单独的身份验证页面,然后一旦用户连接,只需将所有路由代理到“真实”应用程序,在本地主机上使用另一个端口运行。

我在根 Location 下使用 Auth 指令设置了我的虚拟主机:

<VirtualHost *:80> 
    ServerName subdomain.example.com

    DocumentRoot /var/www/subdomain.example.com/web/

    <Location /login.html>
        Order allow,deny
        Allow from all
    </Location>

    <Location />
        SetHandler form-login-handler

        AuthType Form
        AuthName realm
        AuthFormProvider file
        AuthUserFile /var/www/subdomain.example.com/.htpasswd
        AuthFormLoginRequiredLocation "http://subdomain.example.com/login.html"

        require valid-user

        Session On
        SessionCookieName session path=/
        SessionCryptoPassphrase any-secret-passphrase      
    </Location>

    ProxyPass /login.html !
    ProxyPassReverse /login.html !
    ProxyPass / http://localhost:8888
    ProxyPassReverse / http://localhost:8888

    ErrorLog ${APACHE_LOG_DIR}/subdomain.example.com/error.log
    CustomLog ${APACHE_LOG_DIR}/subdomain.example.com/access.log combined
</VirtualHost>

编辑 我需要的只是颠倒&lt;Location&gt;&lt;/Location&gt; 指令的顺序......并为表单处理程序添加一个特殊的位置。 工作解决方案:

<VirtualHost *:80> 
    ServerName subdomain.example.com

    DocumentRoot /var/www/subdomain.example.com/web/

    <Location />
        AuthType Form
        AuthName realm
        AuthFormProvider file
        AuthUserFile /var/www/subdomain.example.com/.htpasswd
        AuthFormLoginRequiredLocation "http://subdomain.example.com/login.html"
        AuthFormLoginSuccessLocation "http://subdomain.example.com/"

        require valid-user

        Session On
        SessionCookieName session path=/
        SessionCryptoPassphrase any-secret-passphrase      
    </Location>

    <Location /login_check.html>
        SetHandler form-login-handler

        AuthType Form
        AuthName realm
        AuthFormProvider file
        AuthUserFile /var/www/subdomain.example.com/.htpasswd
        AuthFormLoginRequiredLocation "http://subdomain.example.com/login.html"
        AuthFormLoginSuccessLocation "http://subdomain.example.com/"

        require valid-user

        Session On
        SessionCookieName session path=/
        SessionCryptoPassphrase any-secret-passphrase      
    </Location>

    <Location /login.html>
        Order allow,deny
        Allow from all
    </Location>

    ProxyPreserveHost On
    ProxyPass /login.html !
    ProxyPassReverse /login.html !
    ProxyPass / http://localhost:8888
    ProxyPassReverse / http://localhost:8888

    ErrorLog ${APACHE_LOG_DIR}/subdomain.example.com/error.log
    CustomLog ${APACHE_LOG_DIR}/subdomain.example.com/access.log combined
</VirtualHost>

当我尝试访问 subdomain.example.com 时,我被重定向到 subdomain.example.com/login.html(这很好!)

这个 /var/www/subdomain.example.com/web/login.html 页面的内容:

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8' />
        <meta name='viewport' content='width=device-width' />
        <title>Authentication</title>
    </head>
    <body>
        <form method='POST' action='/login_check.html'>
            <div class='form-group'>
                <label for='httpd_username'>Username</label>
                <input id='http_username' class='form-control' type='text' name='httpd_username' value='' />
            </div>
            <div class='form-group'>
                <label for='httpd_password'>Password</label>
                <input id='httpd_password' class='form-control' type='password' name='httpd_password' value='' />
            </div>
            <div class='form-group'>
                <input class='btn btn-success' type='submit' name='login' value='Login' />
            </div>
        </form>
    </body>
</html>

但是,这个 login.html 页面永远不会显示,我收到 TOO_MANY_REDIRECTS 错误:

http://subdomain.example.com/login.html 的网页导致了过多的重定向。

似乎这个特殊的路由必须被 Auth 进程“向下锁定”......但我不知道如何启用它......

我尝试添加另一个ErrorDocument 401 /login.html 指令,但它没有改变任何东西。

【问题讨论】:

  • 您可以尝试将http://localhost:8888 映射到您的子域的子目录(例如ProxyPass /proxy/ http://localhost:8888/),这样它就不会干扰您的login.html — 至少作为测试的临时解决方案。如果可行,您可以调查为什么 Location /login.html 似乎没有效果。 (另外,ProxyPass 的两个参数应该都带有斜杠,或者都没有。)
  • 谢谢!确实,子文件夹解决方案有效;除了我在我的 URL 中以 /proxy/ 结尾,我宁愿避免……在保留子文件夹的同时,有没有办法避免它?另一方面,我如何调查为什么我的&lt;Location /login.html&gt; 不起作用? (ProxyPass 参数是什么意思?)
  • 应该有一种方法可以使您的原始目录布局正常工作,但如果不亲自测试整个事情,我不能确定如何。我敢打赌,Apache 正在按照找到它们的顺序评估 &lt;Location&gt; 块,因此第二个块将覆盖第一个块。你试过交换它们吗?至于ProxyPass 参数:要么有ProxyPass /monit/ http://localhost:2812/ 要么有ProxyPass /monit http://localhost:2812。我不完全确定为什么,但如果只有一个参数有斜杠,我以前无法访问某些页面。
  • 您可能还想使用retry=0 参数在代理站点不可用时禁用 Apache 的重试延迟(否则 Apache 将在您尝试访问代理站点一分钟后提供错误页面,而localhost:8888 已关闭):ProxyPass / http://localhost:8888/ retry=0(详情请查看ProxyPass documentation 的参数部分。)
  • 天哪...这确实只是指令顺序的问题...将 /login.html 放在最后解决了问题...我真的认为这会更容易。非常感谢!!我还要检查retry=0 参数:)

标签: apache proxy virtualhost basic-authentication mod-auth-form


【解决方案1】:

Apache 按照它看到的顺序解析配置指令,并将它们应用到指定的位置和它下面的所有东西,所以你的&lt;Location /&gt; 块中的require valid-user 覆盖了你的&lt;Location /login.html&gt; 块中的Allow from all — 你结束了up 需要身份验证才能访问任何内容(包括login.html),因此您在访问login.html 时开始未经授权,并被重定向到login.html 以登录。有适合您的循环。

【讨论】:

    【解决方案2】:

    我在使用 Apache 2.4 时遇到了同样的问题,但原因不同。使用 Apache 2.4+,而不是使用:

    <Location /login.html>
        Order allow,deny
        Allow from all
    </Location>
    

    用途:

    <Location /login.html>
        Require all granted
    </Location>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 2021-03-25
      • 2015-06-13
      • 2019-09-11
      相关资源
      最近更新 更多