我设置了一些 Apache mod_rewrite 规则,用于将 Maven 部署从一个 URL/服务器重定向到另一个。我们正在从 Nexus 迁移到 Artifactory,需要设置这些重定向,以便团队在一段时间内仍可以使用 Nexus URL,而不会中断构建。以下是我们的 mod_rewrite 规则示例:
RewriteRule ^/nexus/content/repositories/nexus-repository/(.*)$ https://artifactory-instance.net/repository/$1 [NE,R=301,L]
该规则成功将 GET 请求重定向到新的 Artifactory URL,但我们遇到了授权标头在重定向时被丢弃的问题。我们收到 401 错误,通过运行 cURL PUT 命令推送单个工件,我可以看到授权标头被丢弃:
...
> PUT /nexus/content/repositories/nexus-repository/com/maven/hello-world/1.0.0/hello-world-1.0.0.jar HTTP/1.1
> Host: nexus-instance.com
> Authorization: Basic XXXXXXXXXX
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 2540
> Expect: 100-continue
>
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 23 Apr 2020 17:27:22 GMT
< Server: Apache/2.4.37 (Red Hat Enterprise Linux)
< Location: https://artifactory-instance.net/repository/com/maven/hello-world/1.0.0/hello-world-1.0.0.jar
< Content-Length: 311
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
...
> PUT /repository/com/maven/hello-world/1.0.0/hello-world-1.0.0.jar HTTP/1.1
> Host: artifactory-instance.net
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 2540
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 401 Unauthorized
< Date: Thu, 23 Apr 2020 17:27:22 GMT
...
* Authentication problem. Ignoring this.
< WWW-Authenticate: Basic realm="Artifactory Realm"
< Content-Type: application/json;charset=ISO-8859-1
< Transfer-Encoding: chunked
...
cURL 有一个名为 的标志--location-trusted
。当我将它应用于我的测试 cURL 命令时,它会将授权标头传递给重定向。
我的问题是,httpd 中是否存在任何等效项--location-trusted
?我该怎么做才能通过重定向传递身份验证标头?
答案1
我弄清楚了如何才能让它工作。mod_rewrite 无法让它工作,但使用 mod_proxy 我能够让它工作:
ProxyPreserveHost Off
SSLProxyEngine On
<Location /nexus/content/repositories/nexus-repository>
ProxyPass https://artifactory-instance.net/repository
ProxyPassReverse https://artifactory-instance.net/repository
</Location>
它不会将用户重定向到 URL,但它会在 nexus URL 中显示 Artifactory 内容。