首页 文章

外部URL的反向代理 - Apache

提问于
浏览
0

我配置了我的apache,以便它可以将我的请求转发到外部URL,例如google.com,但反向代理不起作用 .

<VirtualHost *:443>
ServerName authtest.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
  Order allow,deny
  Allow from All
</Proxy>
<LocationMatch "/google">
  ProxyPass https://www.google.com/
  ProxyPassReverse https://www.google.com/
</LocationMatch>
</VirtualHost>

我可以反向代理外部网站吗?

2 回答

  • 1

    我可以反向代理外部网站吗?

    是的,但有明显的缺点 .

    注意:当我尝试你的配置时,我在日志中得到 SSL Proxy requested for [...] but not enabled [Hint: SSLProxyEngine] 所以我添加了 SSLProxyEngine on .

    主机问题

    向服务器发出HTTP / 1.1请求时,会自动在请求中添加主机名 . 代理它们时,您有两种可能性:

    [browser] --(Host: authtest.com)--> [apache proxy] --(Host: authtest.com)--> Google
    

    要么

    [browser] --(Host: authtest.com)--> [apache proxy] --(Host: google.com)--> Google
    

    第一个是你得到的 ProxyPreserveHost On . Google服务器无法处理 authtest.com 的请求,您应该删除此行 .

    即使在第二种情况下,您也可能遇到问题 . ProxyPassReverse 将处理重定向但仅针对给定域:我'm in France, google.com redirects me to google.fr (a different domain) and the reverse proxy doesn' t重写重定向 .

    另一个问题是引用者:如果服务看到来自不同网站的图像/ css / js请求,则可能将其视为带宽吸收并阻止它们 . 现在,你需要重写响应的html(mod_proxy_html会有所帮助,但它不是一个银弹) .

    路径问题

    在您的示例中,您将<authtest> /google 代理到<google> / . 如上所述,您需要重写html:绝对链接/资源将无法工作,除非您的服务器在任何地方添加 /google . 相对链接/资源相同(但边缘情况更多) . 如果您拥有后端服务器,则可以在html / css / js文件中检查URL . 在这里,如果使用js在浏览器中动态构建url,则无法执行任何操作 .

    如果您可以将 / 代理为 / (或 /whatever/whatever ),则可以避免此处出现大量问题 .

  • 0

    Chech this GIT Repo我分叉了一个GIT Repo并定制它以适应场景:

    [browser] --(Host: google.local)--> [apache proxy] --(Host: google.nl)--> Google
    

    Apache配置如下:

    <VirtualHost *:80>
            ServerName google.local
        SSLProxyEngine on
        ProxyRequests Off
        <Proxy *>
            Order allow,deny
            Allow from All
        </Proxy>
            ProxyPass / https://www.google.nl/
            ProxyPassReverse / https://www.google.nl/
    
            ErrorLog /var/log/apache2/google.local-error.log
            CustomLog /var/log/apache2/google.local-access.log combined
    
    </VirtualHost>
    

相关问题