英文:
Deploying Go web applications with Apache
问题
我找不到一个用于部署Go Web应用程序的mod_go。是否有其他方法可以在Apache Web服务器(甚至IIS)中运行Go的Web应用程序?
更新:现在,经过将近一年的全职使用Go,使用Go与Apache的方式会使Go的目标(高并发性能)失去意义。我使用nginx作为http/https的反向代理,并将我的Go后端放在其后面。尽管在使用Go时,你需要改变一点对Web应用程序的思维方式。
英文:
I can not find a mod_go for deploying Go web applications. Is there any other way to run web applications in Go with an Apache web server (or even IIS)?
Update: Now after doing Go full time for nearly a year; doing this (Go with Apache) is nullifying the very purpose of Go (Performance in terms of being highly concurrent). And I'm using nginx as a reverse proxy for http/https and having my Go backends behind it nicely. Though you have to change your mindset on webapps a bit when using Go.
答案1
得分: 30
没有mod_go。(至少我没有听说过这样的东西。)
一个go web应用本身就是一个功能强大的web服务器。你可以在应用中监听80端口,然后直接在服务器上运行它。相信我:它真的有效!
但是如果你不这样做(出于一些原因,比如在同一台机器上有其他虚拟服务器、负载均衡等),你可以使用像nginx或Apache这样的HTTP服务器作为你的Go应用的HTTP代理。我使用nginx,它非常好用。这是一个过时但仍然非常有用的关于如何使用nginx进行代理的指南。我没有用Apache做过,但是这个应该会有所帮助。
我建议你使用Go web应用本身或者nginx作为HTTP代理。
英文:
There's no mod_go. (At least I've not heard of such a thing.)
A go web app by itself is a capable web server. You can listen to port 80 in your app and then directly run it on your server. Trust me: it really works!
But if you are not doing that (for reasons such as having other virtual servers on the same machine, load balancing, etc.), you can use a HTTP server such as nginx or Apache as a HTTP proxy in front of your Go app. I use nginx and it's great. Here's an outdated but still very useful guide on how to do that with nginx. I haven't done it with Apache, but this should help.
I recommend your Go web app by itself or nginx as a HTTP proxy.
答案2
得分: 24
除了其他选项之外,还有net/http/fcgi包。这与CGI选项类似,但它使用FastCGI,如果需要,您的应用程序可以保持状态。
这是jimt示例的FastCGI版本。请注意,只有两行不同。根据您如何配置Apache,您可能需要将第一个参数更改为不同的内容,但nil是常见情况。
package main
import (
"fmt"
"net/http"
"net/http/fcgi"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
}
func main() {
http.HandleFunc("/", hello)
fcgi.Serve(nil, nil)
}
英文:
In addition to the other options, there's also the net/http/fcgi package. This is similar to the CGI option, but it uses FastCGI and your application can keep state if it needs to.
Here's the FastCGI version of jimt's example. Note that only two lines are different. Depending on how you configure Apache, you may have to change the first argument to something different, but nil is the common case.
package main
import (
"fmt"
"net/http"
"net/http/fcgi"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
}
func main() {
http.HandleFunc("/", hello)
fcgi.Serve(nil, nil)
}
答案3
得分: 15
对于那些感兴趣的人,这里有一个最近发布的Apache的mod_go:
https://github.com/idaunis/mod_go
英文:
For those interested there is a recently released mod_go for Apache in here:
答案4
得分: 10
虽然不是最理想的方式,但你可以将Go程序作为CGI脚本运行,将它们放置在cgi-bin目录中。你可以通过server.com/cgi-bin/myapp?foo=bar
的方式调用它们,就像调用其他页面一样。
一个示例程序如下所示:
package main
import (
"fmt"
"net/http"
"net/http/cgi"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
}
func main() {
http.HandleFunc("/", hello)
cgi.Serve(nil)
}
这种方式不如将程序作为独立服务器运行的优点在于,使用CGI方法时,每个请求都会调用该程序。因此,其中的任何状态都不会持久存在。
为了清晰起见:你应该将编译后的二进制文件放置在cgi-bin目录中,而不是程序源代码。
英文:
While not ideal, you can run Go programs as CGI scripts by placing them in the cgi-bin directory. You can invoke them like any other page through server.com/cgi-bin/myapp?foo=bar
An example program would look like this:
package main
import (
"fmt"
"net/http"
"net/http/cgi"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
}
func main() {
http.HandleFunc("/", hello)
cgi.Serve(nil)
}
The reason this is not as optimal as running the program as its own server, is that with a cgi approach, the program is invoked for every single request. So any state inside it does not persist.
For clarity: You should place the compiled binary in the cgi-bin directory. Not the program source.
答案5
得分: 4
这将代理域名和子域名请求到指定的端口。
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName www.yourdomain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName subdomain.yourdomain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:8081/
ProxyPassReverse / http://127.0.0.1:8081/
</VirtualHost>
英文:
This would proxy the domain and subdomain requests to the specified ports.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName www.yourdomain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName subdomain.yourdomain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:8081/
ProxyPassReverse / http://127.0.0.1:8081/
</VirtualHost>
答案6
得分: 2
我只是使用Web服务器的代理功能,并将我的应用程序作为常规守护进程(使用daemonize)在服务器上运行。在Apache上,可以使用ProxyPass
+ ProxyPreserveHost
来实现。
英文:
I just use the web server's proxy facility and run my app as a regular daemon (using daemonize) on the server. On apache that would be ProxyPass
+ ProxyPreserveHost
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论