使用Nginx作为Go语言的Web服务器前端有哪些好处?

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

What are the benefits of using Nginx in front of a webserver for Go?

问题

使用Nginx作为我的服务器前端与仅使用Go HTTP服务器相比,有哪些好处?

英文:

I am writing some webservices returning JSON data, which have lots of users.

What are the benefits of using Nginx in front my server compared to just using the go http server?

答案1

得分: 152

这取决于情况。

默认情况下,将nginx作为反向代理放在前面会给你带来以下好处:

  • 访问日志
  • 错误日志
  • 简单的SSL终止
  • SPDY支持
  • gzip支持
  • 通过几行代码轻松设置特定路由的HTTP头
  • 非常快速的静态资源服务(如果你是从S3等地方提供服务,这个可能不太相关)

Go的HTTP服务器非常好,但是你需要重新发明一些轮子来完成其中的一些功能(这没问题:它并不意味着要满足所有人的需求)。

我一直觉得将nginx放在前面更容易一些,因为它擅长做“Web服务器”的事情。我的Go应用程序只负责应用程序相关的事情,以及它所需的最基本的头部等。不要认为将nginx放在前面是一件“坏”事情。

英文:

It depends.

Out of the box, putting nginx in front as a reverse proxy is going to give you:

  • Access logs
  • Error logs
  • Easy SSL termination
  • SPDY support
  • gzip support
  • Easy ways to set HTTP headers for certain routes in a couple of lines
  • Very fast static asset serving (if you're serving off S3/etc. though, this isn't that relevant)

The Go HTTP server is very good, but you will need to reinvent the wheel to do some of these things (which is fine: it's not meant to be everything to everyone).

I've always found it easier to put nginx in front—which is what it is good at—and let it do the "web server" stuff. My Go application does the application stuff, and only the bare minimum of headers/etc. that it needs to. Don't look at putting nginx in front as a "bad" thing.

答案2

得分: 19

Go的标准http服务器很好。如果你的应用程序主要/仅仅是“动态”请求/响应,那么这确实是最好的方式。

你可以使用nginx来提供静态资源,但很可能Go的标准服务器对此也很好。如果你需要更高的性能,你应该使用CDN或尽可能多地使用Varnish进行缓存。

如果你需要在同一个IP地址上提供不同的应用程序,nginx是一个很好的选择,可以用作代理来分发请求到不同的应用程序;不过对于这种情况,我更倾向于从工具箱中选择Varnish或HAProxy。

英文:

The standard http server of Go is fine. If your application mostly/only are "dynamic" requests/responses, then it's really the best way.

You could use nginx to serve static assets, but most likely the standard Go one is fine for that, too. If you need higher performance you should just use a CDN or cache as much as you can with Varnish (for example).

If you need to serve different applications off the same IP address, nginx is a fine choice for a proxy to distribute requests between the different applications; though I'd more often get Varnish or HAProxy out of the toolbox for that sort of thing.

答案3

得分: 8

Gorilla web toolkit提供以下功能:

  • 高级路由(域名/子域名限制,正则路径匹配)。
  • gzip支持(通过middleware handlers)。
  • 输出Apache Common Log Format的日志中间件处理程序。
  • 安全加密的cookies。
  • 会话。
  • schema包将表单值转换为结构体。

这填补了Go的net/http和像NGINX这样的HTTP服务器之间的很大差距。

就个人而言,如果我知道可以使用CDN替代,我会避免在net/http之上安装和配置另一个HTTP服务器。

我认为net/http在任何标准库中都拥有最强大的HTTP服务器。

英文:

The Gorilla web toolkit gives you:

  • Advanced routing (domain/subdomain restriction, regex path matching).
  • gzip support (via middleware handlers.)
  • Logging middleware handler that outputs in Apache Common Log Format.
  • Secure encrypted cookies.
  • Sessions.
  • schema package converts form values to a struct.

This fills much gap between Go's net/http and HTTP servers like NGINX.

Personally, I'd avoid installing and configuring another HTTP server on top of net/http if I know I can plug in a CDN instead.

I think net/http has the most powerful HTTP server in any standard library.

答案4

得分: 3

从https://blog.gopheracademy.com/caddy-a-look-inside/看起来,Go可以使用中间件处理gzip、错误、静态文件、路由和HTTP头。
下面这行代码,来自博客,展示了如何处理这样的请求。

logHandler(gzipHandler(fileServer))

他们以一种非常有趣的方式处理错误日志。只要你的中间件返回一个错误代码(int),错误处理中间件就会自动处理它。他们甚至已经配置了整个站点,就像Nginx一样。"Gopher Academy网站的nginx.conf文件超过115行。而等效的Caddyfile只有50行。"

英文:

From https://blog.gopheracademy.com/caddy-a-look-inside/ it looks like Go can handle gzip, errors, static files, routing and http headers using Middleware.
The line below, from the blog, show how you would handle such a request.

logHandler(gzipHandler(fileServer))

They handle error logging in a really interesting way. As long as your middleware returns an error code (int), the error-handling middleware automatically handles it. They've even gone as far as configuring the whole site in Go like Nginx would. "The nginx.conf file for all the Gopher Academy websites was over 115 lines long. The equivalent Caddyfile is only 50 lines."

huangapple
  • 本文由 发表于 2013年7月22日 04:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/17776584.html
匿名

发表评论

匿名网友

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

确定