Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang JSON编码器和nginx代理缓冲_Json_Nginx_Go - Fatal编程技术网

Golang JSON编码器和nginx代理缓冲

Golang JSON编码器和nginx代理缓冲,json,nginx,go,Json,Nginx,Go,我在golang开发一个web应用后端,它有一个JSON api,它位于nginx 1.8.0之后 Nginx配置: server { listen 80; server_name someserver.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host;

我在golang开发一个web应用后端,它有一个JSON api,它位于nginx 1.8.0之后

Nginx配置:

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;
  }
}
我处理的路线如下:

router.GET("/api/cases", checkAuth(api.GetAllCases))
我的checkAuth中间件如下所示:

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)
    }
  }
}
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)
}
最后,一个JSON端点看起来像:

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)
    }
  }
}
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)
}
在做这项工作的某个时候,我开始收到一个关于不完整分块编码的错误。当我开始收到这个错误时,我不能100%确定我改变了什么。在调查这个问题时,我发现go服务器显然正在处理响应分块,然后nginx也会尝试处理它导致的问题。我在nginx配置的location块中添加了“proxy\u buffering off”,它纠正了这个问题

我的问题:与允许nginx处理缓冲并在Go中禁用缓冲相比,我是否错过了任何设置?如果是,我将如何禁用go服务器中的分块


在我看来,nginx应该处理分块/压缩/etc,而golang不应该,但这可能是一个天真的假设。如有任何建议,将不胜感激。谢谢

如果响应体小于,请尝试设置
proxy\u buffer\u size
以减小缓冲区大小。

为什么在这里使用nginx?我没有一个真正好的答案。这是我进入项目时的设置。我认为最终它将实现负载平衡,并拥有多个上游服务器。哦,nginx还处理生产环境中的ssl终止。在go中禁用分块编码将很不方便。您需要弄清楚为什么nginx会截断响应(可能是创建临时文件的权限问题),或者关闭
proxy\u buffering
。进行编码没有什么错。谢谢,这解决了我的疑问。如果你想留下来作为回答,我会接受的。