使用 NTLM 身份验证的站点的 Apache 反向代理使用 mod_rewrite 失败,但 mod_proxy 成功

使用 NTLM 身份验证的站点的 Apache 反向代理使用 mod_rewrite 失败,但 mod_proxy 成功

我们在 Exchange 服务器前面有一个反向代理服务器,并且想要锁定更多路径。最小化示例:

失败(但适用于所有不需要身份验证的页面):

<VirtualHost 192.168.1.81:443>
    ServerName autodiscover.example.com
    SSLEngine On
    SSLProxyEngine On
    Include conf/sslcert.conf
    RewriteEngine On

    RewriteRule (.*) https://exchangecluster.example.com$1 [P,L]
    ProxyPassReverse / https://exchangecluster.example.com/
</VirtualHost>

作品:

<VirtualHost 192.168.1.81:443>
    ServerName autodiscover.example.com
    SSLEngine On
    SSLProxyEngine On
    Include conf/sslcert.conf
    RewriteEngine On

    ProxyPass / https://exchangecluster.example.com/
    ProxyPassReverse / https://exchangecluster.example.com/
</VirtualHost>

使用重写规则时,请求会通过,并以 401 响应,并按预期提供 WWW-Authenticate 选项。使用 ProxyPass,用户的身份验证可以正常工作,而使用 RewriteRUle,系统会不断提示用户进行身份验证,我认为这与 NTLM 有关。

StackExchange 中有几个问题表明 mod_proxy 无法处理 NTLM 直通身份验证,但在这种情况下它可以正常工作。

可以通过处理不需要身份验证的路径,然后拒绝应该阻止的路径,然后执行全局 ProxyPass 来解决 mod-rewrite 问题。

解决方法:

<VirtualHost 192.168.1.81:443>
    ServerName autodiscover.example.com
    SSLEngine On
    SSLProxyEngine On
    Include conf/sslcert.conf
    RewriteEngine On

    # Block all requests except the autodiscover URLs
    RewriteCond "%{REQUEST_URI}" "!^/autodiscover/autodiscover\.(?:xml|json|svc)$" [NC]
    RewriteRule ^ - [F]

    ProxyPass / https://exchangecluster.example.com/
    ProxyPassReverse / https://exchangecluster.example.com/
</VirtualHost>

A评论另外一个问题建议使用 mpm_prefork_module 而不是 mpm_worker_module。我检查了我们的 00_mpm.conf,我们正在使用 worker,并且它与 proxypass 一起工作,所以感觉我们缺少 mod_rewrite 代理选项的某些功能。

我发现大多数问题都是关于通过 NTLM 进行反向代理身份验证。这些问题是关于将身份验证传递给服务器并保持会话完整,而不是从 Apache 进行身份验证(假设这不是实现此目的所必需的)。

使用 mod_rewrite 时是否需要启用任何设置来允许代理?

相关内容