I am working on a web app backend in golang that has a JSON api, it resides behind nginx 1.8.0.
Nginx Config:
server {
listen 80;
server_name someserver.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
# The go server handles the chunking, so with proxy_buffering on it messes up the response
proxy_buffering off;
}
}
I handle routes like:
router.GET("/api/cases", checkAuth(api.GetAllCases))
My checkAuth middleware looks like:
func checkAuth(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
url := strings.Split(r.URL.String(), "/")
if notAuthenticatedCode {
http.Error(w, "", http.StatusUnauthorized)
} else {
if url[1] == "api" {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
} else {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
h(w, r, ps)
}
}
}
And finally, a JSON endpoint that looks like:
func (api API) GetAllCases(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// It used to write the json in a different manner, I think this
// is where I started receiving the error when I changed to this
json.NewEncoder(w).Encode(Cases)
}
At some point while working on this I began receiving an error about incomplete chunked encoding. I'm not 100% sure what I changed when I began receiving this error. While investigating this issue I discovered that apparently the go server is handling response chunking, then nginx would also try to handle it causing issues. I added "proxy_buffering off" into the location block of the nginx config and it corrected this issue.
My question: Am I missing out on anything with this set up versus allowing nginx to handle the buffering and disabling it in Go? If I am, how would I disable the chunking in the go server?
It seems to me that nginx should be handling the chunking/compressing/etc and golang shouldn't, but may be this is a naive assumption. Any advice would be greatly appreciated. Thanks!
If the response body is smaller than default buffer size (4k), try setting proxy_buffer_size
to reduce the buffer size.