Apache-我可以只用Apache制作一个代理服务器吗


Apache - Can I make a proxy server with just Apache?

我在一个服务器中有一堆子域:

  • a.example.com
  • b.example.com
  • news.example.com

所有这些都在同一个Apache虚拟主机中。

我需要使用a和b子域内的新闻子域提供的提要。提要通常如下所示:

  • news.example.com/news/a
  • news.example.com/news/b

在a和b子域上,我使用jquery的ajax函数从新闻源加载数据,并将其呈现在a和b上。由于同源策略,这最初不起作用。

我可以通过将Access-Control-Allow-Origin "*"添加到我的Apache配置文件中来覆盖它。

但这只适用于Firefox、Chrome和Safari。Internet explorer似乎忽略了该指令。

因此,我需要创建一个代理。

我需要的是Apache检测到的所有子目录(例如/proxy)中的一个新目录,并重定向到news.example.com,无论子域是什么。因此:

  • a.example.com/proxy/news/a->返回news.example.com/news/a的内容
  • b.example.com/proxy/news/b->返回news.example.com/news/b的内容

我可以直接在Apache+子模块(例如,mod_rewrite)中执行此操作吗?还是需要使用PHP之类的脚本语言来执行此操作?

您需要ProxyPass指令。

ProxyPass /proxy/news/a http://news.example.com/news/a

最后,我们能够使用两个模块的组合来制作代理:mod_rewritemod_proxy

语法如下:

rewriteEngine on
rewriteRule proxy/(.+)$ http://news.example.com/$1 [P]

结尾的[p]告诉规则"充当代理",如果没有mod_proxy,它就不起作用。如果没有它,apache会进行"重定向"(页面顶部的url会更改),而不是"只为页面提供服务"。

Apache可以配置为使用Apache:

考虑一下这个工作示例代码(代理部分):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName ci.testserver.com
    ServerAlias ci
    ProxyRequests Off
    <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
        ProxyPreserveHost on
        ProxyPass / http://localhost:8080/
</VirtualHost>