Apache2 mod_auth_form 位置下重定向过多

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

我正在尝试使用 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>

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

<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错误:

The webpage at http://subdomain.example.com/login.html has resulted in too many redirects.

似乎这条特殊的路线必须由身份验证过程“锁定”...但我不知道如何启用它...

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

最佳答案

Apache 按照看到的顺序解析配置指令,并将它们应用到指定位置及其下面的所有内容,因此 require valid-user来自您的<Location /> block 覆盖 Allow from all来自您的<Location /login.html>阻止 - 您最终需要身份验证才能访问任何内容(包括 login.html ),因此您在访问 login.html 时开始未经授权并被重定向到login.html登录。这是适合您的循环。

关于Apache2 mod_auth_form 位置下重定向过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708824/

相关文章:

linux - Apache 将 HTTPS 重定向到 HTTP

apache - 如何使用代理和虚拟主机将 session 从 apache2 传递到 tomcat7

php - 使用 lamp-server^ 包安装 LAMP 与单独安装 Apache、PHP、MySQL 的优缺点

docker - 如果使用 docker 部署,为什么 MERN 应用程序无法与后端通信?

node.js - 通过企业代理地址将 Node.js 与 Salesforce 集成,并从 CSV 文件执行批量插入操作

php - 如何在 XAMPP 上创建虚拟主机

apache - 不要在子域上应用重写引擎......?

php - Apache 不允许从网络上的其他设备访问

php - 在现有 Apache 服务器上运行 Laravel

proxy - 使用 webpack 开发服务器,如何代理除 "/app"之外的所有内容,但包括 "/app/api"