【发布时间】:2012-05-25 11:52:43
【问题描述】:
我基本上想要完成的是让我的主网站运行一个用 Go 编写的 CMS。这将位于 www.example.com。
我还有一些用 PHP 编写的应用程序,它们位于目录中,例如 www.example.com/clients/
如何在使用 Go 内置 Web 服务器提供 example.com 的同时使用 Apache/PHP 提供 example.com/clients?
【问题讨论】:
我基本上想要完成的是让我的主网站运行一个用 Go 编写的 CMS。这将位于 www.example.com。
我还有一些用 PHP 编写的应用程序,它们位于目录中,例如 www.example.com/clients/
如何在使用 Go 内置 Web 服务器提供 example.com 的同时使用 Apache/PHP 提供 example.com/clients?
【问题讨论】:
通过 Apache2 中的 mod_proxy,您可以将不同的路径代理到 localhost 或您的服务器可访问的任何其他位置的不同目的地,包括您的本地网络(如果您的服务器可以访问它)。
为此,您将使用ProxyPass(Apache2 Docs for ProxyPass,这是非常有用的阅读),如下例所示:
<VirtualHost *:80>
ServerName some.example.host.xyz
DocumentRoot /var/www/your-document-root
Alias /clients/ /var/www/clients/
ProxyPass /clients/ !
ScriptAlias /something-using-cgi/ /var/www/cgi-stuff/
ProxyPass /something-using-cgi/ !
ProxyPreserveHost On
ProxyPass / http://localhost:9876/
ProxyPassReverse / http://localhost:9876/
ProxyPass /elsewhere/ http://elsewhere.example.host.xyz:1234/
ProxyPassReverse /elsewhere/ http://elsewhere.example.host.xyz:1234/
</VirtualHost>
您需要确保设置代理安全,以使外部用户也无法将您的反向代理用作正向代理。您可以通过ProxyRequests 执行此操作,如官方 Apache2 文档中所述。我在服务器上执行此操作的方式是将其放入您的服务器范围的配置中(您应该自行验证这是否足够安全):
# disables forward proxy
ProxyRequests Off
【讨论】:
【讨论】:
<Location /clients/> 怎么样?或者 location /clients/ { … }; 在 nginx 上?
mod_rewrite 和 mod_proxy 进行代理 URL 过滤,以便您可以将特定路径或目录推送到不同的后端目标,例如基于 Go 的服务器或其他位置,同时保留其他部分根据需要您在 Apache 中的路径。