使用Go的HTTP服务器进行生产部署

huangapple go评论83阅读模式
英文:

Using Go's HTTP server for production

问题

我是一名新手 PHP 开发者,对 Golang 还不太熟悉。PHP 自带一个内置服务器,但不建议在生产环境中使用。我读过 astaxie 的《Go Web 编程》这本书,也看过 golang.org 上关于编写 Web 应用的示例。它们都使用了 http.ListenAndServe() 来创建一个 Web 服务器。我只想知道这个服务器能否用于生产环境,还是像 PHP 的内置服务器一样只是一个虚拟服务器?我所说的生产环境是指,它能否处理像 Apache 或 Nginx 服务器那样的大流量?

英文:

I'm a PHP developer new to Golang. PHP comes with an inbuilt server which is not recommended for production. I read the beautiful book by astaxie on web development in Go and also the golang.org example on writing a web app. They both use the http.ListenAndServe() to create a web server. I just want to know if this server can be used in production or is it just a dummy server like PHP inbuilt server? By production I mean, can it handle huge traffic like an Apache or ngnix server?

答案1

得分: 14

简短回答:是的。

更详细的回答:你当然可以使用内置的Web服务器来处理生产流量。有很大的可能你今天已经使用过它,因为谷歌就是使用它来提供一些流量的。我知道很多公司,包括我所在的公司,都在使用它来处理生产流量。顺便说一下,我不知道有哪个在Go中广泛使用的Web服务器不使用标准的Web服务器。

不过要记住,你需要调整一些参数,比如客户端超时时间,使其真正健壮,并且可能需要限制传入的连接等。

英文:

Short answer: YES.

Longer answer: you can certainly use the built in Web server for production traffic. There's a good chance you've used it today, since Google serves some traffic using it. I know a lot of companies, including the one I work for that use it for production traffic. BTW I don't know of a Web server widely used in Go that does not use the standard web server.

Keep in mind though that you need to tweak things like client timeouts to make it really robust, and perhaps limit incoming connections, etc.

答案2

得分: 7

为了补充@Not_a_Golfer的回答:Go的Web服务器非常稳定,并且到目前为止经过了很好的测试。

然而,有一些原因可能会导致你将其放在像nginx、Apache或HAProxy这样的反向代理后面,包括:

  • SSL终止(nginx具有许多额外的TLS功能,你将不得不部分自己实现,例如简单的OCSP插销支持)
  • 代理缓存(从缓存中提供静态响应,或者如果你的Go应用程序崩溃则返回500)
  • 高性能日志记录
  • SPDY支持(虽然Go和nginx可能会同时获得HTTP/2)
  • 内置的gzip支持和选项
  • 静态文件的文件描述符缓存

一般来说,如果有选择的话,我更喜欢将nginx放在前面,因为虽然你可以在Go中实现所有这些功能,但会有一些重复造轮子的情况发生。你可以使用类似gorilla/handlers和Go自己的crypto/tls库的中间件来实现大部分功能,如果你不是运维人员和/或希望保持非常简洁,那么直接从Go运行所有内容对于生产环境仍然是可以的。

英文:

To add to @Not_a_Golfer's answer: Go's web-server is extremely solid and pretty well tested thus far.

There are, however, reasons why you might put it behind a reverse proxy like nginx, Apache or HAProxy, including:

  • SSL termination (nginx has a lot of additional TLS features that you'll have to partially implement yourself, like easy OCSP stapling support)
  • Proxy caching (serving static responses from cache, or 500 if your Go app crashes)
  • Performant logging
  • SPDY support (although both Go & nginx will probably get HTTP/2 at the same time)
  • Built-in gzip support and options
  • File descriptor caching for static files

I generally prefer to put nginx in front if I have the option, because although you can implement all of those features in Go, there's a bit of wheel re-invention going on. You can get most of the way there with middleware like gorilla/handlers and Go's own crypto/tls lib, and if you're not an ops person and/or want to keep things really thin, then running everything directly from Go is still fine for production.

huangapple
  • 本文由 发表于 2015年6月15日 01:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/30832195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定