【问题标题】:How can I display a remote page at a different URL as if it were at the local URL如何在不同的 URL 上显示远程页面,就好像它在本地 URL 上一样
【发布时间】:2018-03-23 19:59:36
【问题描述】:

我想模仿一个负载均衡器,如果有人去

http://example.com/mysite

他们将看到以下内容:

http://server2.example.com/mysite

http://server3.example.com/mysite

使用 PHP,我可以随机确定用户访问的站点,但我如何才能将其显示在 http://example.com/mysite?我是否以某种方式使用了占据整个窗口高度和宽度的iframe?或者有没有更好的方法?

基本上,我希望用户只在地址栏中看到http://example.com/mysite,而忽略他们正在查看页面的服务器。我不介意他们查看源代码来查看它,但我想确保 URL 相同。

这两个站点都使用 JavaScript(服务器端和客户端)、PHP 和 HTML。这些网站也在服务器端 JavaScript (Node) 上使用长轮询。

更新

感谢 Lawrence Cherone 的建议,我在 Apache 中实现了如下负载均衡器:

<VirtualHost *:80>
        ServerAdmin webmaster@example.com
        ServerName jpl.example.com

        ProxyRequests off

        <Proxy "balancer://mycluster">
                BalancerMember "http://203.0.113.22:3000"
                BalancerMember "http://203.0.113.23:3000"
        </Proxy>

        ProxyPass "/test/" "balancer://mycluster/"
        ProxyPassReverse "/test/" "balancer://mycluster/"

</VirtualHost>

剩下的两个问题:

问题 1

为了允许子目录,我必须在ProxyPassProxyPassReverse 行中添加一个斜杠。

这可以从:

jpl.example.com/test/ => http://203.0.113.22:3000

jpl.example.com/test/subdir1/ => http://203.0.113.22:3000/subdir1/

jpl.example.com/test/subdir2/ => http://203.0.113.22:3000/subdir2/

但是如果我去掉尾部的斜线就行不通了:

jpl.example.com/test

jpl.example.com/test/subdir1 

jpl.example.com/test/subdir2 

如果用户忘记输入斜杠,有什么办法让它工作吗?

问题 2

第二个问题太复杂了,所以我把它移到its own question。该问题涉及长轮询不再起作用,因为它找不到 socket.io。

【问题讨论】:

  • 设置代理,所以访问http://example.com/mysite会被转发到你的其他服务器。
  • @LawrenceCherone 嗯,你的意思是用 PHP 设置代理吗?其他网站使用 JavaScript 来获取数据等,因此不确定是否可以完全正常工作。
  • 不,我的意思是像nginxcaddy 等这样的反向代理
  • @LawrenceCherone 这听起来像是我必须进行更多研究的事情。我正在使用 Apache,所以此页面上的内容是否是您推荐的内容:httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html
  • 我还需要使用长轮询,所以也需要一些可以使用的东西。

标签: php html url


【解决方案1】:

下面将 iframe 在网站的 url 中,基于 URL 中设置的变量,它可以很容易地是一个 SESSION 变量,基于 ip 地址等是你要找的吗?

<?php
//assuming your file is named index.php
  //index.php without user_criteria shows site 2
  ///index.php?user_criteria=tdorg shows site 1
$serverURL = (isset($_GET['user_criteria']) && $_GET['user_criteria'] == 'tdorg')?"http://trumpdonald.org/":"https://giphy.com/gifs/z48aJruaX0Jsk/embed"; 
//if the user criteria is set in the URL, use that.  Otherwise, default to url 1
?>
<iframe src="<?php echo $serverURL; ?>" width="100%" height="100%" />

【讨论】:

    【解决方案2】:

    如果您正在尝试构建一个实际的负载均衡器,请不要使用 PHP。拆分服务器的负载是服务器的功能,而不是您的应用程序。但是假设您的问题背后有一些合理的原因,我会尽力回答。

    从技术上讲,您可以通过简单的包含来做到这一点:

    <?php include 'http://'.$subdomain.'.example.com/mysite'; ?>
    

    但这不会有效,因为每次请求时都会下载该页面。相反,您可能希望将页面写入文件以缓存它。所以你的 php 会做这样的伪代码:

    if cache file exists for this page
        get contents of file
        echo contents of file
    else 
        get page from the other server
        write it to a cache file
        echo contents of file
    

    【讨论】:

    • allow_url_include 存在安全风险,默认关闭。
    【解决方案3】:

    为了在用户不可见的情况下重定向到网站(在地址栏中),您可以执行以下操作:

    1. 使服务器重定向到 server2 或 server3。 (女巫你已经完成了。)
    2. 在您重定向到的页面中,插入这段 JS 代码:
    <script>
        window.history.pushState("", "[Title (ignored by most browsers)]", "[relative addres for the url you want to simulate (eg.: //example.com/mysite]");
    </script>
    

    当服务器重定向浏览器加载的页面时,此脚本将运行,将虚假地址插入地址栏。

    更多信息:Updating address bar with new URL without hash or reloading the page

    PS.: 重新加载页面会导致加载虚假地址。浏览器中的前进和后退导航按钮可能不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多