英文:
What are my options to deploy Go applications alongside PHP applications?
问题
我基本上想要实现的是让我的主要网站运行一个用Go编写的CMS。这将位于www.example.com。
我还有一些用PHP编写的应用程序,位于目录中,例如www.example.com/clients/。
我如何在使用Apache/PHP提供example.com/clients的同时,使用Go内置的Web服务器提供example.com?
英文:
What I'm basically trying to accomplish is having my main website running a CMS written in Go. This will be located at www.example.com.
I also have applications written in PHP located in directories, such as www.example.com/clients/
How can I serve example.com/clients using Apache/PHP while serving example.com using Go built-in web server?
答案1
得分: 4
Andrew Gerrand在他的博客文章中对此进行了很好的解释,针对nginx的原理与Apache相同。
您想将Apache设置为Go应用程序的反向代理,用于处理传入的请求。
对于Apache,您需要查看mod_proxy模块。
英文:
Andrew Gerrand has a good blog post about this for nginx but the principle is the same for Apache.
You want to set up Apache as a reverse proxy for requests coming in for the Go application.
For Apache you want to look at mod_proxy
答案2
得分: 4
通过在Apache2中使用mod_proxy
,您可以将不同的路径代理到本地主机或服务器可访问的任何其他位置,包括本地网络(如果您的服务器可以访问它)。
为此,您可以使用ProxyPass
(Apache2文档中的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>
您需要确保设置代理安全性,以防止外部用户将您的反向代理用作转发代理。您可以通过在官方Apache2文档中描述的ProxyRequests
来实现。我在服务器上的做法是将以下内容放入全局配置中(您应该自行验证其安全性):
# 禁用转发代理
ProxyRequests Off
英文:
Via mod_proxy
in Apache2, you can proxy different paths into different destinations at localhost or anywhere else accessible by your server, including within your local network (if your server can access it).
For this you would use ProxyPass
(Apache2 Docs for ProxyPass, which is very useful reading) like the example below:
<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>
You'll want to be sure that you set your proxy security such that external users can't use your reverse proxy as a forward proxy, too. You can do that via ProxyRequests
as described in the official Apache2 docs. The way I did this on a server is to put this in your server-wide config (you should verify on your own that this is secure enough):
# disables forward proxy
ProxyRequests Off
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论