我正在使用 Apache 构建代理,它根据 URL 代理所有内容。
我的网络服务器在本地运行:http://proxy
当我导航到 http://proxy/{insert-url-here}
我的服务器代理 http://proxy/{insert-url-here}
到 {insert-url-here}
,html正确加载,但资源没有加载。
出现这个问题的原因是从我的服务器发起的对所有资源的请求都是使用http://proxy
作为宿主定向的。我希望请求使用 http://proxy/{insert-url-here}
作为主机 URL,以便请求可以找到所有资源。
我当前在 Apache 中的 VirtualHost 配置非常简单:
ServerAdmin webmaster@company.com
DocumentRoot "C:/proxy/htdocs"
ServerName http://proxy
ServerAlias http://proxy
ErrorLog "logs/error-ssl.log"
CustomLog "logs/access-ssl.log" common
ProxyRequests Off
ProxyPreserveHost On
RequestHeader unset Accept-Encoding
<Location />
Order allow,deny
Allow from all
</Location>
ProxyPassMatch /(.*) http://$1
ProxyPassReverse /(.*) http://$1
所需的解决方案:
我想将 http://proxy/{insert-url-here}
正确地代理到 {insert-url-here}
。如果我必须完全更改当前配置也没关系。
希望大家帮帮我。
最佳答案
我明白你的问题。我有一个类似的用例,我已经在 Apache 2.3 中对其进行了测试。 确保禁用 ProxyPreserveHost,这样您就不会传递当前主机。此选项可能会破坏一些动态 URL。
您可以通过结合使用 Mod_rewrite 和 Mod_proxy 来解决其余问题。 您需要分两部分解决此问题。您需要代理所有内容,然后修复链接。
# Fix broken links -> 确保你请求的 url 不包含在内
RewriteCond %{REQUEST_URI} !(www.*.nl)
# 从 HTTP referer 中获取请求的 url 并使其可用于 %1 变量的巧妙技巧
RewriteCond %{HTTP_REFERER} (www.*.nl)
# 使用上面 RewriteCond 中的基本 url 重写对代理的所有请求,并通过 Regex 捕获的组使用此规则中的捕获组
RewriteRule/(.*) http://proxy/%1/$1
# 仅当断开的链接被修复时才代理
RewriteCond %{REQUEST_URI} www.*.nl
# 这个规则确保你代理一切,这将导致你必须重写的断开链接
RewriteRule/(.*) http://$1 [P]
关于Apache 代理 : Proxy based on url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32476486/