zl程序教程

您现在的位置是:首页 >  Java

当前栏目

apache 里的 ProxyPassReverse 指令

2023-02-18 16:35:09 时间

该指令允许 Apache httpd 调整 HTTP 重定向响应(HTTP redirect response) Location、Content-Location 和 URI 标头中的 URL。 当 Apache httpd 用作反向代理(或网关)以避免绕过反向代理时,这是必不可少的,因为后端服务器上的 HTTP 重定向位于反向代理之后。

只有上面特别提到的 HTTP 响应头会被重写。 Apache httpd 不会重写其他响应标头,默认情况下也不会重写 HTML 页面内的 URL 引用。 这意味着如果代理内容包含绝对 URL 引用,它们将绕过代理。 要重写 HTML 内容以匹配代理,您必须加载并启用 mod_proxy_html.

语法:

ProxyPassReverse [path] url [interpolate]

path 是本地虚拟路径的名称; url 是远程服务器的部分 URL。 这些参数的使用方式与 ProxyPass 指令相同。

例如,假设本地服务器的地址为 http://example.com/; 然后有下面的 proxy 配置:

ProxyPass         "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverse  "/mirror/foo/" "http://backend.example.com/"
ProxyPassReverseCookieDomain  "backend.example.com"  "public.example.com"
ProxyPassReverseCookiePath  "/"  "/mirror/foo/"

上面的配置不仅会导致对 http://example.com/mirror/foo/bar 的本地请求在内部转换为对 http://backend.example.com/bar 的代理请求(ProxyPass 在这里提供的功能). 它还负责在将 http://backend.example.com/bar 重定向到 http://backend.example.com/quux 时服务器 backend.example.com 发送的重定向。 Apache httpd 在将 HTTP 重定向响应转发给客户端之前将其调整为 http://example.com/mirror/foo/quux。 请注意,用于构建 URL 的主机名是根据 UseCanonicalName 指令的设置选择的。

Redirect 指令通过要求客户端在新位置重新获取资源,将旧 URL 映射到新 URL。

旧的 URL 路径是以斜杠开头的区分大小写(%-解码)的路径。 不允许使用相对路径

新 URL 可以是以方案和主机名开头的绝对 URL,也可以是以斜杠开头的 URL 路径。 在后一种情况下,将添加当前服务器的方案和主机名。

然后任何以 URL-path 开头的请求都会在目标 URL 的位置返回一个重定向请求给客户端。 匹配的 URL 路径之外的其他路径信息将附加到目标 URL。

看个例子:

# Redirect to a URL on a different host
Redirect "/service" "http://foo2.example.com/service"

# Redirect to a URL on the same host
Redirect "/one" "/two"

如果客户端请求 http://example.com/service/foo.txt,它将被告知访问 http://foo2.example.com/service/foo.txt。 这包括带有GET参数的请求,比如http://example.com/service/foo.pl?q=23&a=42,它会被重定向到http://foo2.example.com/service/foo.pl? q=23&a=42。 请注意,POST 将被丢弃。