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

4

107

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?

Riparian answered 21/7, 2013 at 20:29 Comment(4)
fyi, had a lot of tcp errors like connection reset by peer, and i/o timeouts using Go http server for service handling 100s of large POST requests a minute. Put nginx in front of it - no more problems.Dobruja
Eventually I decided to use Go http server, without NGINX, and it works very well. I haven't been running in any problem. But my service is not running large POST requests as in your case.Riparian
THIS IS NOT NECESSARILY OPINION BASED. Closing it as such shows a severe lack of understanding of the considerations and caveats that an answer to such a question can fill in and provide. The selected answer is a great example. In fact, I see relevant new considerations in all provided answers.Kana
Yes this is not opinion based.Babs
A
153

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.

Ambert answered 21/7, 2013 at 22:32 Comment(5)
thanks for your reply! - 1) does Go HTTP produce some kind of logs? - 2) does nginx reduce somehow the request/response throughput?Riparian
@DanieleB The Go HTTP server produces only the logs you want it to (i.e. using the log package). If you want to log IP address, resource access, etc, you'll need to write that. Same goes for setting headers, beyond the basics. Although I don't have any concrete data, nginx in front of Go should really be no slower than Go: in fact it may be faster thanks to gzip and its own optimisations. The "cost" will be more memory/CPU utilisation, but nginx is very efficient on that front too.Ambert
One more big feature: How will you upgrade/maintain your app (without dropping packets while it's down)? Nginx will let you steer traffic around without loosing packets.Chink
On my Raspberry Pi, putting Nginx in front of Go significantly increased page load speed.Graniah
you can load balance with nginx - you cannot with Go built-in. Therefore I recommend using nginx in frontRamer
S
19

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.

Starcrossed answered 21/7, 2013 at 20:59 Comment(3)
yes, I am actually only using it for serving dynamic data. So, I guess I don't need NGINX then! Thank for your replyRiparian
Varnish/HAProxy are not required, Nginx has similar toolset for caching and load balancing.Mayfair
@mikhailov isn't that what I said? nginx works well for it; though personally I often prefer Varnish or HAproxy. I find them easier to configure and operate.Entice
P
8

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.

Parable answered 19/12, 2015 at 16:42 Comment(0)
D
3

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."

Diverge answered 19/3, 2016 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.