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?
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?
It depends.
Out of the box, putting nginx in front as a reverse proxy is going to give you:
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.
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 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.
The Gorilla web toolkit gives you:
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.
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."
© 2022 - 2024 — McMap. All rights reserved.